Loading...
Read the convention used by the question before tracing an algorithm. OCR’s inclusive loop limits and substring length parameter differ from familiar Python range and slicing conventions.
What you'll be able to do
OCR pseudocode assigns with = and compares equality with ==. IF/ELSEIF/ELSE selects a path, while SWITCH/CASE expresses alternatives for a value. Arithmetic includes MOD for a remainder, DIV for an integer quotient and ^ for exponentiation. Do not import a different language’s interpretation of a symbol without checking the question.
A FOR i=0 TO 3 loop includes both endpoints and executes four iterations. A WHILE loop tests before its body; a DO ... UNTIL loop tests after and repeats until its condition is true. That means the post-test body executes at least once. Trace each variable update in order, and record the condition’s value at the point it is actually tested.
OCR arrays use zero-based indexing. A five-element array has positions 0 through 4. For strings, subString(startingPosition,numberOfCharacters) specifies a start and a count, rather than an exclusive stop index. In text="ALGORITHM", text.subString(2,3) returns "GOR", taking positions 2,3,4.
The string’s length is nine, so its last character is at index eight. Python text[2:3] would take only the character at position two, illustrating why syntax must be interpreted in its own model. Learners may write clear alternative pseudocode; exact OCR spelling is not a memorisation requirement, but an examiner must be able to infer the algorithm’s meaning.
openRead obtains a file for reading, readLine returns the next text line, endOfFile tests whether reading is complete and close releases the handle. The example checks the file’s state before reading again, so an empty file outputs no lines. An empty line in a file is not the same thing as reaching the end.
openWrite opens a file for writing under OCR’s guide and overwrites previous contents. writeLine writes a line; closing finishes the stated use. If retaining earlier records matters, the algorithm must use a suitable explicitly defined append or update strategy rather than assume openWrite means append. File permissions, missing resources and failure handling also need a defined policy in a real program.
total = 0
for i = 0 to 3
total = total + i
next i
print(total)
text = "ALGORITHM"
print(text.subString(2, 3))
source = openRead("notes.txt")
while NOT source.endOfFile()
print(source.readLine())
endwhile
source.close()Scroll horizontally to see the full code or table.Attempt the task before opening its worked response. Record each change of state, explain the rule used, and check the result against the original requirements. Code examples labelled Python 3 use that language’s conventions; pseudocode states its own assumptions.
Common mistakes to avoid
Key takeaways
Test yourself
Answer a few questions, then review anything you missed.
Start with the quiz above to check the key ideas. You can also choose another topic from the practice library.