Welcome to the OCR Exam Reference Language Guide!

In your OCR J277 exam, you will see code written in a specific way called Exam Reference Language (ERL). It’s not a language you run on a computer like Python or Java; instead, it is a clear, standard way of writing logic so that everyone in the country is tested on the same thing. This chapter focuses on how ERL handles strings, arrays, files, and sub programs.

Don't worry if these look a bit different from the programming language you use in class. Once you learn the "rules of the road" for ERL, you'll find it very logical and easy to follow!

1. String Manipulation

A string is just a sequence of characters (letters, numbers, or symbols) wrapped in speech marks. In ERL, strings are 0-indexed. This means the very first character is at position \(0\), the second is at \(1\), and so on.

Imagine the string "COMPUTER":

Index \(0\): C
Index \(1\): O
Index \(2\): M
...and so on.

Common String Methods in ERL:

.length — Returns how many characters are in the string.
Example: "Hello".length would give \(5\).

.substring(x, i) — Starts at index x and takes i characters.
Example: "Science".substring(0, 3) gives "Sci".

.left(i) — Takes the first i characters from the left.
Example: "Apple".left(2) gives "Ap".

.right(i) — Takes the last i characters from the right.
Example: "Apple".right(2) gives "le".

.upper and .lower — Changes the string to ALL CAPS or all lowercase.
Example: "Cat".upper gives "CAT".

Concatenation (+) — Joining two strings together.
Example: "Computer" + "Science" gives "ComputerScience".

ASC(...) and CHR(...)ASC returns the number code for a character; CHR returns the character for a number.
Example: ASC("A") might give \(65\).

Quick Tip: Always remember to start counting from \(0\)! If you start at \(1\), your "substring" or "index" answers will be wrong.

2. Arrays (1D and 2D)

An array is a variable that can hold multiple pieces of data under one name, as long as they are the same data type. Like strings, arrays in ERL are 0-indexed.

1D Arrays (One-Dimensional)

Think of this like a single row of lockers.

To create an empty array of 5 items: array names[5]
To create an array with values: array colours = ["Red", "Blue", "Green"]
To change a value: colours[0] = "Yellow" (This changes "Red" to "Yellow").

2D Arrays (Two-Dimensional)

Think of this like a grid, a table, or a chessboard. You need two coordinates to find a value: [row, column].

array gameboard[8, 8] — This creates a grid with 8 rows and 8 columns.
gameboard[1, 0] = "Pawn" — This puts "Pawn" in the second row (index \(1\)) and first column (index \(0\)).

Key Takeaway: Arrays are "static" in ERL, meaning they have a fixed length once you create them.

3. File Handling

Programs often need to save data so it isn't lost when the power goes out. ERL uses specific commands to talk to external text files.

Step-by-Step File Process:
1. newFile("data.txt") — Creates a brand new file.
2. myFile = open("data.txt") — You must assign the opened file to a variable to "hold" it.
3. myFile.writeLine("Hello") — Adds a line of text to the end of the file.
4. line = myFile.readLine() — Reads the next available line from the file.
5. myFile.close() — Always close the file when finished to save it properly!

Checking for the end of the file:

We often use a loop to read a whole file until there is nothing left. In ERL, we use .endOfFile().
Example:
while NOT myFile.endOfFile()
    print(myFile.readLine())
endwhile

4. Sub Programs (Procedures and Functions)

Sub programs are mini-programs inside your main code. They help keep code organized and prevent you from writing the same thing twice. They can use local variables (only exist inside the sub program) or global variables (exist everywhere).

Procedures

A procedure performs a task but does not send a value back to the main program.

procedure welcome(name)
    print("Hello " + name)
endprocedure

Called like this: welcome("Alice")

Functions

A function is different because it returns a value. Think of it like a calculator: you give it numbers, and it hands you back the answer.

function multiply(a, b)
    return a * b
endfunction

Called like this: result = multiply(5, 4) (The variable result would now hold \(20\)).

Did you know? In ERL, you can pass arrays into sub programs just like you pass single variables!

Common Mistakes to Avoid

1. Mixing up DIV and MOD: Remember, DIV is the whole number part of a division, and MOD is the remainder.
2. Off-by-one errors: In an array of size \(5\), the last index is \(4\), not \(5\).
3. Forgetting to Return: If a question asks for a function, make sure you use the return keyword, or it’s just a procedure!
4. File Variable: Remember that myFile = open("test.txt") requires that myFile variable. You can't just write open("test.txt").readLine().

Quick Review:
- Strings and Arrays start at index \(0\).
- Functions return values; Procedures do not.
- Use .endOfFile() to avoid errors when reading files.
- .substring(x, i): \(x\) is where you start, \(i\) is how many you take.