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
2

Loops with arrows

Computing 9: Topic 1.2, pp. 25–45
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.

Startlap ← 1lap <= 4?yesOUTPUT lap * 100lap ← lap + 1noEnd
Worked examples
Test numberlaplap <= 4?Output
11Yes100
22Yes200
33Yes300
44Yes400
55Noloop 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.
FeaturePseudocodeFlowchart
counterlaplap
start valueFOR lap ← 1lap ← 1
endTO 4lap <= 4?
incrementNEXT laplap ← lap + 1
bodyOUTPUT lap * 100OUTPUT 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.
MistakeWhat happens
the box lap ← lap + 1 is missinglap stays 1, lap <= 4 is always Yes: infinite loop
the arrow goes back to above lap ← 1lap 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?
Startbox ← 3box <= 6?yesOUTPUT box + 10box ← box + 1noEnd
2
Follow this flowchart when the user types 3, and then when the user types 0. What is output each time?
StartINPUT limittick ← 1tick <= limit?yesOUTPUT "Tick ", ticktick ← tick + 1noOUTPUT "Done"End
3
Change the flowchart in task 1 so that it outputs 13, 14, 15, 16, 17 and 18. Which box do you change?
4
Draw a flowchart for this pseudocode. Say which boxes you need, in order.
Pseudocode
1FOR day ← 1 TO 3
2    OUTPUT "Day ", day
3NEXT day