GoalRead a count-controlled loop in a flowchart and find its counter, start value, end test and increment.
New words
flowchart loop · a loop made from a decision diamond and an arrow that goes backincrement · to add 1 (or another fixed amount) to a counter; here we always add 1decision · a diamond that asks a yes or no question
Explanation
There is no loop symbol in a flowchart. A loop is drawn with a decision diamond and an arrow that returns to it. A flowchart loop that repeats a fixed number of times has the same features as a FOR loop. A counter is given its start value in a process box. The diamond tests whether the loop should go on. The boxes inside the loop do the work. A process box adds 1 to the counter, and the arrow goes back to the diamond. In pseudocode the FOR line adds 1 to the counter out of sight, but in a flowchart nothing does it unless you draw the box. The test can be written in two ways. In our drawings the diamond asks ‘keep going?’, so Yes goes round the loop and No leaves it. Some books ask ‘finished?’ and Yes leaves the loop. Both ways give the same loop.
Worked examples
Test number
lap
lap <= 4?
Output
1
1
Yes
100
2
2
Yes
200
3
3
Yes
300
4
4
Yes
400
5
5
No
loop ends
The flowchart above is the same as FOR lap ← 1 TO 4 with OUTPUT lap * 100. Follow it with a table. Each time the diamond is reached, lap is tested. The fifth test is No, so the loop ends. The output is 100, 200, 300 and 400.
Feature
Pseudocode
Flowchart
counter
lap
lap
start value
FOR lap ← 1
lap ← 1
end
TO 4
lap <= 4?
increment
NEXT lap
lap ← lap + 1
body
OUTPUT lap * 100
OUTPUT lap * 100
Matching the two versions. Each feature of the FOR loop has a box or a decision in the flowchart. The FOR loop hides the increment, but the flowchart shows it.
Mistake
What happens
the box lap ← lap + 1 is missing
lap stays 1, lap <= 4 is always Yes: infinite loop
the arrow goes back to above lap ← 1
lap is set to 1 again every time: infinite loop
the test is lap < 4?
the loop stops early: 100, 200, 300 only
Three common mistakes in the flowchart. Each one changes how the loop behaves. Trace with the table to see what goes wrong, but never wait for an infinite loop to finish.
Class activity
Human flowchart (no computer). Draw the boxes and diamond of a flowchart on separate sheets of paper and lay them on the floor with arrows made from tape. One person walks the flowchart carrying a card with the value of the counter, and changes the number on the card when passing the increment box. A second person writes down the output. Try different end values in the diamond. Talk about what would happen if the increment sheet were taken away, but do not walk it for ever.
Practice
1
Follow the flowchart. What is the counter, what is its start value, how many times does the loop run, and what is output?
The counter is box. It starts at 3. The test is box <= 6, so the loop runs for 3, 4, 5 and 6, which is 4 times. The output is 13, 14, 15 and 16.
2
Follow this flowchart when the user types 3, and then when the user types 0. What is output each time?
3: tick is 1, 2 and 3 and then 4 fails the test, so the output is Tick 1, Tick 2, Tick 3 and then Done. 0: 1 <= 0 is No at once, so the loop body never runs and the output is only Done.
3
Change the flowchart in task 1 so that it outputs 13, 14, 15, 16, 17 and 18. Which box do you change?
Change the test in the diamond to box <= 8, so the loop runs for 3 to 8, which is six times. The start box and the output box stay the same.
4
Draw a flowchart for this pseudocode. Say which boxes you need, in order.
Pseudocode
1FOR day ← 1 TO 32 OUTPUT "Day ", day
3NEXT day
Start; day ← 1; diamond day <= 3? Yes: OUTPUT "Day ", day; day ← day + 1; arrow back to the diamond. No: end. The output is Day 1, Day 2, Day 3.