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

Build it yourself

Week 4 · 1.3 Creating algorithms · 3 lessons
3

Build a loop

Computing 9: Topic 1.3, pp. 46–79
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 ← 0
2FOR day ← 1 TO 4
3    INPUT sold
4    total ← total + sold
5NEXT day
6OUTPUT "Total sold: ", total
Output
Total sold: 44
Worked examples
daysold typedtotal
(before)0
11212
2719
32039
4544
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.
Starttotal ← 0day ← 1day <= 4?yesINPUT soldtotal ← total + soldday ← day + 1noOUTPUT "Total sold: ", totalEnd
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 ← 0
2FOR meal ← 1 TO 5
3    INPUT stars
4    IF stars >= 4 THEN
5        count ← count + 1
6    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 ← 0
2FOR trip ← 1 TO 3
3    INPUT km
4    total ← total + km
5NEXT trip
6average ← total / 3
7OUTPUT "Average: ", average
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.
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 3
2    total ← 0
3    INPUT weight
4    total ← total + weight
5NEXT box
6OUTPUT total
4
The empty box in the flowchart is missing. What goes in it? What would happen if it was left out?
Startcard ← 1card <= 3?yesOUTPUT "Card ", card?noEnd