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.
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.
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)
FOR X ← 3 TO 12 STEP 4 run, and with which values of X?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.
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 ... ENDWHILE | REPEAT ... UNTIL | |
|---|---|---|
| Condition tested | before each pass | after each pass |
| Fewest passes | 0 | 1 |
| Loop continues while the condition is | TRUE | FALSE |
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
The REPEAT condition says when to stop; the WHILE condition says when to keep going. Turning one into the other means reversing the condition.
Four quick checks. Each correct answer earns XP and records this skill on your profile.
Which loop always runs its body at least once?
How many times does the body of FOR Count ← 2 TO 6 run?
In the lab's WHILE loop program, the input is 40. What is output?
Which statement adds a value to a running total?
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
OUTPUT "Average: ", Total / 5, which gives 266 / 5 = 53.2.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.
Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.
What is output by FOR X ← 1 TO 10 STEP 3, OUTPUT X, NEXT X?
A program must ask for a PIN and keep asking until a valid one is entered. Which construct is most suitable?
X is 3. WHILE X < 20 DO X ← X * 2 ENDWHILE, then OUTPUT X. What is output?
Count ← 0, REPEAT Count ← Count + 2 UNTIL Count > 7, OUTPUT Count. What is output?
This skill is now recorded on your profile. Keep the chain going.