OCR GCSE计算机 变量 选择 循环 子程序
OCR 的 GCSE 计算机科学(J277)把一半的分数押在编程上:J277/02 Computational thinking, algorithms and programming 是一张 1 小时 30 分钟、80 分的笔试,占最终成绩的 50%,其中 Section B 要求学生用 OCR 的 Exam Reference Language 或者自己学过的高级语言写出、补全、改写算法。这份考纲 2020 年 9 月首次教学,2022 年夏季首次考评,2024 年 3 月又出过一次修订版,适用于 2024 年 6 月起的考季。很多学生丢分的地方不是难题,而是变量、循环、子程序这些基础构件写得含糊:变量名随手起,循环次数差一,函数忘了 return。下面按 J277/02 的考点顺序,把编程基础里扣分最密集的八个部分拆开,每个部分给出可以直接套用的写法和真实考场上出现的错误。
OCR's GCSE Computer Science (J277) puts half of the qualification into programming: Component 02, Computational thinking, algorithms and programming, is a 90-minute written paper worth 80 marks, and Section B asks candidates to write, complete or refine algorithms in the OCR Exam Reference Language or in a high-level language they have learned. The specification was first taught in September 2020, first examined in summer 2022, and last amended in March 2024 for the June 2024 exam series onwards. Marks are lost far less often on clever algorithms than on the basics, where variable names are vague, loops run one time too many and functions forget to return anything. The eight areas below follow the specification order, so you can check them against your own code one by one.
变量、常量与数据类型
变量在第一次赋值时就完成了声明,数据类型由赋给它的值决定:score = 0 是整数,price = 12.5 是实数,name = "Aisha" 是字符串,finished = False 是布尔值,单个字符可以单独作为一种类型看待。常量在程序运行期间不改变,OCR 的参考语言里写成 global MAX_MARK = 80,命名习惯上全部用大写,读代码的人一眼就知道这个值不该被改。类型的选择直接影响运算结果,整数和实数相除得到实数,整数和字符串相加则会直接报错。常见的错误是忘记 input() 返回的是字符串:写了 age = input("Age: ") 之后再写 age + 1,程序立刻崩掉,正确写法是 age = int(input("Age: "))。类型转换靠 int、str、float 三个函数完成,int("3") 得到 3,str(3) 得到 "3",float("3.14") 得到 3.14。考试问数据类型时,答案要落到具体的值上,判断一个变量存的是整数还是实数,看的是赋进去的那个数有没有小数点。
In the OCR reference language a variable is declared the moment a value is assigned to it, and that value fixes the data type: integer, real, Boolean, character or string; constants hold values that must not change while the program runs, and writing them in capitals is a convention that tells anyone reading the code that the value is fixed. Type errors are expensive because they turn a working algorithm into one that crashes, and the usual culprit is input, which always hands back a string even when the user types digits. Wrap it in int() or float() before doing any arithmetic, and reach for str() when a number has to be printed alongside text. When a question asks about data types, point at the actual value being stored rather than naming the type in the abstract.
算术与比较运算符 MOD DIV
算术运算符除了加减乘除,还有三个被 OCR 明确列进考纲的:MOD 取余、DIV 取整商、^ 求幂。12 MOD 5 得到 2,因为 12 除以 5 余 2;17 DIV 5 得到 3,因为商的小数部分被舍掉;3^4 得到 81。这三个运算符在判断奇偶和分组这一类题目里几乎每套卷子都会出现,判断偶数写 number MOD 2 == 0,把 100 条数据分成每组 7 条就用到 DIV 和 MOD 配合。比较运算符一共六个:== 等于、!= 不等于、< 小于、<= 小于或等于、> 大于、>= 大于或等于,条件里最常写错的是把赋值号当判断号,x = 5 是存值,x == 5 才是比较。很多学生以为把 OCR 写法里的 3^4 搬进 Python 也是 81,其实 Python 的 ^ 是按位异或,乘方要写成 3**4,翻译伪代码时这一条经常被漏掉。
Three operators exist in the reference language that catch candidates out: MOD returns the remainder, DIV returns the whole-number quotient, and the caret raises a value to a power, so 12 MOD 5 is 2, 17 DIV 5 is 3 and 3^4 is 81; dividing 100 records into groups of seven leans on both MOD and DIV; comparisons use exactly six operators, == for equal, != for not equal, and the four orderings, and the single most common slip is a lone equals sign, which stores a value instead of comparing two. Translating pseudocode into Python takes one deliberate change, because the caret is a bitwise operator there and a power has to be written with two asterisks.
选择结构 if elseif else 的走向
选择结构用 if、elseif、else、endif 四个关键字表达,OCR 的参考语言还提供 switch 和 case 处理多分支判断。分支的顺序比语法更容易失分,如果先判断 marks >= 70 再判断 marks >= 80,那么 85 分会套进第一个分支,第二个分支永远走不到,正确的顺序是从最严格的条件开始往下写。布尔运算符只有 AND、OR、NOT 三个,AND 两边都为真结果才为真,OR 只要一边为真结果就为真,NOT 把真假翻转,把 NOT 和括号放在一起考是常见的出题方式。条件多了以后嵌套的 if 会变得难读,把整个条件算成一个布尔变量再判断,代码短而且不容易写错。写这类题时把每个分支在草稿纸上标出它负责的分数段,交卷前再扫一眼有没有分数段被跨过。
Selection is written with if, elseif, else and endif, and the reference language also offers switch and case when a value has several possible outcomes; the switch form is often the tidier answer for a menu; order matters more than syntax here, because a chain that tests a lower threshold first will swallow every higher mark, so conditions run from strictest to loosest. Boolean logic is limited to AND, OR and NOT, where AND needs both sides true, OR needs only one, and NOT flips the result; examiners like to wrap them in brackets so the gate has to be reasoned out rather than guessed. When a decision needs several conditions at once, storing the outcome in a Boolean variable keeps the code readable and makes the branch easier to test.
循环 for next 与 while endwhile
循环分计数控制和条件控制两类,OCR 的写法分别是 for i = 0 to 7 配 next i,以及 while 配 endwhile,另外还有 do ... until,它的循环体至少执行一次,因为条件放在最后判断。for i = 0 to 7 会打印八次,两端都包含在内,循环次数的公式是终止值减起始值再加一,这个细节每年都有人算错。while 循环里如果忘了改循环变量,程序会一直转下去,考试时用一张小表格把每轮变量的值记下来最保险,这也正是 trace table 题的标准解法。累加是最基础的练习,total = 0 之后在循环里写 total = total + count,循环结束再输出 total;要是把 total = 0 写进循环体里,结果永远只剩最后一个数。嵌套循环做二维数组遍历时,外层次数乘以里层次数才是总循环次数,写之前先算一遍能省下大量检查时间。
Loops come in two flavours in the specification, count-controlled and condition-controlled, written as for i = 0 to 7 with a matching next i, or with while and endwhile, and there is also a do until form whose body always runs at least once because its condition is tested last; A counted loop includes both end values, so the number of iterations is the end value minus the start value plus one, which is where the annual off-by-one marks disappear. A while loop keeps going as long as its condition holds, so the variable inside it has to change or the program hangs, and tracing one row per iteration on paper is how the planted fault is found; accumulating a total inside a loop, with the counter reset outside it, is the standard pattern worth practising until it is automatic.
子程序 function procedure 与作用域
子程序分成两种,function 有返回值,procedure 只执行动作不返回值,OCR 的写法是 function triple(number) 配 return number*3 配 endfunction,以及 procedure greeting(name) 配 endprocedure。参数默认按值传递,子程序拿到的是一份副本,改它不影响主程序里的原变量,只有明确标出 byRef 时才会改到调用方的变量,这一条在补全代码题里经常作为考点出现。作用域是另一个高频考点,在 function 或 procedure 内部声明的变量是局部变量,随子程序结束而消失,程序主体里的变量只有加了 global 才是全局变量。阅卷时经常看到学生把一整段代码裹进函数却不写参数,靠全局变量传递数据,这种答案很难拿到结构化编程的分。判断该用函数还是过程,看问题有没有要求返回一个结果:算出平均分要用 function,打印一张表用 procedure。
Sub-programs split into functions, which return a value, and procedures, which carry out an action and return nothing, and each ends with the matching endfunction or endprocedure keyword; parameters are passed by value by default, so the sub-program works on a copy and a change inside it leaves the caller's variable untouched unless byRef is stated. Scope is examined just as often: a variable created inside a sub-program is local and disappears when it finishes, while a main-program variable needs the global keyword before a sub-program can see it. Writing a sub-program that reaches for global variables instead of taking parameters is a design that earns few marks however well it works, and the choice between function and procedure usually comes down to whether a result has to travel back to the caller.
字符串 length substring 与数组下标
字符串依赖两个属性和方法,someText.length 返回字符个数,someText.substring(startingPosition, numberOfCharacters) 从指定位置取出指定数量的字符,两者都从 0 开始数。以 someText = "Computer Science" 为例,length 是 16,substring(3, 3) 得到 "put",因为下标 3 对应第四个字符。数组同样从 0 开始,array names[5] 声明的数组有 5 个元素,下标范围是 0 到 4,二维数组写成 array board[8,8],访问时用 board[0,0] 这样的两个下标。学生最常把下标和序号混在一起,从 1 开始数结果整体偏移一位;越界访问会直接触发运行错误,或者读到一段毫无意义的数据。写代码之前把数组的长度和最后一个合法下标写在注释里,能省掉一轮调试。
Strings carry two tools worth memorising, length for the number of characters and substring for a slice, which takes a starting position and a character count, with the first character sitting at index 0; for the string Computer Science the length is 16 and substring(3, 3) returns put, so counting from 1 costs the mark. Arrays are declared with the array keyword and are zero-indexed as well, which means array names[5] creates five elements holding indexes 0 to 4, and a two-dimensional array such as array board[8,8] is addressed with two indexes, for example board[0,0]. Reading past the final index throws a runtime error instead of quietly returning nonsense, so write the last legal index down before coding.
输入校验 测试数据与两类错误
J277 的 2.3 把能运行的代码和可信的代码分开考,防错设计包括预判误用、身份验证和输入校验,输入校验的常见形式有范围检查、类型检查、长度检查、存在性检查和格式检查,白名单和黑名单是两种常用实现。测试分成迭代测试和最终测试,前者在开发过程中反复执行,后者在程序完成后整体跑一遍。错误只需分清两类:语法错误让程序根本无法翻译和运行,比如括号缺失或关键字拼错;逻辑错误程序照常运行但结果不对,比如该用减号的位置写了加号,这类错误只能靠测试数据暴露出来。挑测试数据时把取值分成正常数据、边界数据、无效数据和错误数据四类,边界值要覆盖允许范围的两端以及刚刚超出的值,成绩允许 0 到 100 就测 0、100、-1 和 101。答这类题时把每个值的用途写出来,只写一个 101 拿不到分,写成 101 超出上界的无效数据才有分。
Component 2.3 separates code that runs from code that can be trusted; defensive design covers anticipating misuse, authentication and input validation, and validation wears recognisable shapes such as range checks, type checks, length checks, presence checks and format checks, usually implemented with a whitelist of accepted values or a blacklist of rejected ones; testing is either iterative, done while the program is being built, or final, done once the whole program works, and the data chosen should cover normal values, boundary values, invalid data and erroneous data. Boundary values sit at both ends of the accepted range and just outside it, so a mark that must fall between 0 and 100 is tested with 0, 100, -1 and 101. Of the two error types, a syntax error stops the program translating at all, while a logic error lets it run and quietly produce the wrong output; writing the purpose of each test value beside it is what turns a list into a mark.
Section B 算法题的限时练习顺序
复习可以从默写开始,把六个构件的标准写法各默一遍,再拿近五年的 J277/02 真题把 Section B 的算法题限时做一遍,做完用 trace table 逐轮核对变量值。每个知识点都可以写成一个二十行以内的完整程序,跑到能输出正确结果为止,手写伪代码和真正跑通的代码之间的差距比想象中大,尤其是字符串取子串和数组下标这两处。定稿前留三分钟检查括号配对、循环边界和函数返回值,这三处是补全题最常设的坑。需要针对性的编程与算法辅导,可以联系我们。
The revision order that works is to write the standard form of each construct from memory first, then work through Section B style algorithm questions against the clock and verify every iteration with a trace table; turning each topic into a short program that actually runs closes the gap between pseudocode on paper and working code, particularly for string slicing and array indexes. Saving three minutes at the end to check bracket pairs, loop boundaries and return statements catches most of the faults these questions plant.
戴睿(dere)Neo优选系列 创意双触摸屏高性能笔记本电脑大学生AI学习商务办公设计炒股娱乐手提轻薄本32/1TB¥4586.0📱 去京东购买京东精选
需要一对一辅导?Need One-on-One Tutoring?
A-Level / GCSE / IB / KS2-KS3 全科辅导
16621398022同微信
Visit aleveler.com
或截图后用「扫一扫 → 相册」选择本图