You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers iteration (loops) as required by OCR J277 Section 2.3. Iteration means repeating a block of code. OCR requires you to know three types of loop: for loops (count-controlled), while loops (condition-controlled), and do...until loops (condition-controlled, post-test).
Iteration is the process of repeating a set of instructions either a fixed number of times or until a condition is met. Without iteration, you would have to write the same code many times over.
There are two main categories:
| Type | Description | OCR Loop Types |
|---|---|---|
| Count-controlled | Repeats a fixed number of times | for loop |
| Condition-controlled | Repeats until a condition changes | while loop, do...until loop |
The following flowchart shows the logic of a while loop (condition checked before each iteration):
flowchart TD
A([Start]) --> B{Condition\ntrue?}
B -- No --> E([End loop])
B -- Yes --> C["Execute loop\nbody"]
C --> D["Update\nvariable"]
D --> B
A for loop repeats a block of code a specific number of times. You know in advance how many iterations are needed.
OCR Pseudocode:
for i = 0 to 4
print("Hello " + str(i))
next i
This prints:
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Python:
for i in range(5): # range(5) gives 0, 1, 2, 3, 4
print("Hello", i)
for variable = start to end
// code to repeat
next variable
start to end inclusive.for i = 1 to 10 runs 10 times (i = 1, 2, 3, ..., 10).for i = 0 to 9 also runs 10 times (i = 0, 1, 2, ..., 9).OCR Exam Tip: In OCR pseudocode,
for i = 0 to 4iterates 5 times (0, 1, 2, 3, 4) because both endpoints are inclusive. In Python,range(5)also gives 5 values (0 to 4) but the upper bound is exclusive. Be careful with this difference.
A while loop repeats as long as a condition is true. The condition is checked before each iteration, so the loop body might not execute at all.
OCR Pseudocode:
password = ""
while password != "secret"
password = input("Enter password: ")
endwhile
print("Access granted")
Python:
password = ""
while password != "secret":
password = input("Enter password: ")
print("Access granted")
A do...until loop is similar to a while loop, but the condition is checked after each iteration. This means the loop body always executes at least once.
OCR Pseudocode:
do
password = input("Enter password: ")
until password == "secret"
print("Access granted")
Python equivalent (Python does not have a built-in do...until, so we simulate it):
while True:
password = input("Enter password: ")
if password == "secret":
break
print("Access granted")
| Feature | while loop | do...until loop |
|---|---|---|
| Condition check | Before each iteration | After each iteration |
| Minimum executions | 0 (might not run at all) | 1 (always runs at least once) |
| OCR syntax | while...endwhile | do...until |
| Use when | Loop might not need to run | Loop must run at least once |
OCR Exam Tip: The do...until loop is a distinctive OCR feature — not all exam boards include it. Remember that it runs the code first, THEN checks the condition. The loop stops when the condition becomes TRUE (unlike while, which stops when the condition becomes FALSE).
| Situation | Best Loop | Reason |
|---|---|---|
| Repeat exactly 10 times | for | Known number of iterations |
| Repeat until user enters "quit" | while or do...until | Unknown number of iterations |
| Validate input (must ask at least once) | do...until | Must execute at least once |
| Process each item in a list | for | Known number of items |
| Keep running until a condition is met | while | Condition-controlled |
OCR Pseudocode:
do
mark = int(input("Enter a mark (0-100): "))
if mark < 0 OR mark > 100 then
print("Invalid! Must be between 0 and 100")
endif
until mark >= 0 AND mark <= 100
print("Valid mark entered: " + str(mark))
Python:
while True:
mark = int(input("Enter a mark (0-100): "))
if 0 <= mark <= 100:
break
print("Invalid! Must be between 0 and 100")
print("Valid mark entered:", mark)
Loops can be placed inside other loops. This is called nesting.
OCR Pseudocode — printing a multiplication table:
for i = 1 to 5
for j = 1 to 5
print(str(i) + " x " + str(j) + " = " + str(i * j))
next j
next i
Python:
for i in range(1, 6):
for j in range(1, 6):
print(f"{i} x {j} = {i * j}")
OCR Exam Tip: If a question says "the user must enter at least one value", this is a hint to use a
do...untilloop. If the question says "repeat 10 times", use aforloop. Choosing the correct loop type demonstrates understanding of iteration.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.