📚 Programming Fundamentals | 编程基础考点精讲
Mastering the fundamentals of programming is the first step towards building reliable and efficient software. This article covers the core concepts required for the IB and AQA Computer Science syllabi, from data types and operators to subroutines and scope, providing a bilingual revision guide that consolidates both theoretical understanding and practical pseudocode examples.
掌握编程基础是构建可靠高效软件的第一步。本文涵盖 IB 和 AQA 计算机科学大纲所需的核心概念,从数据类型和运算符到子程序和作用域,提供一份双语复习指南,巩固理论理解并结合伪代码实例精讲。
1. Data Types and Variables | 数据类型与变量
Every piece of data in a program has a data type that determines what operations can be performed on it. Common primitive types include integer (whole numbers), real/float (numbers with a fractional part), Boolean (TRUE or FALSE), character (a single symbol) and string (a sequence of characters).
程序中的每一条数据都有一个数据类型,它决定了可以执行哪些操作。常见的原始类型包括整数(整数)、实数/浮点数(带小数部分的数字)、布尔型(真或假)、字符(单个符号)和字符串(字符序列)。
A variable is a named storage location whose value can change during execution. The declaration of a variable often includes its identifier and type, e.g. DECLARE age : INTEGER in AQA pseudocode. Assigning a value uses the left arrow: age ← 17.
变量是一个命名的存储位置,其值在程序执行期间可以改变。变量的声明通常包括标识符和类型,例如在 AQA 伪代码中 DECLARE age : INTEGER。赋值使用左箭头:age ← 17。
Understanding type compatibility is crucial: attempting to assign a string to an integer variable typically causes a type error. Strongly typed languages enforce strict type checks at compile time, while weakly typed languages allow implicit conversions.
理解类型兼容性至关重要:试图将字符串赋给整数变量通常会导致类型错误。强类型语言在编译时强制执行严格的类型检查,而弱类型语言允许隐式转换。
2. Constants and Literals | 常量与字面量
A constant is similar to a variable but its value is fixed at design time and cannot be altered during execution. In pseudocode, it is declared with the keyword CONSTANT: CONSTANT PI ← 3.14159. Using constants improves code readability and maintainability by avoiding magic numbers.
常量与变量相似,但其值在设计时固定,执行期间不可更改。在伪代码中,使用关键字 CONSTANT 声明:CONSTANT PI ← 3.14159。使用常量可避免魔术数字,从而提高代码的可读性和可维护性。
A literal is the actual value written in the source code, such as 100, 3.14, 'A' or "Hello". The type of a literal is implied by its format: numbers without a decimal point are integer literals; text in quotes forms string literals.
字面量是写入源代码的实际值,例如 100、3.14、'A' 或 "Hello"。字面量的类型由其格式隐式确定:不带小数点的数字是整数字面量;引号中的文本构成字符串字面量。
3. Operators: Arithmetic, Relational and Logical | 运算符:算术、关系与逻辑
Arithmetic operators perform mathematical calculations. The standard set includes + (addition), − (subtraction), * (multiplication), / (division), MOD (remainder) and DIV (integer division). For example, remainder ← 17 MOD 5 gives 2.
算术运算符执行数学计算。标准集合包括 +(加)、−(减)、*(乘)、/(除)、MOD(取余)和 DIV(整除)。例如 remainder ← 17 MOD 5 得到 2。
Relational operators compare two values and yield a Boolean result. The common operators are =, ≠, >, <, ≥ and ≤. The expression score > 80 evaluates to TRUE if score exceeds 80.
关系运算符比较两个值并产生布尔结果。常用运算符有 =、≠、>、<、≥ 和 ≤。如果分数超过 80,表达式 score > 80 的结果为真。
Logical operators combine Boolean expressions: AND, OR and NOT. They follow a truth table. In pseudocode, IF age > 17 AND hasLicense THEN uses logical AND to check both conditions.
逻辑运算符组合布尔表达式:AND、OR 和 NOT。它们遵循真值表。在伪代码中,IF age > 17 AND hasLicense THEN 使用逻辑与同时检查两个条件。
Operator precedence determines the evaluation order. Typically, parentheses are evaluated first, then arithmetic operators, then relational operators, and finally logical operators. Using parentheses explicitly avoids confusion.
运算符优先级决定了求值顺序。通常先计算括号,然后是算术运算符,接着是关系运算符,最后是逻辑运算符。明确使用括号可以避免混淆。
4. Input and Output | 输入与输出
Programs interact with users through input and output commands. In AQA pseudocode, OUTPUT displays data on the screen: OUTPUT "Enter your name: ". INPUT reads data from the keyboard into a variable: INPUT name.
程序通过输入和输出命令与用户交互。在 AQA 伪代码中,OUTPUT 在屏幕上显示数据:OUTPUT "Enter your name: "。INPUT 从键盘读取数据存入变量:INPUT name。
When handling input, the programmer must consider type conversion. For instance, reading a number as a string and then converting it to an integer using STRING_TO_INT or similar functions guarantees correct type handling.
处理输入时,程序员必须考虑类型转换。例如,先将数字作为字符串读入,然后使用 STRING_TO_INT 或类似函数将其转换为整数,可保证类型处理正确。
Output formatting is also important. Concatenating strings and variables can produce meaningful messages: OUTPUT "Age: " + strAge.
输出格式也很重要。将字符串和变量拼接可以生成有意义的消息:OUTPUT "Age: " + strAge。
5. Sequence and Assignment | 顺序结构与赋值
Sequence is the simplest control flow where instructions are executed one after another, in the order they appear. Assignment statements store the result of an expression into a variable using the left arrow.
顺序结构是最简单的控制流,指令按其出现的顺序一条一条地执行。赋值语句使用左箭头将表达式的结果存入变量。
total ← price × quantity
The right-hand side is evaluated first, and then the result is placed into the left-hand variable. Multiple assignments can be combined: x ← x + 1 increments the value by one.
先计算右侧表达式,然后将结果放入左侧变量。多条赋值可以组合使用:x ← x + 1 将值增加一。
Sequential execution is the foundation upon which selection and iteration are built. Even complex algorithms can be broken down into a sequence of simple steps.
顺序执行是选择和迭代构建的基础。即使是复杂的算法也可以分解为一系列简单的步骤序列。
6. Selection: IF Statements | 选择结构:IF 语句
Selection allows a program to choose different paths based on a condition. The simplest form is IF condition THEN ... ENDIF. When the condition is TRUE, the indented block runs; otherwise it is skipped.
选择结构允许程序根据条件选择不同的执行路径。最简单的形式是 IF condition THEN ... ENDIF。当条件为真时,执行缩进的代码块;否则跳过。
A two-way branch uses IF ... THEN ... ELSE ... ENDIF. For multiple conditions, ELSE IF can chain branches. The first TRUE condition triggers its block; if none are TRUE, the optional ELSE block executes.
双分支使用 IF ... THEN ... ELSE ... ENDIF。对于多个条件,ELSE IF 可以链接多个分支。第一个为真的条件触发其代码块;如果都不为真,则执行可选的 ELSE 块。
Nested IF statements place one selection inside another. While powerful, deep nesting can reduce readability. Logical operators often simplify complex conditions.
嵌套 IF 语句将一个选择结构放在另一个内部。虽然功能强大,但深度嵌套会降低可读性。逻辑运算符通常可以简化复杂的条件。
7. Iteration: FOR and WHILE Loops | 迭代结构:FOR 与 WHILE 循环
Iteration repeats a block of code. The FOR loop is count-controlled and runs a predetermined number of times. In pseudocode: FOR i ← 1 TO 10 ... NEXT i. The loop variable i takes values from 1 to 10 inclusive.
迭代重复执行一段代码。FOR 循环是计数控制循环,运行预定的次数。在伪代码中:FOR i ← 1 TO 10 ... NEXT i。循环变量 i 依次取值从 1 到 10(包含)。
The WHILE loop is condition-controlled and repeats as long as the condition remains TRUE: WHILE temp > 0 ... ENDWHILE. If the condition is FALSE initially, the loop body never executes.
WHILE 循环是条件控制循环,只要条件为真就重复执行:WHILE temp > 0 ... ENDWHILE。如果初始条件为假,循环体一次也不执行。
The REPEAT ... UNTIL loop is a post-condition loop that always executes at least once because the test is at the end. Each type of loop can be transformed into another, but choosing the right one improves clarity.
REPEAT ... UNTIL 循环是一种后测循环,因为测试在末尾,所以至少执行一次。每种循环都可以相互转换,但选择正确的类型可以提高清晰度。
8. Subroutines: Procedures and Functions | 子程序:过程与函数
Subroutines are self-contained blocks of code that perform a specific task. A procedure carries out a task but does not return a value; a function performs a task and returns a single value. Both promote modularity and code reuse.
子程序是执行特定任务的独立代码块。过程执行任务但不返回值;函数执行任务并返回一个值。两者都促进模块化和代码复用。
In AQA pseudocode, a function is defined with FUNCTION funcName(parameters) RETURNS type ... RETURN ... ENDFUNCTION. A procedure uses PROCEDURE procName(parameters) ... ENDPROCEDURE. Calls are made by using the name followed by arguments in parentheses.
在 AQA 伪代码中,函数用 FUNCTION funcName(parameters) RETURNS type ... RETURN ... ENDFUNCTION 定义。过程用 PROCEDURE procName(parameters) ... ENDPROCEDURE 定义。调用时使用名称并在括号中传入参数。
Using subroutines makes programs easier to debug and test. Each subroutine can be developed and verified independently, then integrated into the larger solution.
使用子程序使程序更容易调试和测试。每个子程序可独立开发和验证,然后集成到更大的解决方案中。
9. Parameter Passing: By Value and By Reference | 参数传递:按值与按引用
Parameters allow data to be passed into subroutines. Passing by value provides the subroutine with a copy of the argument; changes inside the subroutine do not affect the original variable. This is the default in many languages and protects the caller’s data.
参数允许数据传入子程序。按值传递为子程序提供实参的副本;子程序内部的修改不会影响原始变量。这是许多语言中的默认方式,保护了调用者的数据。
Passing by reference gives the subroutine direct access to the original variable. Any modifications are reflected outside the subroutine. This is useful when a subroutine needs to return multiple results. In pseudocode, the parameter is marked with BYREF.
按引用传递使子程序可直接访问原始变量。任何修改都会反映在子程序外部。当子程序需要返回多个结果时,这种方式很有用。在伪代码中,参数用 BYREF 标记。
| Aspect 方面 | By Value 按值 | By Reference 按引用 |
|---|---|---|
| Copy made? 是否复制 | Yes | No (alias to original) |
| Side effects? 副作用 | No | Yes, can alter caller’s variable |
| Memory usage 内存使用 | Higher for large data | Lower, only address passed |
10. Scope: Local and Global Variables | 作用域:局部与全局变量
Scope refers to the region of a program where a variable is accessible. Local variables are declared inside a subroutine and exist only during its execution. They are invisible to the rest of the program, preventing accidental modification and reducing side effects.
作用域指的是程序中变量可访问的区域。局部变量在子程序内部声明,仅在其执行期间存在。它们对程序的其余部分不可见,可防止意外修改并减少副作用。
Global variables are declared outside all subroutines and can be accessed from anywhere in the program. While convenient, overusing global variables makes code harder to debug and maintain, because any part of the program can change them.
全局变量在所有子程序外部声明,可在程序中的任何位置访问。虽然方便,但过度使用全局变量会使代码更难以调试和维护,因为程序的任何部分都可能更改它们。
Good practice limits global variables to constants and essential shared data, relying on parameter passing for communication between subroutines. This encapsulates behaviour and makes programs more predictable.
良好的实践将全局变量限制为常量和必要的共享数据,依靠参数传递实现子程序间的通信。这封装了行为,使程序更可预测。
11. Debugging, Testing and Comments | 调试、测试与注释
Debugging is the process of finding and fixing errors (bugs) in code. Common techniques include dry running (tracing through the code by hand), using breakpoints, and inserting temporary output statements to inspect variable values at runtime.
调试是查找和修复代码中错误的过程。常用技术包括干运行(手动跟踪代码)、使用断点以及插入临时输出语句在运行时检查变量值。
Testing verifies that a program meets its requirements. Test plans should include normal data, boundary data (extremes of valid ranges) and erroneous data. Iterative testing after each change ensures stability.
测试验证程序是否满足需求。测试计划应包括正常数据、边界数据(有效范围的极值)和错误数据。每次更改后进行迭代测试可确保稳定性。
Comments are non-executable lines that explain what the code does. They start with // or are enclosed in /* */ in many languages. Well-placed comments clarify intent, but excessive commenting of obvious code should be avoided. Self-documenting code with meaningful identifier names is preferred.
注释是不被执行的说明性文字。在许多语言中以 // 开头或用 /* */ 包围。适当的注释可阐明意图,但应避免对显而易见的代码进行过度注释。更推荐使用有意义的标识符名称编写自文档化代码。
12. Introduction to Programming Paradigms | 编程范式简介
Different programming paradigms offer distinct ways to structure code. The imperative paradigm, which includes procedural and object-oriented programming, focuses on describing step-by-step how to achieve a result. The declarative paradigm, such as functional or logic programming, emphasises what result is desired without specifying the control flow explicitly.
不同的编程范式提供了不同的代码组织方式。命令式范式(包括过程式和面向对象编程)侧重于逐步描述如何实现结果。声明式范式(如函数式或逻辑编程)强调预期的结果,而不显式指定控制流。
Understanding paradigms helps in selecting the most suitable language for a problem. Most AQA exam questions use an imperative pseudocode, but awareness of other paradigms broadens problem-solving skills.
理解范式有助于为问题选择最合适的语言。大多数 AQA 考题使用命令式伪代码,但对其他范式的认知能够拓宽问题解决能力。
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导