📚 Programming Fundamentals Key Concepts | 编程基础 考点精讲
Mastering programming fundamentals is the cornerstone of success in both IB and OCR Computer Science. This revision guide breaks down every essential concept – from variables and data types to file handling and algorithms – with clear bilingual explanations, ready for your exam preparation. Let’s build a solid base for your coding skills.
掌握编程基础是 IB 和 OCR 计算机科学成功的基石。这份复习指南将每个核心概念——从变量、数据类型到文件处理和算法——都用清晰的双语解释一一拆解,为你的备考做好充分准备。让我们为你的编程技能打下坚实基础。
1. Variables and Data Types | 变量与数据类型
A variable is a named storage location in memory that holds a value which can change during program execution. Every variable must be declared with a data type that determines the kind of data it can store and the operations permitted on it. Common primitive data types include integer, float (real), character, string, and Boolean.
变量是内存中的一个命名存储位置,用于保存可在程序执行期间更改的值。每个变量必须用数据类型声明,该类型决定了变量可存储的数据种类以及允许在其上执行的操作。常见的基本数据类型包括整型、浮点型(实数)、字符、字符串和布尔型。
In strongly typed languages like Java or C#, you must explicitly state the type. In Python, variables are dynamically typed – the type is inferred at runtime. IB and OCR syllabi expect you to recognise both approaches and understand type conversion (casting), such as converting a string ‘123’ to an integer.
在 Java 或 C# 等强类型语言中,你必须显式声明类型。在 Python 中,变量是动态类型的——类型在运行时推断。IB 和 OCR 大纲要求你认识这两种方式,并理解类型转换(强制转换),例如将字符串 ‘123’ 转换为整数。
Memory allocation differs: integers typically occupy 4 bytes, floats 4 or 8 bytes, and characters 1 or 2 bytes depending on encoding. Overflow occurs when a value exceeds the allocated space, an important concept for exam calculations.
内存分配有所不同:整数通常占用 4 字节,浮点数 4 或 8 字节,字符 1 或 2 字节,具体取决于编码。当值超出分配的空间时会发生溢出,这是考试计算中的一个重要概念。
2. Operators and Expressions | 运算符与表达式
Operators perform operations on operands. Arithmetic operators (+, -, *, /, MOD, DIV) handle mathematical calculations. The modulus operator (MOD) returns the remainder of a division, while DIV gives the integer quotient. In many languages, / performs floating-point division and // is used for integer division.
运算符对操作数执行操作。算术运算符(+、-、*、/、MOD、DIV)处理数学计算。取模运算符(MOD)返回除法的余数,而 DIV 给出整数商。在许多语言中,/ 执行浮点除法,// 用于整数除法。
Relational operators (==, !=, >, <, >=, <=) compare values and yield Boolean results. Logical operators (AND, OR, NOT) combine Boolean expressions following truth tables. Precedence rules determine the order of evaluation: parentheses first, then arithmetic, relational, and finally logical operators.
关系运算符(==、!=、>、<、>=、<=)比较值并产生布尔结果。逻辑运算符(AND、OR、NOT)根据真值表组合布尔表达式。优先级规则决定了求值顺序:首先是括号,然后是算术运算符,接着是关系运算符,最后是逻辑运算符。
Be careful with string concatenation using +; some languages also support shorthand assignment operators like +=, -=, *=, which make code more concise and are common in exam pseudocode.
注意使用 + 进行字符串连接;有些语言还支持简写赋值运算符,如 +=、-=、*=,它们使代码更简洁,在考试伪代码中很常见。
3. Input and Output | 输入与输出
Programs interact with users through input and output statements. Input reads data from the keyboard or a file; output displays information on the screen or writes to a file. In pseudocode, INPUT and OUTPUT (or PRINT) are standard commands. In Python, input() reads a string, and print() outputs to the console.
程序通过输入和输出语句与用户交互。输入从键盘或文件读取数据;输出在屏幕上显示信息或写入文件。在伪代码中,INPUT 和 OUTPUT(或 PRINT)是标准命令。在 Python 中,input() 读取字符串,print() 输出到控制台。
When reading numeric input, you often need to cast the string to an integer or float. Validating user input – checking it is within expected range or format – is a key skill tested in both IB and OCR exam scenario questions. Always consider prompting the user with clear messages.
读取数字输入时,你通常需要将字符串强制转换为整数或浮点数。验证用户输入——检查其是否在预期范围或格式内——是 IB 和 OCR 考试场景题中测试的关键技能。始终考虑用清晰的消息提示用户。
4. Conditional Statements | 条件语句
Conditional statements control the flow of execution based on Boolean conditions. The if-else structure allows branching: if the condition is true, one block executes; otherwise, an alternative block runs. Nested if statements handle multiple levels of decision-making.
条件语句根据布尔条件控制执行流程。if-else 结构允许分支:如果条件为真,执行一个代码块;否则,执行另一个代码块。嵌套 if 语句处理多级决策。
The switch-case (or select-case) statement is a multi-way branch that tests a variable against several constant values. It is more readable than long if-else chains when many discrete values are involved. IB exams often expect you to choose the most appropriate conditional structure for a given problem.
switch-case(或 select-case)语句是一种多路分支,它将变量与多个常量值进行测试。当涉及许多离散值时,它比长的 if-else 链更具可读性。IB 考试通常期望你为给定问题选择最合适的条件结构。
Boolean flags and complex logical expressions combining AND, OR, NOT are frequently used in guard conditions. Remember short-circuit evaluation: in (A AND B), if A is false, B is not evaluated; in (A OR B), if A is true, B is skipped. This can prevent runtime errors.
布尔标志以及结合 AND、OR、NOT 的复杂逻辑表达式常用于守护条件。记住短路求值:在 (A AND B) 中,如果 A 为假,则不计算 B;在 (A OR B) 中,如果 A 为真,则跳过 B。这可以防止运行时错误。
5. Iteration: Loops | 迭代:循环
Loops repeat a block of code while a condition holds. The while loop checks the condition before each iteration; if it is false initially, the loop body may never execute. The repeat-until (do-while) loop executes at least once because the condition is tested at the end.
循环在条件成立时重复执行一段代码。while 循环在每次迭代前检查条件;如果初始条件为假,循环体可能一次都不执行。repeat-until (do-while) 循环至少执行一次,因为条件在末尾测试。
The for loop is count-controlled – it iterates a fixed number of times based on a counter variable. In pseudocode: FOR i ← 1 TO 10. Python uses for i in range(1,11). Understanding loop control variables, step values, and nested loops is essential for tackling array traversal and matrix problems.
for 循环是计数控制的——它根据计数器变量迭代固定次数。在伪代码中:FOR i ← 1 TO 10。Python 使用 for i in range(1,11)。理解循环控制变量、步长值和嵌套循环对于处理数组遍历和矩阵问题至关重要。
Infinite loops occur when the termination condition is never met; this is a common logic error. You can break out of a loop early using BREAK, or skip to the next iteration with CONTINUE. Exam questions often ask you to trace loop execution for a given input.
当终止条件永远不满足时,会发生无限循环;这是一个常见的逻辑错误。你可以使用 BREAK 提前跳出循环,或使用 CONTINUE 跳到下一次迭代。考试问题经常要求你针对给定输入追踪循环的执行过程。
6. Arrays and Lists | 数组与列表
An array is a data structure that stores a fixed number of elements of the same type, accessed by an index. In many languages, indices start at 0. A one-dimensional array is like a list; a two-dimensional array resembles a table with rows and columns, used to represent grids or matrices.
数组是一种数据结构,存储固定数量的相同类型元素,通过索引访问。在许多语言中,索引从 0 开始。一维数组就像列表;二维数组类似于带有行和列的表格,用于表示网格或矩阵。
Dynamic arrays (or lists in Python) can grow and shrink. Common operations include traversing, inserting, deleting, and searching. You must be able to write algorithms to find the maximum/minimum, calculate an average, or reverse the elements. Both IB and OCR require fluency in array manipulation pseudocode.
动态数组(或 Python 中的列表)可以增长和收缩。常见操作包括遍历、插入、删除和搜索。你必须能够编写算法来查找最大值/最小值、计算平均值或反转元素。IB 和 OCR 都要求熟练掌握数组操作的伪代码。
| Operation | Pseudocode Example |
|---|---|
| Access element | myArray[2] |
| Assign value | myArray[0] ← 99 |
| Loop through | FOR i ← 0 TO LEN(myArray)-1 |
| 2D array access | grid[row][col] |
上表总结了常见的数组操作及其伪代码表示。考试中常要求你分析使用数组的代码片段或补全缺失部分。
7. String Handling | 字符串处理
Strings are sequences of characters. Basic operations include concatenation (joining two strings), extraction of substrings, determining length, and character position search. Common functions: LEFT, RIGHT, MID, LENGTH, POSITION, and converting case (UPPER, LOWER).
字符串是字符序列。基本操作包括连接(合并两个字符串)、提取子串、确定长度以及查找字符位置。常用函数:LEFT、RIGHT、MID、LENGTH、POSITION 以及转换大小写(UPPER、LOWER)。
String comparison can be lexicographical based on character codes. ASCII and Unicode are the standard encoding systems. In exams, you may be asked to construct algorithms that count vowels, check palindromes, or parse data from a structured string like a CSV line.
字符串比较可以基于字符编码按字典顺序进行。ASCII 和 Unicode 是标准编码系统。在考试中,你可能被要求构建算法来统计元音字母、检查回文或从结构化字符串(如 CSV 行)中解析数据。
Immutability: in languages like Python and Java, strings are immutable – any modification creates a new string. This affects efficiency when building large strings in loops; using a StringBuilder (Java) or join() (Python) is preferred.
不可变性:在 Python 和 Java 等语言中,字符串是不可变的——任何修改都会创建一个新字符串。这会影响在循环中构建大型字符串的效率;最好使用 StringBuilder(Java)或 join()(Python)。
8. Functions and Procedures | 函数与过程
A function is a named block of code that performs a specific task and returns a value. A procedure (or void method) performs a task but does not return a value. Both help in modularising code, promoting reusability and readability.
函数是一个命名的代码块,执行特定任务并返回一个值。过程(或 void 方法)执行任务但不返回值。两者都有助于代码模块化,提高可重用性和可读性。
Parameters (arguments) pass data into routines. There are two main passing mechanisms: by value (a copy is used, original unchanged) and by reference (the memory address is passed, so changes affect the original). IB and OCR both test your understanding of these mechanisms and their implications.
参数(实参)将数据传入例程。有两种主要的传递机制:按值传递(使用副本,原始值不变)和按引用传递(传递内存地址,因此更改会影响原始值)。IB 和 OCR 都会测试你对这些机制及其影响的理解。
Scope: variables declared inside a function are local; those outside are global. Using global variables excessively can lead to side effects and make debugging harder. Recursion – a function calling itself – requires a base case to terminate; it is a powerful tool for problems like factorial or Fibonacci sequence.
作用域:函数内部声明的变量是局部的;外部的变量是全局的。过度使用全局变量可能导致副作用并使调试更困难。递归——函数调用自身——需要一个基本条件来终止;它是解决阶乘或斐波那契数列等问题的有力工具。
9. File Handling | 文件处理
Persistent storage is achieved through files. Basic operations: open a file (with a mode: read, write, append), read data, write data, and close. Pseudocode often uses OPENFILE, READLINE, WRITELINE, CLOSEFILE. Exception handling for file not found or end-of-file is crucial.
持久存储通过文件实现。基本操作:打开文件(带有模式:读取、写入、追加)、读取数据、写入数据和关闭。伪代码通常使用 OPENFILE、READLINE、WRITELINE、CLOSEFILE。针对文件未找到或文件结束的异常处理至关重要。
Sequential files are read from start to end; direct-access (random) files allow jumping to any record using a record key or byte offset. Text files store human-readable characters; binary files store data in machine-readable form. You must know how to process a file line by line until EOF (end of file).
顺序文件从头到尾读取;直接访问(随机)文件允许使用记录键或字节偏移量跳转到任何记录。文本文件存储人类可读的字符;二进制文件以机器可读形式存储数据。你必须知道如何逐行处理文件直到 EOF(文件结束)。
In Python, the with open(…) as f: construct ensures the file is properly closed even if an error occurs. Always consider file paths and permissions as part of error scenarios in exam questions.
在 Python 中,with open(…) as f: 结构确保即使发生错误,文件也能正确关闭。在考试问题中,始终将文件路径和权限视为错误场景的一部分。
10. Basic Algorithms: Searching and Sorting | 基本算法:搜索与排序
Linear search scans each element sequentially until the target is found or the list ends. It works on unsorted data and has O(n) time complexity. Binary search requires a sorted array and repeatedly divides the search interval in half; its O(log n) efficiency makes it much faster for large datasets.
线性搜索按顺序扫描每个元素,直到找到目标或列表结束。它适用于未排序的数据,时间复杂度为 O(n)。二分搜索需要一个已排序的数组,并反复将搜索区间一分为二;其 O(log n) 的效率使其在大数据集上快得多。
Sorting algorithms: Bubble sort repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. It is simple but O(n²) inefficient. Insertion sort builds the sorted list one element at a time, efficient for small or nearly sorted data. Merge sort divides the list into halves, sorts them recursively, and merges – O(n log n) and stable.
排序算法:冒泡排序重复遍历列表,比较相邻元素,如果顺序错误则交换它们。它简单但效率为 O(n²)。插入排序一次构建一个元素的已排序列表,对于小型或近乎排序的数据很高效。归并排序将列表分成两半,递归排序后合并——O(n log n) 且稳定。
You must be able to trace these algorithms step by step, write pseudocode, and compare their performance. Complexity analysis using Big O notation is a key component of both IB HL and OCR A-Level papers.
你必须能够逐步追踪这些算法,编写伪代码,并比较它们的性能。使用大 O 表示法的复杂度分析是 IB HL 和 OCR A-Level 试卷的关键组成部分。
11. Debugging and Testing | 调试与测试
Errors are inevitable. Syntax errors violate language rules and prevent compilation/interpretation. Logic errors produce incorrect results; runtime errors occur during execution (division by zero, file not found). Debugging techniques include dry-running (tracing with a table), breakpoints, and print statements.
错误是不可避免的。语法错误违反语言规则,阻止编译/解释。逻辑错误产生错误结果;运行时错误在执行期间发生(除以零、文件未找到)。调试技术包括干运行(用跟踪表)、断点和打印语句。
Testing verifies correctness. Normal data, boundary data (e.g., minimum/maximum allowed values), and erroneous data should all be tested. Black-box testing focuses on inputs and outputs without knowing internal structure; white-box testing examines internal logic and paths.
测试验证正确性。应测试正常数据、边界数据(例如,最小/最大允许值)和错误数据。黑盒测试侧重于输入和输出,无需了解内部结构;白盒测试检查内部逻辑和路径。
A trace table is an essential tool for exam questions: columns for each variable and condition, updating values as you step through code. It helps locate where a program deviates from expected behaviour.
跟踪表是考试问题的重要工具:为每个变量和条件设置列,随着你逐步执行代码更新值。它有助于定位程序偏离预期行为的位置。
12. Programming Paradigms Overview | 编程范式概述
IB and OCR both expect awareness of major programming paradigms. Procedural programming organises code as a sequence of instructions operating on data, using functions and procedures. Object-oriented programming (OOP) bundles data and methods into objects, promoting encapsulation, inheritance, and polymorphism.
IB 和 OCR 都期望你了解主要的编程范式。过程式编程将代码组织为对数据进行操作的一系列指令,使用函数和过程。面向对象编程(OOP)将数据和方法捆绑成对象,促进封装、继承和多态。
Declarative languages (like SQL or functional languages) express what to compute rather than how. Logic programming (e.g., Prolog) uses facts and rules. Understanding the differences helps in choosing the right tool for a problem and appears in higher-level syllabus topics.
声明式语言(如 SQL 或函数式语言)表达要计算什么,而不是如何计算。逻辑编程(例如 Prolog)使用事实和规则。理解这些差异有助于为问题选择正确的工具,并出现在更高层次的大纲主题中。
Modern languages often support multiple paradigms. For exams, focus on recognising the characteristics of each paradigm and giving examples of languages that implement them.
现代语言通常支持多种范式。对于考试,重点在于识别每种范式的特征,并给出实现它们的语言示例。
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课程辅导,国外大学本科硕士研究生博士课程论文辅导