← 返回资讯屏轩教育 · 行业资讯

Algorithms, Programming and Logic | 算法、编程与逻辑

2026-08-03 04:51 · 本文部分内容由AI辅助创作

📚 Algorithms, Programming and Logic | 算法、编程与逻辑

In the world of computer science, algorithms, programming and logic form the essential foundation that enables us to solve problems using computers. An algorithm is a step-by-step set of instructions to complete a task; programming is the process of writing these instructions in a language a computer can understand; and logic provides the rules for making decisions and controlling the flow of a program. Together, they allow us to create everything from simple calculators to complex artificial intelligence systems. This revision guide explores the key concepts you need to master for Cambridge IGCSE Computer Science, with clear examples, practical techniques and a focus on computational thinking.

在计算机科学的世界里,算法、编程和逻辑构成了用计算机解决问题的基石。算法是一套完成任务的步骤指令;编程是用计算机能理解的语言编写这些指令的过程;逻辑则提供了做出决策和控制程序流程的规则。它们共同让我们能够创造从简单计算器到复杂人工智能系统的一切。本复习指南深入探讨剑桥 IGCSE 计算机科学需要掌握的核心概念,配有清晰的示例、实用的技巧,并聚焦计算思维。

1. What is an Algorithm? | 什么是算法?

An algorithm is a precise, step-by-step plan for solving a problem or completing a task. It must have a clear start and end point, unambiguous instructions, and it must produce the correct output for all valid inputs. Every algorithm must be written before any code is typed, as it acts as the blueprint for a program. Examples include recipes, instructions for assembling furniture, or a set of rules for calculating the total price of items in a shopping cart. In computing, algorithms are often expressed using flowcharts or pseudocode before they are translated into a programming language like Python.

算法是解决问题或完成任务的一套精确、逐步的计划。它必须有明确的起点和终点,指令毫不含糊,并且必须对所有有效输入产生正确输出。在编写任何代码之前,都要先设计好算法,因为它就像程序的蓝图。例如食谱、家具组装说明书,或者计算购物车商品总价的一套规则。在计算中,算法通常先用流程图或伪代码表达出来,然后再转换成 Python 这样的编程语言。

The key properties of a good algorithm are: unambiguous – each step is clear and has only one meaning; finite – it stops after a limited number of steps; input – it takes zero or more inputs; output – it produces at least one result; and effective – each step is simple enough to be carried out exactly and in a reasonable amount of time.

一个好的算法应具备以下关键属性:明确性——每步都清晰且只有一种含义;有限性——在有限步骤后停止;输入——接受零个或多个输入;输出——至少产生一个结果;有效性——每一步都足够简单,可以准确且合理的时间内执行。


2. Representing Algorithms: Flowcharts and Pseudocode | 算法的表示方法:流程图与伪代码

Algorithms can be represented visually using flowcharts or in written form using pseudocode. A flowchart uses special symbols: an oval for start/stop, a rectangle for a process or calculation, a diamond for a decision (yes/no or true/false), and a parallelogram for input/output. Arrows connect these symbols to show the sequence of steps. Flowcharts help beginners visualise the logic of a solution before coding.

算法可以用流程图直观地表示,也可用伪代码以书面形式呈现。流程图使用特殊符号:椭圆表示开始/结束,矩形表示处理或计算,菱形表示判断(是/否或真/假),平行四边形表示输入/输出。箭头将这些符号连接起来,显示步骤的顺序。流程图帮助初学者在编码前将解题逻辑形象化。

Pseudocode is a simplified, language-independent way of writing algorithms using ordinary English words and common programming structures such as INPUT, OUTPUT, IF…THEN…ELSE, REPEAT…UNTIL, and WHILE…ENDWHILE. It ignores strict syntax rules, allowing the designer to focus on the logic. For example, to find the larger of two numbers:

伪代码是一种简化的、独立于语言的算法书写方式,使用日常英语单词及常见的编程结构,如 INPUT、OUTPUT、IF…THEN…ELSE、REPEAT…UNTIL 和 WHILE…ENDWHILE。它忽略严格的语法规则,让设计者专注于逻辑。例如,找出两个数中较大的一个:

INPUT a, b
IF a > b THEN
    OUTPUT a
ELSE
    OUTPUT b
ENDIF


3. Programming Basics: Variables, Data Types and Assignment | 编程基础:变量、数据类型与赋值

In programming, a variable is a named storage location in memory that holds a value which can change during execution. Variables have a data type that determines what kind of data they can store. Common data types in IGCSE Computer Science include integer (whole numbers), real/float (numbers with decimal points), char (a single character), string (a sequence of characters), and boolean (TRUE or FALSE). Choosing the correct data type ensures efficient use of memory and prevents errors.

在编程中,变量是内存中一块命名的存储位置,存放的值在程序执行过程中可以改变。变量有着确定其可存储数据类型的数据类型。IGCSE 计算机科学中常见的数据类型包括整数(整数)、实数/浮点数(带小数点的数)、字符(单个字符)、字符串(一串字符)以及布尔类型(TRUE 或 FALSE)。选用正确的数据类型能保证高效地使用内存,并防止错误。

Assignment statements are used to store a value into a variable, typically written as Variable ← Expression. For instance, score ← 0 sets the variable score to zero. In many languages, the equals sign is used (e.g., score = 0), but it is important not to confuse it with mathematical equality. You should also understand how to declare constants, which are named values that cannot be changed during the program’s execution, improving code maintainability.

赋值语句用于把值存入变量,通常写为 Variable ← Expression。例如,score ← 0 将变量 score 设为 0。许多语言使用等号(如 score = 0),但绝不能与数学中的相等混淆。你还应了解如何声明常量,即程序执行期间不可更改的命名值,这能够提升代码的可维护性。


4. Sequence, Selection and Iteration | 顺序、选择与迭代

All programs, regardless of complexity, are built using three fundamental control structures: sequence, selection and iteration. Sequence means instructions are executed one after another in the order they appear. This is the default flow of a program. Selection allows the program to choose between different paths based on a condition. It is implemented using IF…THEN…ELSE statements, or CASE statements for multiple alternatives. Iteration (also called repetition or looping) enables a block of code to run repeatedly either a fixed number of times (count-controlled, using FOR loops) or until a condition is met (condition-controlled, using WHILE or REPEAT loops).

所有程序,无论多复杂,都由三种基本控制结构构建:顺序、选择和迭代。顺序指指令按出现的先后顺序依次执行。这是程序的默认流程。选择让程序能够根据条件在不同的路径中进行选择。它通过 IF…THEN…ELSE 语句实现,或使用 CASE 语句处理多个分支。迭代(也叫循环)使一段代码能够重复运行,可以运行固定的次数(计数控制,使用 FOR 循环),也可以一直运行到某个条件满足为止(条件控制,使用 WHILE 或 REPEAT 循环)。

For example, a program to output all even numbers from 2 to 10 might use a FOR loop: FOR i ← 2 TO 10 STEP 2. A program to keep asking for a password until it is correct might use a WHILE loop: WHILE input ≠ password DO. Understanding when to use each type of iteration is a key skill for writing efficient and bug-free code.

例如,输出 2 到 10 所有偶数的程序可能会使用 FOR 循环:FOR i ← 2 TO 10 STEP 2。一个不断要求输入密码直到正确的程序可能会使用 WHILE 循环:WHILE input ≠ password DO。理解何时使用每种迭代类型是编写高效且无 bug 代码的关键技能。


5. Logical Operators and Boolean Logic | 逻辑运算符与布尔逻辑

Boolean logic is named after George Boole and deals with values that are either TRUE or FALSE. It is central to decision-making in programs and to how computers perform arithmetic and make comparisons. The three fundamental logical operators are AND, OR and NOT. In many exam board notations, they are symbolised as ∧, ∨ and ¬ respectively. The AND operator returns TRUE only if both operands are TRUE; OR returns TRUE if at least one operand is TRUE; NOT simply reverses the truth value.

布尔逻辑以乔治·布尔的名字命名,处理的是只有 TRUE(真)或 FALSE(假)的值。它是程序决策和计算机执行算术与比较操作的核心。三种基本的逻辑运算符是与、或和非。在许多考试局的符号系统中,它们分别表示为 ∧、∨ 和 ¬。与运算符只有两个操作数都为 TRUE 时才返回 TRUE;或运算符只要至少有一个操作数为 TRUE 就返回 TRUE;非运算符则简单地反转真值。

Boolean expressions can be combined to form complex conditions. For example, a condition that checks if a number x is between 10 and 20 inclusive could be written as (x ≥ 10) ∧ (x ≤ 20). In exams you must be able to construct, evaluate and simplify such expressions. Truth tables are used to list every possible input combination and the resulting output.

布尔表达式可以组合形成复杂的条件。例如,检查数字 x 是否在 10 到 20 之间(含)的条件可写作 (x ≥ 10) ∧ (x ≤ 20)。在考试中你必须能够构建、计算并简化这类表达式。真值表则用来列出所有可能的输入组合及其产生的输出。


6. Truth Tables and Logic Circuits | 真值表与逻辑电路

A truth table is a systematic way of displaying how a logical expression behaves for every possible combination of inputs. For a simple AND gate with two inputs A and B, the output is TRUE only when both are TRUE. The table below shows the truth tables for the basic gates. You may also be asked to draw or interpret logic circuits formed by combining these gates to perform more complex operations.

真值表是一种系统性地展示逻辑表达式针对每种可能输入组合之行为的方法。对于一个简单的两输入与门(A 和 B),只有当两者都为 TRUE 时输出才为 TRUE。下表展示了基本逻辑门的真值表。你可能还需要绘制或解读将这些门组合起来执行更复杂操作的逻辑电路图。

ABA ∧ B (AND)A ∨ B (OR)¬A (NOT A)
00001
01011
10010
11110

Note: In logic, 1 represents TRUE and 0 represents FALSE. In logic circuits, gates are represented by distinctive shapes: a D-shaped gate for AND, a curve at the back for OR, and a triangle with a small circle for NOT. Understanding how these gates can be combined to form half-adders or full-adders is essential for the hardware part of the syllabus.

注意:在逻辑中,1 代表 TRUE,0 代表 FALSE。在逻辑电路中,各门由独特的形状表示:与门是 D 形,或门背面有弧度,非门是一个三角形加一个小圆圈。理解这些门如何组合形成半加器或全加器,对考纲中硬件部分的内容至关重要。


广告 / Sponsored

7. Common Algorithms: Searching and Sorting | 常见算法:搜索与排序

Searching and sorting are fundamental operations in computing. A linear search examines each element in a list sequentially until the target is found or the list ends. It is simple but inefficient for large datasets, with a worst-case time proportional to the size of the list. A binary search works only on a sorted list; it repeatedly divides the list in half, comparing the middle element to the target until it is found or the sublist is empty. Its time complexity is logarithmic, making it much faster for large datasets.

搜索和排序是计算中的基础操作。线性搜索逐个检查列表中的每一个元素,直到找到目标或列表结束。它很简单,但对大数据集效率低下,最坏情况耗时与列表大小成正比。二分搜索仅在有序列表上工作;它反复将列表对半分,将中间元素与目标比较,直到找到目标或子列表为空。它的时间复杂度是对数级的,大数据集下速度要快得多。

Sorting algorithms arrange data in a particular order. The bubble sort repeatedly steps through the list, compares adjacent items and swaps them if they are in the wrong order. Larger elements 'bubble' to the end of the list. Though easy to understand, it is inefficient with a worst-case performance proportional to n². The insertion sort builds the final sorted array one item at a time, taking the next element and inserting it into the correct position among the already sorted items. It is more efficient for small or nearly sorted lists.

排序算法将数据按特定顺序排列。冒泡排序反复遍历列表,比较相邻项并在顺序错误时交换它们。较大的元素会“冒泡”到列表末尾。它虽易于理解,但效率低下,最坏情况性能与 n² 成正比。插入排序一次构建一个元素的有序数组,取出下一个元素并将其插入已排序部分的正确位置。对于小型或近乎有序的列表,它更高效。


8. Algorithm Efficiency and Readability | 算法效率与可读性

Two important criteria for judging an algorithm are its efficiency and readability. Efficiency generally refers to how the running time or memory usage grows as the input size increases. This is expressed using Big O notation, for example O(n) for linear search and O(log n) for binary search. In IGCSE, you are expected to identify which of two algorithms is more efficient by considering the number of comparisons or steps required for a given input size.

评判算法的两条重要标准是效率和可读性。效率通常指随着输入规模的增长,运行时间或内存用量如何增长。这使用大 O 表示法来表达,例如线性搜索为 O(n),二分搜索为 O(log n)。在 IGCSE 中,你需要通过考虑给定输入规模所需的比较次数或步骤数,识别哪种算法更高效。

Readability is equally important because programs are often maintained by other programmers. Good practices include using meaningful variable names (e.g., studentName instead of sn), adding comments to explain complex logic, proper indentation to show structure, and avoiding 'magic numbers' by using named constants. A readable algorithm is easier to debug, modify and test, reducing the overall cost of software development.

可读性同样重要,因为程序经常需要由其他程序员维护。良好的实践包括:使用有意义的变量名(例如用 studentName 而不是 sn),添加注释解释复杂逻辑,使用正确的缩进来展示结构,并通过命名常量来避免“魔法数字”。可读性高的算法更容易调试、修改和测试,从而降低软件开发的整体成本。


9. Program Design and Debugging | 程序设计与调试

Before coding, a clear design phase is essential. This involves defining the problem precisely, identifying inputs and outputs, breaking the problem into smaller sub-problems (decomposition), and then designing the algorithm using tools like structure diagrams, flowcharts or pseudocode. In IGCSE, you may be asked to interpret or correct a given design, or to create your own for a specified scenario.

在编码之前,清晰的设计阶段必不可少。这包括准确定义问题、识别输入和输出、将问题分解为更小的子问题(分解),然后使用结构图、流程图或伪代码等工具设计算法。在 IGCSE 中,你可能需要解释或纠正给定的设计,或针对指定场景创建自己的设计。

Debugging is the process of finding and fixing errors (bugs) in a program. Common types of error include syntax errors (mistakes in the programming language rules), logic errors (the program runs but produces incorrect results), and run-time errors (crashes during execution, e.g., division by zero). Strategies such as dry-running code with trace tables, using breakpoints, and printing intermediate values help isolate the source of the problem.

调试是寻找并修复程序中错误(bug)的过程。常见的错误类型包括语法错误(违反编程语言规则)、逻辑错误(程序能运行但结果不正确)和运行时错误(执行中崩溃,例如除零错误)。用跟踪表进行手动执行代码、使用断点以及打印中间值等策略,有助于隔离问题根源。


10. Subroutines and Modularity | 子程序与模块化

A subroutine is a named block of code that performs a specific task and can be called from elsewhere in the program. It comes in two forms: functions, which return a single value, and procedures, which do not return a value but may produce an output or affect the program state. In pseudocode, functions are often defined using FUNCTION and procedures using PROCEDURE, with parameters passed in parentheses.

子程序是一段命名的代码块,执行特定任务并可以被程序的其他地方调用。它有两种形式:函数(返回单个值)和过程(不返回值,但可能产生输出或影响程序状态)。在伪代码中,函数常使用 FUNCTION 定义,过程使用 PROCEDURE 定义,并通过括号传递参数。

Modularity means breaking a large program into smaller, independent, manageable modules (subroutines). This offers several advantages: code can be reused (written once, called many times), the program becomes easier to read and test, and a team can work on different modules simultaneously. Libraries are collections of ready-made subroutines that programmers can use without having to rewrite common functions like sorting or mathematical calculations.

模块化是指将大型程序拆分成更小、独立、可管理的模块(子程序)。这带来许多优点:代码可以复用(一次编写,多次调用),程序变得更容易阅读和测试,团队可以并行开发不同的模块。库是预先编写好的子程序集合,程序员可以直接使用,无需重写排序或数学计算等常用功能。


11. Testing and Verification | 测试与验证

Testing is the process of running a program with the intention of uncovering errors. It should cover three categories of test data: normal data (values that are accepted and should work correctly), boundary/extreme data (values at the limits of what is allowed), and erroneous/abnormal data (values that should be rejected). A well-designed test plan lists each test case with its input, expected output and the actual result, helping to locate where the program fails.

测试是运行程序以期发现错误的过程。它应覆盖三类测试数据:正常数据(可接受且应正常工作的值)、边界/极端数据(位于允许范围极限的值)以及异常/错误数据(应被拒绝的值)。一份设计良好的测试计划会列出每个测试用例及其输入、预期输出和实际结果,帮助定位程序出错的位置。

Verification ensures that the program does the job correctly according to its specification. Techniques include trace tables (manually following the code step by step to track variable values), visual checks of output, and comparing results with manual calculations. Validation, often confused with verification, is about checking that input data is sensible (e.g., a date of birth should not be in the future). Both are essential for robust software.

验证确保程序根据其规格说明正确地完成任务。方法包括跟踪表(手工逐步跟踪代码以记录变量值)、输出的可视化检查,以及与手工计算结果的对比。校验(常与验证混淆)则是检查输入数据是否合理(例如出生日期不应在未来)。两者对于稳健的软件都至关重要。


12. Computational Thinking and Problem Solving | 计算思维与问题解决

Computational thinking is a problem-solving approach that draws on concepts fundamental to computer science. It involves four key techniques: decomposition – breaking a complex problem into smaller, more manageable parts; pattern recognition – identifying similarities among and within problems; abstraction – focusing on the important information only, ignoring irrelevant detail; and algorithm design – creating a step-by-step solution to the problem. These skills are not only for programmers; they help in many areas of life, from planning an event to conducting a scientific investigation.

计算思维是一种借鉴计算机科学基本概念的问题解决方法。它包含四项核心技术:分解——将复杂问题拆分为更小、更易管理的部分;模式识别——找出问题之间和内部的相似性;抽象——只关注重要信息,忽略不相关的细节;算法设计——创建解决问题的逐步方案。这些技能不仅对程序员有用,在生活的许多领域都有帮助,从策划活动到进行科学探究。

In the Cambridge IGCSE examination, you may be presented with a real-world scenario and asked to apply computational thinking to propose a computer-based solution. This could involve designing a flowchart, writing pseudocode, justifying the choice of data structures, or evaluating the solution against given success criteria. Practising past paper questions that combine algorithms, programming and logic is the most effective way to prepare.

在剑桥 IGCSE 考试中,你可能会遇到一个真实情境,并被要求运用计算思维提出基于计算机的解决方案。这可能包括设计流程图、编写伪代码、说明数据结构的选择理由,或者根据给定的成功标准评估方案。练习将算法、编程和逻辑结合起来的历年真题是备考最有效的方法。


Published by TutorHao | Computer Science Revision Series | aleveler.com

广告 / Sponsored
现货 Cambridge International AS & ALevel IT Coursebook剑桥国际IGC…
计算机 备考教辅 · 正版现货 · 与本文内容相关
查看详情 / View on Taobao参考价 ¥467.0

更多咨询请联系16621398022(同微信)

备考资讯每日更新,关注不错过
考纲变化 · 科目分析 · 学习方法
扫码关注公众号 tutorhao
在微信里长按二维码识别关注
或截图后用「扫一扫 → 相册」选择本图
公众号:tutorhao