Core Programming Concepts | 编程核心概念

📚 Core Programming Concepts | 编程核心概念

Programming is the process of designing and writing a set of instructions that a computer can follow to solve a problem. This article covers the essential programming concepts required for the CIE IGCSE Computer Science syllabus.

编程是设计并编写一组计算机能够遵循的指令以解决问题的过程。本文涵盖 CIE IGCSE 计算机科学教学大纲所要求的核心编程概念。


1. What Is a Program? | 什么是程序

A program is a sequence of instructions written in a programming language. The computer executes these instructions one after another to perform a specific task, such as calculating a total or displaying a message.

程序是用编程语言编写的一组指令序列。计算机逐条执行这些指令,以完成特定任务,例如计算总和或显示消息。

Key properties of a good program:

一个优秀程序的关键特性:

  • It is correct: it produces the expected output for all valid inputs.
  • 它是正确的:对所有有效输入都能产生预期输出。
  • It is readable: other programmers can understand the code easily.
  • 它是可读的:其他程序员能轻松理解代码。
  • It is efficient: it uses time and memory reasonably.
  • 它是高效的:合理使用时间和内存。

Programs are written using programming languages such as Python, Pascal, or Java. At IGCSE level, you need to understand the general structure of code, not just one language.

程序使用 Python、Pascal 或 Java 等编程语言编写。在 IGCSE 阶段,你需要理解代码的一般结构,而不仅仅是某一种语言。


2. Variables and Constants | 变量与常量

A variable is a named storage location in memory that can hold a value. The value stored in a variable can change during program execution. In contrast, a constant is a named value that does not change while the program is running.

变量是内存中的一个具名存储位置,可以保存一个值。变量的值在程序执行过程中可以改变。相比之下,常量是一个具名值,在程序运行期间不会改变。

Declaration examples:

声明示例:

DECLARE score AS INTEGER
DECLARE PI AS CONSTANT = 3.142

Rules for naming variables and constants:

变量和常量的命名规则:

  • Names must be meaningful, for example totalMarks rather than t.
  • 名称必须有意义,例如用 totalMarks 而不是 t
  • Names usually start with a letter and may contain digits and underscores.
  • 名称通常以字母开头,可以包含数字和下划线。
  • Most languages are case-sensitive, so Score and score are different.
  • 大多数语言区分大小写,因此 Scorescore 不同。

Using meaningful names makes a program self-documenting and easier to debug.

使用有意义的名称能使程序具有自文档性,并更容易调试。


3. Data Types | 数据类型

A data type defines the kind of value a variable can hold. The main data types in IGCSE Computer Science are integer, real, char, string, and Boolean.

数据类型定义了变量可以保存的值的种类。IGCSE 计算机科学中的主要数据类型有整数、实数、字符、字符串和布尔值。

Data Type | 数据类型 Description | 描述 Example | 示例
Integer A whole number without a decimal point 42, -7, 0
整数 没有小数点的整数 42, -7, 0
Real A number with a fractional part 3.14, -0.5, 2.0
实数 带有小数部分的数 3.14, -0.5, 2.0
Char A single character ‘A’, ‘b’, ‘7’
字符 单个字符 ‘A’, ‘b’, ‘7’
String A sequence of characters “Hello”, “A1B2”
字符串 字符序列 “Hello”, “A1B2”
Boolean True or False TRUE, FALSE
布尔 真或假 TRUE, FALSE

Choosing the correct data type is important because it determines what operations can be performed. For example, you cannot multiply two strings, and you cannot use a Boolean value in arithmetic calculations.

选择正确的数据类型非常重要,因为它决定了可以执行哪些操作。例如,不能将两个字符串相乘,也不能在算术计算中使用布尔值。


4. Operators and Expressions | 运算符与表达式

Operators are symbols that perform operations on operands. An expression is a combination of variables, constants, and operators that produces a value.

运算符是对操作数执行操作的符号。表达式是变量、常量和运算符的组合,它产生一个值。

The main arithmetic operators are:

主要的算术运算符有:

  • Addition +, subtraction , multiplication *, division /
  • 加法 +、减法 、乘法 *、除法 /
  • Integer division DIV (or //) and modulus MOD (or %)
  • 整除 DIV(或 //)和取余 MOD(或 %
  • Exponentiation ^ (or **)
  • 幂运算 ^(或 **

Example expressions:

表达式示例:

total = price + tax
remainder = 17 MOD 5 → remainder = 2
result = 2 3 + 4 × 2

Operator precedence follows the usual mathematical rules: brackets first, then exponents, then multiplication and division, then addition and subtraction.

运算符优先级遵循通常的数学规则:先括号,再幂运算,然后乘除,最后加减。

Comparison operators produce Boolean values:

比较运算符产生布尔值:

  • Equal to =, not equal to (or <>)
  • 等于 =、不等于 (或 <>
  • Greater than >, less than <
  • 大于 >、小于 <
  • Greater than or equal to (or >=), less than or equal to (or <=)
  • 大于等于 (或 >=)、小于等于 (或 <=

Logical operators combine Boolean values: NOT, AND, OR. For example, age > 16 AND passed = TRUE.

逻辑运算符组合布尔值:NOT(非)、AND(与)、OR(或)。例如,age > 16 AND passed = TRUE


5. Sequence | 顺序结构

Sequence is the most basic control structure. In a sequence, statements are executed one after another in the order they appear in the program. This is the default flow of execution.

顺序是最基本的控制结构。在顺序结构中,语句按照程序中出现的顺序逐条执行。这是默认的执行流程。

Example of a sequence:

顺序结构示例:

INPUT name
OUTPUT “Hello ” + name
OUTPUT “Welcome to IGCSE Computer Science”

The program first reads a value, then displays a greeting, then displays a second message. Each line happens exactly once and in order.

程序首先读取一个值,然后显示问候语,再显示第二条消息。每一行都恰好执行一次,并且按顺序执行。

Almost every program contains at least one sequence. Even when selection and iteration are used, the individual blocks contain sequences of instructions.

几乎每个程序都至少包含一个顺序结构。即使使用了选择和循环,各个代码块内部也包含指令序列。


6. Selection | 选择结构

Selection allows a program to make decisions. The program evaluates a condition and then chooses one of two or more paths. The most common selection statement is IF-THEN-ELSE.

选择允许程序做出决策。程序对条件进行求值,然后选择两条或多条路径之一。最常见的选择语句是 IF-THEN-ELSE(如果-那么-否则)。

General form:

一般形式:

IF condition THEN
  statements for true
ELSE
  statements for false
ENDIF

Example:

示例:

IF score >= 50 THEN
  OUTPUT “Pass”
ELSE
  OUTPUT “Fail”
ENDIF

If the condition is True, the program runs the first block; otherwise it runs the second block.

如果条件为真,程序运行第一个代码块;否则运行第二个代码块。

CASE statements are another form of selection. They allow a variable to be tested against multiple values. CASE is often clearer than many nested IF statements.

CASE 语句是选择的另一种形式。它允许将一个变量与多个值进行比较。CASE 通常比许多嵌套的 IF 语句更清晰。

Example:

示例:

CASE grade OF
  ‘A’: OUTPUT “Excellent”
  ‘B’: OUTPUT “Good”
  ‘C’: OUTPUT “Average”
ENDCASE


7. Iteration | 循环结构

Iteration, also called repetition or looping, causes a block of code to be executed more than once. There are three types of loops: count-controlled, condition-controlled with pre-test, and condition-controlled with post-test.

循环,也称为重复或迭代,使一段代码被多次执行。循环有三种类型:计数控制循环、先测试条件循环和后测试条件循环。

Count-controlled loop: the loop runs a fixed number of times. A FOR loop is a common example.

计数控制循环:循环运行固定的次数。FOR 循环是常见的例子。

FOR i ← 1 TO 5
  OUTPUT i
NEXT i

This outputs 1, 2, 3, 4, 5.

这将输出 1, 2, 3, 4, 5。

Pre-test loop: the condition is checked before the loop body runs. If the condition is False initially, the loop body may never run. WHILE loops work this way.

先测试循环:在循环体运行之前检查条件。如果初始条件为假,循环体可能永远不会运行。WHILE 循环就是这种方式。

WHILE count < 10
  OUTPUT count
  count ← count + 1
ENDWHILE

Post-test loop: the condition is checked after the loop body runs, so the loop body always runs at least once. REPEAT-UNTIL loops work this way.

后测试循环:在循环体运行后检查条件,因此循环体至少运行一次。REPEAT-UNTIL 循环就是这种方式。

REPEAT
  OUTPUT “Enter a positive number”
  INPUT n
UNTIL n > 0

An important difference: in a WHILE loop, the loop continues while the condition is True; in a REPEAT-UNTIL loop, the loop continues until the condition becomes True.

一个重要区别:在 WHILE 循环中,当条件为真时继续循环;在 REPEAT-UNTIL 循环中,循环一直持续到条件变为真为止。


8. Arrays | 数组

An array is a data structure that stores multiple values of the same data type under a single name. Each element is accessed using an index. In IGCSE, array indexes usually start at 0 or 1 depending on the language.

数组是一种数据结构,在同一个名称下存储多个相同数据类型的值。每个元素通过索引访问。在 IGCSE 中,数组索引通常根据语言从 0 或 1 开始。

Declaring a one-dimensional array:

声明一维数组:

DECLARE marks[1:5] AS INTEGER

This creates an array called marks with 5 elements, indexed 1 to 5.

这将创建一个名为 marks 的数组,包含 5 个元素,索引从 1 到 5。

Accessing and updating array elements:

访问和更新数组元素:

marks[1] ← 78
marks[2] ← 65
total ← marks[1] + marks[2]

Arrays are extremely useful when processing lists of data, such as student scores or daily temperatures. Combined with loops, arrays allow efficient processing of large amounts of data.

数组在处理数据列表(如学生成绩或每日温度)时非常有用。数组与循环结合,可以高效处理大量数据。

Example of using a loop to fill an array:

使用循环填充数组的示例:

FOR i ← 1 TO 5
  INPUT marks[i]
NEXT i

Two-dimensional arrays can store tables of data, such as a table of seats in a cinema. Access uses two indices, for example seat[row, column].

二维数组可以存储数据表,例如电影院座位的表格。访问使用两个索引,例如 seat[row, column]


9. Procedures and Functions | 过程与函数

Procedures and functions are named blocks of code that can be called from different parts of a program. They help to avoid repetition and make programs modular and easier to test.

过程和函数是可以从程序不同位置调用的命名代码块。它们有助于避免重复,使程序模块化并更容易测试。

A procedure performs a task but does not return a value. A function performs a task and returns a single value. For example, a procedure might display a menu, while a function might calculate the area of a circle.

过程执行任务但不返回值。函数执行任务并返回一个值。例如,过程可能显示菜单,而函数可能计算圆的面积。

Procedure example:

过程示例:

PROCEDURE greeting(name : STRING)
  OUTPUT “Hello ” + name
ENDPROCEDURE

Function example:

函数示例:

FUNCTION area(radius : REAL) : REAL
  RETURN 3.142 × radius × radius
ENDFUNCTION

Parameters are the values passed into a procedure or function. They allow the same code to work with different data. Arguments are the actual values supplied when the call is made.

参数是传递给过程或函数的值。它们允许相同的代码处理不同的数据。实参是调用时提供的实际值。

Using the function:

使用函数:

circleArea ← area(5)

This calls the function with argument 5 and stores the returned value in circleArea.

这会以实参 5 调用函数,并将返回值存储在 circleArea 中。


10. Input and Output | 输入与输出

Input allows a program to receive data from the user or from files. Output allows the program to display results or write data to files. At IGCSE level, you need to understand reading from the keyboard and writing to the screen.

输入允许程序从用户或文件接收数据。输出允许程序显示结果或将数据写入文件。在 IGCSE 阶段,你需要理解从键盘读取和输出到屏幕。

Input examples:

输入示例:

INPUT age
name ← READ()

Output examples:

输出示例:

OUTPUT “Your age is ” + age
PRINT “The total is “, total

Input values are usually stored in variables. The data type of the input must match the variable’s declared type.

输入值通常存储在变量中。输入的数据类型必须与变量声明的类型匹配。

Good input/output design includes clear prompts and messages. For example, rather than writing INPUT n, write OUTPUT “Enter a number:” before it.

良好的输入/输出设计包括清晰的提示和信息。例如,与其写 INPUT n,不如在前面写 OUTPUT “Enter a number:”

File input/output is also important for storing data permanently. A program can open a file, read from it, write to it, and close it.

文件输入/输出对于永久存储数据也很重要。程序可以打开文件、读取文件、写入文件并关闭文件。


11. Totalling and Counting | 求和与计数

Totalling and counting are common patterns in programming. A total accumulates the sum of values, while a counter counts the number of times an event occurs.

求和与计数是编程中常见的模式。总数累积数值的总和,而计数器计算事件发生的次数。

Total pattern:

求和模式:

total ← 0
FOR i ← 1 TO 10
  INPUT number
  total ← total + number
NEXT i
OUTPUT total

The variable total must be initialised to 0 before the loop starts.

变量 total 必须在循环开始前初始化为 0。

Counting pattern:

计数模式:

count ← 0
FOR i ← 1 TO 10
  INPUT number
  IF number > 100 THEN
    count ← count + 1
  ENDIF
NEXT i
OUTPUT count

This counts how many numbers greater than 100 were entered.

这统计输入了多少个大于 100 的数。

Counting is often used to calculate averages: average = total ÷ count. Remember that averages may be real numbers, even if total and count are integers.

计数常用于计算平均值:平均值 = 总数 ÷ 计数。请记住,即使总数和计数是整数,平均值也可能是实数。


12. Testing and Debugging | 测试与调试

Testing is the process of running a program with selected input values to check that it behaves correctly. Debugging is the process of finding and correcting errors, also called bugs.

测试是使用选定的输入值运行程序以检查其行为是否正确的过程。调试是查找并纠正错误(也称为 bug)的过程。

Three types of error:

三类错误:

Error Type | 错误类型 Description | 描述 Example | 示例
Syntax error Breaking the grammar rules of the language Missing ENDIF
语法错误 违反语言的语法规则 缺少 ENDIF
Runtime error An error that occurs while the program is running Dividing by zero
运行时错误 程序运行时发生的错误 除以零
Logic error The program runs but produces wrong results Using + instead of ×
逻辑错误 程序运行但产生错误结果 使用了 + 而不是 ×

Testing strategies include:

测试策略包括:

  • Normal data: typical valid values.
  • 正常数据:典型的有效值。
  • Boundary data: values at the limit of allowed ranges.
  • 边界数据:允许范围极限处的值。
  • Invalid data: values that should be rejected.
  • 无效数据:应该被拒绝的值。

Trace tables are used to manually record the values of variables while stepping through code. They are helpful for finding logic errors.

跟踪表用于在逐步执行代码时手动记录变量的值。它们有助于发现逻辑错误。


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

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

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version