OCR GCSE Computer Science Exam Reference Language (J277): every command with examples
GCSE Computer Science
Start today's paperPaper resets in
Formula sheet
The card below opens today's Computer Science paper with the language reference in the drawer, so you practise reading and writing code the way the paper presents it. The tables follow, in our own layout. The commands are those in OCR's specification section 3c, so check it before you rely on anything here. The specification does not say whether the language is printed as a sheet in the paper, so learn it as if it is not.
GCSE Computer Science
Operators
MOD gives the remainder after division and DIV gives the whole number quotient.
| Concept | Keywords and symbols | Example |
|---|---|---|
| Comparison | == != < <= > >= | x >= 18 |
| Arithmetic | + - * / ^ MOD DIV | 17 MOD 5 gives 2, 17 DIV 5 gives 3 |
| Boolean | AND OR NOT | age >= 13 AND age < 20 |
Variables, input and output
| Concept | Keywords and symbols | Example |
|---|---|---|
| Comment | // | // this is a comment |
| Assignment | = | x = 3 |
| Constant | const | const vat = 0.2 |
| Global variable | global | global userID = "Cust001" |
| Input | input(...) | myName = input("Enter a name") |
| Output | print(...) | print("Hello") |
| Casting | str() int() float() real() bool() | int("3") gives 3 |
Iteration and selection
| Concept | Keywords and symbols | Example |
|---|---|---|
| Count-controlled loop | for ... to ... next | for i = 0 to 9 ... next i |
| Loop with a step | step | for i = 10 to 0 step -1 |
| Condition-controlled loop, test first | while ... endwhile | while answer != "Correct" |
| Condition-controlled loop, test last | do ... until | do ... until answer == "Correct" |
| Selection | if ... then elseif ... then else endif | if mark >= 50 then |
| Case selection | switch ... : case ... : default: endswitch | switch day : case "Sat": |
Strings
In .substring(x, i), x is the starting index and i is the number of characters. Strings are indexed from 0.
| Concept | Keywords and symbols | Example |
|---|---|---|
| Length | .length | subject.length |
| Substring | .substring(x, i) | subject.substring(3, 5) |
| Left and right | .left(i) .right(i) | subject.left(4) |
| Concatenation | + | print("Hello " + name) |
| Upper and lower case | .upper .lower | subject.upper |
| ASCII conversion | ASC(...) CHR(...) | ASC('A') gives 65 |
Files and arrays
Arrays are indexed from 0, so array colours[5] has indexes 0 to 4, and an array stores one data type.
| Concept | Keywords and symbols | Example |
|---|---|---|
| Open and close a file | open(...) .close() | myFile = open("sample.txt") |
| Read a line | .readLine() | myFile.readLine() |
| Write a line | .writeLine(...) | myFile.writeLine("New line") |
| End of file | .endOfFile() | while NOT myFile.endOfFile() |
| Create a file | newFile(...) | newFile("myText.txt") |
| One dimensional array | array name[n] | array colours[5] |
| Two dimensional array | array name[r, c] | array gameboard[8, 8] |
| Assign an element | name[i] = ... | names[3] = "Noni" |
Subprograms and random numbers
random(1, 6) gives a random integer from 1 to 6 inclusive, and random(-1.0, 10.0) gives a real number in that range.
| Concept | Keywords and symbols | Example |
|---|---|---|
| Procedure | procedure ... endprocedure | procedure greet(name) |
| Function | function ... return ... endfunction | function double(n) |
| Call a procedure | name(parameters) | greet("Ada") |
| Call a function | name(parameters) | y = double(4) |
| Random number | random(..., ...) | x = random(1, 6) |
Reading and writing ERL
Read the code from the top, one line at a time, and keep a table of the variables. Take this short program, which averages five marks:
total = 0
for i = 0 to 4
mark = int(input("Enter mark"))
total = total + mark
next i
average = total / 5
print("Average: " + str(average))The loop runs for i = 0, 1, 2, 3 and 4, which is 5 times. Suppose the marks entered are 12, 15, 9, 18 and 6. Trace the variables line by line:
| i | mark | total |
|---|---|---|
| 0 | 12 | 12 |
| 1 | 15 | 27 |
| 2 | 9 | 36 |
| 3 | 18 | 54 |
| 4 | 6 | 60 |
The program then prints the word Average followed by 12, because 60 / 5 = 12. The input function returns text, so the int() casting is needed before adding.
A function that returns a value
function isEven(n)
if n MOD 2 == 0 then
return true
else
return false
endif
endfunction
print(isEven(17))17 MOD 2 is 1, so the condition is false and the program prints false. A function returns a value, so store it in a variable or print it. A procedure does not return one.
What the reference does not give you
- How to choose the structure. It lists a for loop, a while loop and a do until loop. Which one a question needs is your decision.
- Algorithms. Searching, sorting and the other algorithms in the specification are not written out. You need to know how they work.
- Logic and design tools. Flowchart symbols, trace tables and the rest of the Component 2 skills sit outside the language reference.
- Every function of every language. If you answer in a high-level language, the reference does not cover its own syntax.
- Errors. It does not tell you what a syntax or logic error looks like, so learn to spot both.
How to use ERL in the exam
- Read the whole program once before you trace it.
- Make a table with a column for each variable, and fill in one row for each pass of a loop.
- Remember that arrays and strings are indexed from 0.
- In a "write" question use the exact keywords: endif, endwhile, next, endfunction.
- Check boundary values: a for loop to 9 includes 9, and DIV and MOD behave differently from /.
ERL and other boards' languages
OCR's specification says answers in pseudocode, natural English or bullet points will not be awarded marks where a response must be in ERL or a high-level language. Other boards use their own language references,. If you sit AQA or Edexcel, use the language your board names.
The specification says Component 2 lasts 1 hour 30 minutes. For the boards, papers and grade boundaries, see our GCSE Computer Science guide.
Use the sheet with today's paper
Uni-Sine publishes a free daily paper for GCSE Computer Science. It is a short timed paper of 8 questions worth 25 to 35 marks, not a full OCR paper, and it is not written by OCR. Open the reference, set a timer and write the code you use at the top of each answer. After marking, look at any question where you used the wrong keyword or the wrong loop.
Common mistakes
- Off by one in a for loop. for i = 0 to 9 runs 10 times, from 0 to 9 inclusive.
- Mixing up MOD and DIV. MOD is the remainder and DIV is the whole number quotient.
- Forgetting to cast input. input() gives text, so use int() or float() before arithmetic.
- Counting arrays from 1. array colours[5] has indexes 0 to 4.
- Missing the end keyword. Every if has an endif, and every while has an endwhile.
- Using = to compare. Assignment is =, and comparison is ==.
Exam answers
The answers and a worked mark scheme come with today's paper once it is marked. Sit the paper with the sheet open beside you, mark it, and the worked answers show which line of the sheet each question wanted. We do not publish official or past paper answers.
Common questions
What is the OCR Exam Reference Language?
Do I have to write my answers in ERL?
Will I lose marks for a small syntax error in ERL?
Are pseudocode answers accepted?
What is the difference between MOD and DIV in ERL?
Revision cards
Say the answer first, then flip the card to check it.
Where to go next
- Solve it
Code Editor
Write and run code right in your browser
Open the tool - Learn it
Programming Languages
Compiled vs interpreted, paradigms and the evolution of how we talk to machines
Start the course - Test it
GCSE Computer Science
A new exam-style paper every day, with instant marking and a mark scheme
Start today's paper