[ OCR J277 ERL ]

Exam Reference Language (ERL) Quick Reference

This is a complete list of valid ERL syntax as specified by OCR for the J277 specification.

1. Variables and Constants

SyntaxDescription
variableName = valueAssigns a value to a variable based on the given data type.
const name = valueCreates a constant which cannot be changed later.
global variableNameDeclares a variable that is accessible across all subroutines.
str(x), int(x), float(x), bool(x)Type casting (converting between data types).

2. Input & Output

SyntaxDescription
print("Hello")Outputs data to the screen.
name = input("Prompt")Receives input from the keyboard.

3. Operators

SyntaxDescription
+, -, *, /, ^Standard arithmetic: Add, Subtract, Multiply, Divide, Exponentiation.
MODModulo arithmetic. Finds the remainder (e.g., 10 MOD 3 is 1).
DIVInteger division. Finds the whole number of divisions (e.g., 10 DIV 3 is 3).
==, !=, <, <=, >, >=Comparison operators (Equal, Not equal, Less than, etc.).
AND, OR, NOTLogical (Boolean) operators.

4. Selection (IF & SWITCH)

SyntaxExample
if ... then
elseif ... then
else
endif
if age >= 18 then
    print("Adult")
elseif age >= 13 then
    print("Teen")
else
    print("Child")
endif
switch ... :
case ...:
default:
endswitch
switch choice:
    case "A":
        print("Apple")
    case "B":
        print("Banana")
    default:
        print("Invalid")
endswitch

5. Iteration (Loops)

TypeExample
Count-controlled (FOR)
for x = 0 to 9
    print(x)
next x
Condition-controlled (WHILE)
while password != "123"
    password = input("Try again")
endwhile
Condition-controlled (DO UNTIL)
do
    password = input("Try again")
until password == "123"

6. Arrays

SyntaxDescription
array names[5]Creates a 1D array of length 5 (indices 0 to 4).
array grid[3, 3]Creates a 2D array (3 rows, 3 columns).
names[0] = "Bob"Assigns a value to a 0-indexed element.

7. Subroutines

SyntaxExample
Procedure (No return value)
procedure sayHello(name)
    print("Hello " + name)
endprocedure
Function (Returns a value)
function addMe(x, y)
    return x + y
endfunction

8. File Handling

SyntaxDescription
myFile = openRead("data.txt")Opens a file for reading.
myFile = openWrite("data.txt")Opens a file for writing.
myFile.readLine()Reads a single line from the file.
myFile.writeLine(data)Writes a single line to the file.
myFile.endOfFile()Returns True if the end of the file has been reached.
myFile.close()Closes the file.

9. String Operations

SyntaxDescription
stringname.lengthReturns the number of characters in the string.
stringname.substring(start, length)Returns a portion of the string. Example: "Hello".substring(0,2) is "He".
stringname.upperConverts the string to UPPERCASE.
stringname.lowerConverts the string to lowercase.