AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Loops in new clothes

Week 3 · 1.2 Iteration · 3 lessons
3

Redraw, then change

Computing 9: Topic 1.2, pp. 25–45
GoalTurn a FOR loop into a flowchart, change a loop in both forms, and meet a loop whose end value is typed in.
New words
limit · the end value of a loop, when it is a variable that the user types, so the number of repeats depends on the datazero repeats · a loop whose first test fails, so its body never runsmodify · to change a loop, for example its start value, end value or body
Explanation

To turn a FOR loop into a flowchart, follow five steps. First, set the counter to the start value in a process box. Second, draw a diamond that tests the counter against the end value. Third, put the body inside the loop. Fourth, add a process box that adds 1 to the counter, with an arrow back to the diamond. Fifth, add the lines that come before and after the loop. Check by tracing both versions with the same values. To modify a loop, change the start box and the end test to change the numbers, or the body to change what is done. Sometimes the end value is a variable that the user types, so the number of repeats depends on the data. FOR n ← 1 TO limit runs limit times. If the user types 0, the first test already fails, so the body never runs and the program goes straight to the line after NEXT. Test a loop like this with a small value, a large value and 0.

Pseudocode
1FOR bell ← 1 TO 3
2    OUTPUT "Ring ", bell
3NEXT bell
Worked examples
Startbell ← 1bell <= 3?yesOUTPUT "Ring ", bellbell ← bell + 1noEnd
This is the flowchart for the loop in the picture above. The counter bell is set to 1, the diamond tests bell <= 3 and the body is followed by the increment. The output is Ring 1, Ring 2 and Ring 3.
WishPseudocode editFlowchart edit
start at ring 2FOR bell ← 2 TO 3first box bell ← 2
five ringsFOR bell ← 1 TO 5diamond bell <= 5?
show DingOUTPUT "Ding ", belloutput box OUTPUT "Ding ", bell
Changing the loop in both forms. Each wish needs the same idea in the pseudocode and in the flowchart. Notice that the ‘five rings’ wish is a change to only one number in each.
Pseudocode
1INPUT candles
2FOR n ← 1 TO candles
3    OUTPUT "Candle ", n
4NEXT n
5OUTPUT "Cake ready"
A loop whose end value is typed. With 3 typed, the output is Candle 1, Candle 2, Candle 3 and then Cake ready. With 1 typed it is Candle 1 and Cake ready. With 0 typed, the loop runs zero times and only Cake ready is shown. The number of repeats depends on the data typed.
Class activity

Loop makeover (no computer). Write a four-line FOR loop on a card, for example FOR step ← 1 TO 4 with a message in the body. Hand it to a partner, who changes one thing (start value, end value or body) and predicts the output before tracing it. Then draw the flowchart of the new loop together. Finally, write FOR n ← 1 TO limit on a card and make six number cards, such as 0, 1, 4 and 9. Turn over a number card and say how many times the loop runs, and what the last value of n is (if it runs at all).

Practice
1
Draw a flowchart for this loop and trace it. What is output?
Pseudocode
1FOR stamp ← 1 TO 4
2    OUTPUT "Stamp ", stamp * 2
3NEXT stamp
2
This flowchart outputs 3, 4, 5 and 6. Change it so that it outputs 6, 7, 8 and 9. Find two different ways to do it.
Startn ← 1n <= 4?yesOUTPUT n + 2n ← n + 1noEnd
3
How many times does each loop run? Write the number.
Loop headerValue typed for limitRuns
(a) FOR i ← 1 TO limit6?
(b) FOR i ← 0 TO limit6?
(c) FOR i ← 3 TO limit5?
(d) FOR i ← 1 TO limit0?
4
Follow the algorithm with the inputs 4, then 6, then 8. What is output each time?
Pseudocode
1INPUT start
2FOR n ← start TO 6
3    OUTPUT n * 2
4NEXT n