Introduction to Reading and Tracing Code

In your OCR J277 exam, you aren't just expected to write code; you need to be a "code detective." Reading code is the ability to look at a program and understand what it is trying to do. Tracing is a specific technique where you follow the code line-by-line, keeping track of how the values of variables change. This is a vital skill for Section B of Paper 2, where you will often be asked to complete trace tables to show how an algorithm works.

Don't worry if reading code feels like learning a secret language at first. By breaking it down into small steps, you can master even the most complex algorithms!

The Basics: Understanding the "Language of the Exam"

Before you can trace code, you need to recognize the "grammar" of the OCR Exam Reference Language (ERL). Here is a quick refresher of the most important symbols you will see when reading code:

1. Assignment vs. Comparison
One of the most common mistakes is mixing up \( = \) and \( == \).
- \( x = 10 \): This assigns the value 10 to the variable \( x \).
- \( x == 10 \): This checks if \( x \) is equal to 10 (returns True or False).

2. The "Math" Operators
- \( + \), \( - \), \( * \), \( / \): Standard addition, subtraction, multiplication, and division.
- \( ^ \): Exponentiation (e.g., \( 2^3 = 8 \)).
- MOD (Modulo): Gives the remainder after a division. (e.g., \( 10\ MOD\ 3 = 1 \)).
- DIV (Quotient): Gives the whole number part of a division. (e.g., \( 10\ DIV\ 3 = 3 \)).

3. Logic Operators
- AND: Both sides must be true.
- OR: At least one side must be true.
- NOT: Reverses the result (True becomes False).

What is a Trace Table?

A trace table is a tool used to record the state of variables as an algorithm runs. It helps you find logic errors (errors where the code runs but gives the wrong answer).

Imagine a program that counts from 1 to 3 and adds the numbers together. A trace table would have a column for the line number, a column for each variable, and a column for the output.

Note: For a deeper dive into variables and loops, see the chapter on "Programming Fundamentals."

How to Trace Code: A Step-by-Step Guide

When you are asked to trace an algorithm in the exam, follow these steps to avoid getting confused:

Step 1: Set up your columns
Look at the code and identify every variable. Create a column for each one. Also, add a column for "Output" if the code uses print statements.

Step 2: Follow the code line-by-line
Never skip ahead! Execute only the line you are currently reading. If a variable changes, write the new value in the next row of your table under the correct column.

Step 3: Handle Loops Carefully
- FOR loops: Remember that in ERL, \( for\ i = 0\ to\ 4 \) includes both 0 and 4. It will run 5 times.
- WHILE loops: Check the condition before you enter the loop.
- DO UNTIL loops: Run the code at least once, then check the condition at the end to see if you should stop.

Step 4: Update only what changes
If a line of code only changes variable \( A \), leave the column for variable \( B \) blank for that row, or carry the old value down, depending on how the exam table is formatted.

Example Trace

Code:
\( x = 1 \)
\( y = 3 \)
\( while\ x < y \)
    \( x = x + 1 \)
    \( print(x) \)
\( endwhile \)

Tracing process:
1. \( x \) starts at 1, \( y \) starts at 3.
2. Is \( 1 < 3 \)? Yes. Enter loop.
3. \( x \) becomes 2 (\( 1 + 1 \)). Print 2.
4. Is \( 2 < 3 \)? Yes. Loop again.
5. \( x \) becomes 3 (\( 2 + 1 \)). Print 3.
6. Is \( 3 < 3 \)? No. Exit loop.

Identifying Algorithms

Part of "reading" code is recognizing common patterns. The syllabus requires you to identify searching and sorting algorithms from ERL code snippets:

- Linear Search: Look for a loop that checks every item in a list one by one.
- Binary Search: Look for variables named mid, low, and high, and code that repeatedly cuts the data in half.
- Bubble Sort: Look for nested (one inside another) loops and a "swap" process using a temporary variable.
- Merge Sort/Insertion Sort: Look for patterns of splitting lists or inserting items into a sorted section.

Quick Tip: You don't need to memorize the exact code for these algorithms, but you must be able to recognize them when you see them!

Common Pitfalls to Avoid

1. The "Off-by-One" Error: This happens in loops. Always double-check if a loop runs \( n \) or \( n+1 \) times. Remember: \( for\ i = 0\ to\ 5 \) iterates for \( i = 0, 1, 2, 3, 4, 5 \) (6 times).

2. Assuming instead of Tracing: Students often think they know what the code does and skip lines. Even if the code looks like it's calculating a sum, follow it line-by-line—the exam might include a "trick" line that subtracts instead!

3. MOD and DIV confusion: Remember that MOD is the remainder. If you see \( x\ MOD\ 2 == 0 \), the code is checking if a number is even.

Key Takeaways

- Trace Tables are the best way to track variable changes and find logic errors.
- ERL inclusive bounds: \( FOR \) loops include the final number in the range.
- Line-by-line: Only change values when the code specifically tells you to.
- Identify patterns: Use your knowledge of searching and sorting to recognize algorithms quickly.