GoalBuild a count-controlled loop in a flowchart and in pseudocode, using a running total and a tally.
New words
running total · a variable that adds each new value to the amount already storedtally · a variable that counts how many times something happens, by adding 1 each timeinitialise · to give a variable its starting value before the loop begins
Explanation
A count-controlled loop has a counter variable, a start value, an end value and a way to increase the counter. To build one, decide what must repeat, and then write the lines in this order. Before the loop, give every variable its start value; that is called initialising. Then write the loop and the body. After the loop, write the output. A running total is initialised to 0 before the loop, and inside the loop total ← total + value adds each new value. A tally is also set to 0 first, and an IF inside the loop adds 1 only when the thing you are counting happens. If you set the total to 0 inside the loop, it would start again every time round. Output the total after the loop, unless you want to show it at every step.
Pseudocode
1total ← 02FOR day ← 1 TO 43 INPUT sold
4 total ← total + sold
5NEXT day
6OUTPUT "Total sold: ", total
Output
Total sold: 44
Worked examples
day
sold typed
total
(before)
0
1
12
12
2
7
19
3
20
39
4
5
44
The picture above adds up the tickets sold on four days. The user types 12, 7, 20 and 5. The trace table shows the running total growing, and the output comes after the loop.
The same algorithm as a flowchart. The total and the counter are both set before the loop. Inside the loop there is an input, the total is increased, and the counter is increased. The output comes after the loop. Here the counter is day, as in the pseudocode.
Pseudocode
1count ← 02FOR meal ← 1 TO 53 INPUT stars
4 IF stars >= 4 THEN
5 count ← count + 16 ENDIF
7NEXT meal
8OUTPUT "Happy customers: ", count
A tally. A café asks five customers for a star rating from 1 to 5 and counts how many gave 4 or more. The user types 5, 3, 4, 2 and 4. The tally becomes 1, then stays 1, 2, 2, 3. The output is Happy customers: 3. The IF is inside the loop, because each rating has to be tested.
Class activity
Roll and record (no computer). You are the computer. Roll a die six times, or draw six numbers from 1 to 6 from a bag, putting each back. Keep a trace table with the columns roll, number, total and sixes. After each roll, add the number to the running total and add 1 to the tally if the number is 6. Write ‘total ← 0’ and ‘sixes ← 0’ before you begin. At the end, say the total and the tally. Then write the pseudocode for what you did, using a FOR loop.
Practice
1
Draw a trace table and follow the algorithm when the user types 8, 15 and 4. What is output?
Pseudocode
1total ← 02FOR trip ← 1 TO 33 INPUT km
4 total ← total + km
5NEXT trip
6average ← total / 37OUTPUT "Average: ", average
total becomes 8, then 23, then 27. The average is 27 / 3 = 9, so the output is Average: 9. The division is after the loop, because it needs the final total.
2
Write pseudocode. The user types the number of goals scored in each of 5 matches. Output the total number of goals and the number of matches with no goals.
total ← 0 / zeros ← 0 / FOR match ← 1 TO 5 / INPUT goals / total ← total + goals / IF goals = 0 THEN / zeros ← zeros + 1 / ENDIF / NEXT match / OUTPUT "Total goals: ", total / OUTPUT "Goalless matches: ", zeros. For 3, 0, 2, 4, 1 it gives 10 and 1.
3
The user types 4, 6 and 5. The output should be 15 but it is 5. Find the error and correct it.
Pseudocode
1FOR box ← 1 TO 32 total ← 03 INPUT weight
4 total ← total + weight
5NEXT box
6OUTPUT total
total ← 0 is inside the loop, so the total starts again from 0 each time round, and only the last weight is left. Move total ← 0 to before the FOR line.
4
The empty box in the flowchart is missing. What goes in it? What would happen if it was left out?
The box must be card ← card + 1. Without it, card stays 1, card <= 3 is always Yes, and the loop never ends: an infinite loop.