Course 8.1Lesson 3 of 3
A program repeats steps with a loop. This lesson writes the three loops of the syllabus in Cambridge pseudocode: the count-controlled FOR loop, the pre-condition WHILE loop and the post-condition REPEAT loop. It then uses them for totals, counts and nested statements, with a trace table.
Try the code and the simulations beside the lesson.
A teacher adds up thirty marks. A game asks for a password until it is right. A program halves a number while it is more than one. Each one repeats steps. This is iteration. The Python Foundations chapters "Loops: Repeat with for" and "Loops: Repeat Until with while" teach it in Python. The syllabus has three loops, and the exam asks you to choose the right one.
Use a FOR loop when you know how many times to repeat.
DECLARE Count : INTEGER
DECLARE Total : INTEGER
Total ← 0
FOR Count ← 2 TO 10 STEP 2
Total ← Total + Count
NEXT Count
OUTPUT TotalThe counter Count takes each value from 2 to 10, going up by the STEP of 2. The statements inside run once for each value. So Total becomes , and the output is 30. Without STEP, the counter goes up by 1. The step can be negative, to count down. If the start value is already past the end value, the loop runs no times at all.
In Python, FOR Count ← 2 TO 10 STEP 2 is for count in range(2, 11, 2). The end of range() is one past the last value.
DECLARE Password : STRING
REPEAT
OUTPUT "Please enter the password"
INPUT Password
UNTIL Password = "Secret"The condition is tested after the statements. So the statements always run at least once, and the loop stops when the condition is TRUE. That suits a password check: you must ask at least once. Python has no REPEAT keyword. It uses while True: with break at the end.
DECLARE Number : INTEGER
INPUT Number
WHILE Number > 9 DO
Number ← Number - 9
ENDWHILE
OUTPUT NumberThe condition is tested before the statements. The loop goes on while the condition is TRUE. If it is FALSE the first time, the statements never run. With the input 40, Number becomes 31, 22, 13 and then 4, so the output is 4. With the input 7, the loop does not run, and the output is 7.
The two flowcharts show the difference. In the WHILE loop the decision comes before the statements. In the REPEAT loop it comes after them.
A total starts at zero and has each new value added: Total ← Total + Temperature. A count starts at zero and goes up by one each time something happens: HotDays ← HotDays + 1. Here a weather station reads five temperatures in degrees Celsius:
DECLARE Count : INTEGER
DECLARE Temperature : REAL
DECLARE Total : REAL
DECLARE HotDays : INTEGER
Total ← 0.0
HotDays ← 0
FOR Count ← 1 TO 5
INPUT Temperature
Total ← Total + Temperature
IF Temperature > 30.0
THEN
HotDays ← HotDays + 1
ENDIF
NEXT Count
OUTPUT "Average ", Total / 5
OUTPUT "Hot days ", HotDaysThe five inputs are , , , and . Three of them are above . Their total is , so the average is . The output is Average 30.5 and then Hot days 3. Notice the IF statement inside the FOR loop. That is a nested statement.
A trace table records the value of each variable, and each output, at every step of a dry run. You start a new row when a value changes. Here is the start of the trace table for the weather program.
Read it from the top. Count is 1 and the input is : the total becomes , and the count of hot days stays 0. In the second row the input is above , so HotDays becomes 1. A trace table on a phone is easier to read with three columns at most, so the outputs are left for the end.
A loop can sit inside another loop. The inner loop runs all the way through for every pass of the outer loop.
DECLARE Row : INTEGER
DECLARE Column : INTEGER
DECLARE Stars : INTEGER
Stars ← 0
FOR Row ← 1 TO 3
FOR Column ← 1 TO Row
Stars ← Stars + 1
NEXT Column
NEXT Row
OUTPUT StarsFor Row 1 the inner loop runs once, for Row 2 twice, and for Row 3 three times. So Stars is . The exam does not ask you to write more than three levels of nesting.
The program has the three loops of this lesson in Python, and a nested loop that prints a small times table.
FOR Count ends with NEXT Count.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
FOR Index ← 10 TO 1 STEP -3 run, and what values does Index take?25, what does the WHILE program of this lesson output?TO Row to TO 3. What is the output now?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place in the course.