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 32 OUTPUT "Ring ", bell
3NEXT bell
Worked examples
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.
Wish
Pseudocode edit
Flowchart edit
start at ring 2
FOR bell ← 2 TO 3
first box bell ← 2
five rings
FOR bell ← 1 TO 5
diamond bell <= 5?
show Ding
OUTPUT "Ding ", bell
output 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?
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.
Way 1: change the body to OUTPUT n + 5 (n is 1 to 4 and the output is 6 to 9). Way 2: start with n ← 4 and change the test to n <= 7 (the output of n + 2 is 6 to 9).
3
How many times does each loop run? Write the number.
Loop header
Value typed for limit
Runs
(a) FOR i ← 1 TO limit
6
?
(b) FOR i ← 0 TO limit
6
?
(c) FOR i ← 3 TO limit
5
?
(d) FOR i ← 1 TO limit
0
?
(a) 6 times (1 to 6). (b) 7 times (0 to 6 includes 0). (c) 3 times (3, 4, 5). (d) 0 times: the first test fails at once, so the body is skipped.
4
Follow the algorithm with the inputs 4, then 6, then 8. What is output each time?
Pseudocode
1INPUT start
2FOR n ← start TO 63 OUTPUT n * 24NEXT n
4: n is 4, 5 and 6, so the output is 8, 10 and 12. 6: n is 6 only, so the output is 12. 8: the start value 8 is already past 6, so the loop runs zero times and nothing is output.