Welcome to Writing and Refining Programs!
In your OCR J277 exam, you won't just be reading code—you'll be expected to write it and refine (improve or fix) it. Think of the OCR Exam Reference Language (ERL) as a middle ground between plain English and a language like Python. It is designed to be clear and logical so you can show off your problem-solving skills without getting stuck on every single comma or bracket!
In this guide, we will learn how to build programs from scratch and how to take a "broken" or "messy" program and make it work perfectly.
1. Writing Programs: The "Input-Process-Output" Model
Every program you write follows a simple pattern: Input (getting data), Process (doing something with it), and Output (showing the result).
The Basics of Writing in ERL
- Input: Use \( input(...) \). You must store the result in a variable using the \( = \) sign.
Example: \( name = input("Enter your name") \) - Variables: You don't need special keywords to create a variable; just name it and assign a value.
Example: \( score = 0 \) - Output: Use \( print(...) \).
Example: \( print("Hello " + name) \)
Quick Tip: In the exam, don't worry about being "perfectly" neat with your handwriting, but do make sure your indentation (sliding the code to the right inside loops and If-statements) is clear. It helps the examiner see your logic!
2. Making Decisions and Repeating Steps
A program that just runs in one straight line is boring. To write useful programs, we use Selection and Iteration.
Selection (Choosing a Path)
We use \( IF \), \( THEN \), \( ELSEIF \), and \( ELSE \) to help the computer make decisions. Always end with \( ENDIF \).
Example:
\( IF\ score >= 50\ THEN \)
\( print("You passed!") \)
\( ELSE \)
\( print("Try again.") \)
\( ENDIF \)
Iteration (Looping)
There are three ways to repeat code in ERL:
- Count-controlled: Use \( FOR \). Best when you know exactly how many times to loop.
\( FOR\ i = 0\ TO\ 9 \dots NEXT\ i \) (This runs 10 times, including 0 and 9). - Condition-controlled (Pre-check): Use \( WHILE \). It checks the condition before it runs.
\( WHILE\ answer != "correct" \dots ENDWHILE \) - Condition-controlled (Post-check): Use \( DO\ UNTIL \). This always runs at least once because it checks the condition at the end.
\( DO \dots UNTIL\ score == 10 \)
Key Takeaway: Choose \( FOR \) if you know the number of loops, \( WHILE \) if it might not need to run at all, and \( DO\ UNTIL \) if it must run at least once.
3. Refining Programs: Fixing and Improving
"Refining" is a fancy word for looking at code that is either wrong or clunky and making it better. In the exam, you might be asked to "refine" a program to handle errors or make it more "robust."
Identifying and Fixing Errors
There are two main types of errors you will need to fix:
- Syntax Errors: These are "grammar" mistakes in the code (e.g., forgetting an \( ENDIF \) or misspelling a keyword). The program won't run at all.
- Logic Errors: The program runs, but the result is wrong (e.g., using \( < \) when you should have used \( <= \)).
How to Refine for "Robustness"
A robust program is one that doesn't crash when a user does something silly. To refine code for robustness, you can:
- Add Input Validation: Use a loop to keep asking for an input until it is valid.
- Add Comments: Use \( // \) to explain what the code is doing.
- Use Meaningful Names: Change a variable name like \( x \) to something descriptive like \( userAge \).
- Improve Structure: Break long code into Sub programs (procedures or functions). (See the chapter on Sub programs for more detail).
4. Using Operators in Your Programs
When writing or refining logic, you must use the correct symbols. Here is a quick reference guide:
Arithmetic Operators
- \( + \), \( - \), \( * \), \( / \) (Standard math)
- \( MOD \): Finds the remainder (e.g., \( 10\ MOD\ 3 = 1 \))
- \( DIV \): Finds the quotient (e.g., \( 10\ DIV\ 3 = 3 \))
- \( ^ \): Exponentiation (e.g., \( 2 ^ 3 = 8 \))
Comparison Operators
- \( == \) (Equal to)
- \( != \) (Not equal to)
- \( < \) (Less than) and \( <= \) (Less than or equal to)
- \( > \) (Greater than) and \( >= \) (Greater than or equal to)
Did you know? Using \( == \) is for comparing two things, while \( = \) is for giving a variable a value. Mixing these up is a very common logic error!
5. Step-by-Step: How to Refine a Program
If you are given an algorithm in the exam and asked to refine it, follow these steps:
- Trace it: Run through the code with sample data (a trace table helps!) to see what it actually does.
- Find the Logic Gap: Does it fail if the user enters a negative number? Does it miss the last item in a list?
- Apply the Fix: Rewrite the specific lines. Usually, this involves changing a condition in an \( IF \) statement or a loop.
- Check "Boundary" cases: If the code checks if someone is 18, make sure it works exactly for the number 18!
Common Mistake to Avoid: When refining a \( FOR \) loop, remember that \( FOR\ i = 0\ TO\ 5 \) includes both 0 and 5. It will run 6 times in total!
Chapter Summary
Writing is about turning a problem into a sequence of Inputs, Processes, and Outputs using ERL syntax. Refining is about spotting mistakes (Syntax or Logic) and making the code more professional and robust. Keep your indentation clear, use the correct operators, and always double-check your loop boundaries!
Note: For more details on specific data structures used in writing programs, see the chapters on Strings, Arrays, and Files.