Cambridge IGCSE · Topic 8.1
IGCSE Computer Science 0478 / Topic 8 / IGCSE

Do it again.

Computers are good at repeating things. Learn the three kinds of loop in Cambridge pseudocode, when to use each one, and how loops are used for totalling and counting.

The key idea

Iteration means repeating a group of statements. A count-controlled loop (FOR ... NEXT) runs a set number of times. A pre-condition loop (WHILE ... DO ... ENDWHILE) tests its condition before each pass, so it may run zero times. A post-condition loop (REPEAT ... UNTIL) tests its condition after each pass, so it always runs at least once. Totalling (Total ← Total + Number) and counting (Count ← Count + 1) are done inside loops.

FOR ... NEXTWHILE ... DOREPEAT ... UNTIL count-controlledpre-conditionpost-condition body5 passes for1 TO 5 testbody bodytest number of passesknown in advance tested first:body may run0 times tested last:body runs atleast once
Fig. 1: the three loop types differ in when the condition is checked
Section 01

Count-controlled: FOR.

In the lab, try the Nested FOR with STEP and REPEAT UNTIL programs. The trace table shows the loop variable taking each value in turn.

DECLARE Count : INTEGER
FOR Count ← 1 TO 5
    OUTPUT Count * Count
NEXT Count

This outputs 1, 4, 9, 16 and 25. The loop variable takes every value from 1 to 5 inclusive. A STEP changes the increment, and it can be negative:

FOR Count ← 10 TO 0 STEP -2
    OUTPUT Count
NEXT Count

This outputs 10, 8, 6, 4, 2 and 0.

The same idea in Python (for comparison only: the exam uses pseudocode)

for count in range(1, 6):        # range stops BEFORE 6
    print(count * count)
for count in range(10, -1, -2):  # 10, 8, 6, 4, 2, 0
    print(count)
Worked example Counting passes
How many times does the body of FOR X ← 3 TO 12 STEP 4 run, and with which values of X?
  1. X starts at 3: 3 <= 12, run the body.
  2. X ← 7: run. X ← 11: run.
  3. X ← 15 goes past 12, so the loop ends.
Answer: 3 times, with X = 3, 7 and 11
Examiner trap

A FOR loop in pseudocode includes the end value: FOR Count ← 1 TO 5 runs 5 times. Python's range(1, 5) would stop at 4, so do not mix the two up.

Section 02

Pre-condition and post-condition.

Use WHILE or REPEAT when you do not know in advance how many passes are needed: the loop stops when a condition changes.

DECLARE Number : INTEGER
INPUT Number
WHILE Number > 9 DO
    Number ← Number - 9
ENDWHILE
OUTPUT Number
DECLARE Password : STRING
REPEAT
    OUTPUT "Please enter the password"
    INPUT Password
UNTIL Password = "Secret"
WHILE ... ENDWHILEREPEAT ... UNTIL
Condition testedbefore each passafter each pass
Fewest passes01
Loop continues while the condition isTRUEFALSE

The same idea in Python (for comparison only: the exam uses pseudocode)

while True:                      # Python has no REPEAT ... UNTIL
    password = input("Please enter the password")
    if password == "Secret":
        break
Worked example Trace a WHILE loop
The WHILE program above is run with the input 30. What is output?
  1. 30 > 9 is TRUE: Number ← 21.
  2. 21 > 9 is TRUE: Number ← 12.
  3. 12 > 9 is TRUE: Number ← 3.
  4. 3 > 9 is FALSE, so the loop ends and 3 is output.
Answer: 3
Your turn
What does the same WHILE program output for the input 9?
Examiner trap

The REPEAT condition says when to stop; the WHILE condition says when to keep going. Turning one into the other means reversing the condition.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

Which loop always runs its body at least once?

Quick check+10 XP

How many times does the body of FOR Count ← 2 TO 6 run?

Quick check+10 XP

In the lab's WHILE loop program, the input is 40. What is output?

Quick check+10 XP

Which statement adds a value to a running total?

Section 03

Totalling and counting.

A running total adds each value on; a counter adds 1 each time something happens. Both must be set to 0 before the loop starts.

DECLARE Total : INTEGER
DECLARE Count : INTEGER
DECLARE Mark : INTEGER
DECLARE Index : INTEGER
Total ← 0
Count ← 0
FOR Index ← 1 TO 5
    INPUT Mark
    Total ← Total + Mark
    IF Mark >= 50
      THEN
        Count ← Count + 1
    ENDIF
NEXT Index
OUTPUT "Total: ", Total, " Passed: ", Count
Worked example Trace table
The marks entered are 62, 45, 50, 38 and 71. What is output?
  1. 62: Total 62, 62 >= 50 so Count 1.
  2. 45: Total 107, Count stays 1.
  3. 50: Total 157, 50 >= 50 so Count 2.
  4. 38: Total 195, Count stays 2.
  5. 71: Total 266, Count 3.
Answer: Total: 266 Passed: 3
Your turn
How would you output the average mark as well?
Examiner trap

Put Total ← 0 before the loop. If it is inside the loop, the total is reset on every pass and only the last mark is kept.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

What is output by FOR X ← 1 TO 10 STEP 3, OUTPUT X, NEXT X?

Exam style+20 XP

A program must ask for a PIN and keep asking until a valid one is entered. Which construct is most suitable?

Exam style+20 XP

X is 3. WHILE X < 20 DO X ← X * 2 ENDWHILE, then OUTPUT X. What is output?

Exam style+20 XP

Count ← 0, REPEAT Count ← Count + 2 UNTIL Count > 7, OUTPUT Count. What is output?

Skill unlocked

Iteration, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
FOR, WHILE and REPEAT loops: choosing, tracing and writing them, with totalling and counting.