Teachers' Portal  /  IGCSE Computer Science 0478  /  Programming  /  Iteration: count-controlled, pre-condition and post-condition loops
Lesson pack · IGCSE Computer Science 0478 · 8.1 · IGCSE

Iteration: count-controlled, pre-condition and post-condition loops

Do it again. Computers are good at repeating things.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Programming
Syllabus reference
Cambridge IGCSE Computer Science 0478, 8.1
Level
IGCSE
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 3 of 11 in Programming; follows Sequence and selection: IF and CASE; leads into String handling
Central model
Fig. 1: the three loop types differ in when the condition is checked
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • Count-controlled: FOR: In the lab, try the Nested FOR with STEP and REPEAT UNTIL programs.
  • 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.
  • Totalling and counting: A running total adds each value on; a counter adds 1 each time something happens.
Key points to leave on the board
  • FOR: known number of passes; the end value is included.
  • WHILE: tested first, may run 0 times, continues while TRUE.
  • REPEAT: tested last, runs at least once, stops when the UNTIL condition is TRUE.
  • Totals and counts: set to 0 before the loop, update inside it, output after it.
The core visual model

One picture every idea returns to

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
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, Do it again.. Computers are good at repeating things. Put the figure on the board: it is the one picture every idea in the lesson returns to.Whole class, then pairs
5 to 13 minTeach and check: Count-controlled: FORIn the lab, try the Nested FOR with STEP and REPEAT UNTIL programs. Project tab 1 of the lab (Loops with a trace table) and let a student drive it while the class predicts. Model the worked example Counting passes on the board, one line at a time.Whole class, then pairs
13 to 21 minTeach and check: Pre-condition and post-conditionUse WHILE or REPEAT when you do not know in advance how many passes are needed: the loop stops when a condition changes. Model the worked example Trace a WHILE loop on the board, one line at a time. Your turn: What does the same WHILE program output for the input 9?Pairs
21 to 30 minTeach and check: Totalling and countingA running total adds each value on; a counter adds 1 each time something happens. Model the worked example Trace table on the board, one line at a time. Your turn: How would you output the average mark as well?Jigsaw
30 to 36 minCheckThe four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong.Individual, then whole class
36 to 43 minPlenaryThe exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording.Individual, random call
43 to 45 minExit and homeworkSet the worksheet below (6 questions, 18 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

Protect the parts that carry the learning

The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.

Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.

Cut first if the build overruns: the last check in Totalling and counting, never the check or the plenary.

In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.

Running the cooperative task

Jigsaw

Split the content into expert pieces. Expert groups master one piece each, then regroup so every home team has one expert per piece, and each expert teaches their piece to the team.

The full structure, with its variations and the other twelve, is on the cooperative-learning page.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching move that pre-empts it
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.
The REPEAT condition says when to stop; the WHILE condition says when to keep going.Turning one into the other means reversing the condition.
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.
Worksheet

Iteration: count-controlled, pre-condition and post-condition loops: worksheet

6 questions, 18 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · recall[3 marks]

Describe the three types of loop used in pseudocode.

Q2 · trace[4 marks]

Complete a trace table for this pseudocode and state the output.

Total ← 0
Number ← 1
WHILE Total < 20 DO
    Total ← Total + Number
    Number ← Number + 2
ENDWHILE
OUTPUT Total, " ", Number
Q3 · write[4 marks]

Write pseudocode that uses a FOR loop to input 10 numbers, then outputs their total and their average.

Q4 · write[3 marks]

Write pseudocode using a REPEAT ... UNTIL loop that makes the user enter a number from 1 to 10 inclusive.

Q5 · explain[2 marks]

Explain the difference between a pre-condition loop and a post-condition loop.

Q6 · analysis[2 marks]

This loop should output the numbers 1 to 5 but never stops. Explain why and correct it.

Count ← 1
WHILE Count <= 5 DO
    OUTPUT Count
ENDWHILE
Answer key and marking notes
Q1 · 3 marks
  • Count-controlled (FOR): repeats a set number of times.
  • Pre-condition (WHILE): the condition is tested before each pass, so it may not run at all.
  • Post-condition (REPEAT ... UNTIL): the condition is tested after each pass, so it runs at least once.
Q2 · 4 marks
  • Total: 0, 1, 4, 9, 16, 25.
  • Number: 1, 3, 5, 7, 9, 11.
  • The loop stops when Total is 25 (25 < 20 is FALSE).
  • Output: 25 11
Q3 · 4 marks
  • Total ← 0 before the loop
  • FOR Count ← 1 TO 10 ... NEXT Count
  • INPUT Number and Total ← Total + Number inside the loop
  • After the loop: OUTPUT Total, Total / 10
Q4 · 3 marks
  • REPEAT
  • INPUT Number (with a prompt)
  • UNTIL Number >= 1 AND Number <= 10
Q5 · 2 marks
  • A pre-condition loop tests the condition before the body, so the body may run zero times.
  • A post-condition loop tests the condition after the body, so the body always runs at least once.
Q6 · 2 marks
  • Count is never changed inside the loop, so Count <= 5 is always TRUE.
  • Add Count ← Count + 1 inside the loop, after the OUTPUT.
Quiz

6 multiple-choice questions on this lesson

From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.

Q11 mark

Which type of loop always runs its body at least once?

  • AA post-condition loop: REPEAT ... UNTIL
  • BA pre-condition loop: WHILE ... DO ... ENDWHILE
  • CA count-controlled loop: FOR ... NEXT
  • DAny loop that changes a counter variable inside its body
Q21 mark

How many times does the body of the loop FOR X ← 2 TO 14 STEP 3 run?

  • A5
  • B4
  • C6
  • D12
Q31 mark

What is output by this pseudocode?

Number ← 50
Count ← 0
WHILE Number > 10 DO
    Number ← Number - 12
    Count ← Count + 1
ENDWHILE
OUTPUT Count, " ", Number

  • A4 2
  • B3 14
  • C5 -10
  • D4 14
Q41 mark

The inputs are 12, 8 and 5. What is output?

DECLARE Total : INTEGER
DECLARE Mark : INTEGER
DECLARE Index : INTEGER
FOR Index ← 1 TO 3
    Total ← 0
    INPUT Mark
    Total ← Total + Mark
NEXT Index
OUTPUT Total

  • A5
  • B25
  • C12
  • D0
Q51 mark

This loop is rewritten as a WHILE loop with an INPUT Guess just before it and another at the end of its body.

REPEAT
    INPUT Guess
UNTIL Guess = 7
Which first line should the WHILE loop have?

  • AWHILE Guess <> 7 DO
  • BWHILE Guess = 7 DO
  • CWHILE Guess > 7 DO
  • DWHILE Guess < 7 DO
Q61 mark

The inputs are 14, 20, 11 and -1. What is output?

Total ← 0
Count ← 0
INPUT Mark
WHILE Mark <> -1 DO
    Total ← Total + Mark
    Count ← Count + 1
    INPUT Mark
ENDWHILE
OUTPUT Total / Count

  • A15
  • B11
  • C11.25
  • D45
Quiz key
Q1: A

REPEAT ... UNTIL tests its condition after each pass, so the body has always run once before the first test. A WHILE loop tests first and may run zero times. A FOR loop runs a set number of times, which can be zero (for example FOR Count ← 5 TO 1). Changing a counter has nothing to do with when the condition is tested.

Q2: A

X takes the values 2, 5, 8, 11 and 14, and the end value is included, so the body runs 5 times. 4 forgets that 14 itself is reached. 6 would need one more value of X, for example a start at -1. 12 is 14 - 2, which ignores the STEP.

Q3: A

Number goes 50, 38, 26, 14, 2 and Count goes 0, 1, 2, 3, 4. When Number is 2 the test 2 > 10 is FALSE, so the loop stops and 4 2 is output. 3 14 stops one pass early (14 > 10 is still TRUE). 5 -10 does one pass too many. 4 14 gives the right count but the value before the last subtraction.

Q4: A

Total ← 0 is inside the loop, so the total is reset on every pass and only the last mark survives: 0 + 5 = 5. 25 is what the program should output, which needs Total ← 0 before the loop. 12 would be the first mark only. 0 would need the reset to come after the addition.

Q5: A

The REPEAT loop stops when Guess = 7, so the WHILE loop must keep going while Guess is not 7: Guess <> 7, the reverse of the UNTIL condition. Guess = 7 would loop only for the correct answer. Guess > 7 stops on any guess below 7, and Guess < 7 stops on any guess above 7, so both accept wrong guesses.

Q6: A

The test happens before each pass, so -1 ends the loop without being added or counted: Total = 45, Count = 3 and 45 / 3 = 15. 11 would come from adding and counting the -1 (44 / 4). 11.25 counts the -1 but does not add it (45 / 4). 45 is the total, not the average.

Lab and demo

What to project

The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.

Slides

Slides for this lesson

A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.
Previous lessonSequence and selection: IF and CASE