Totalling in a loop (7a)

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Totalling in a loop (7a)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where should a running total be set to 0?
A Inside the loop at the start of each repeat
B Before the loop starts
2. Which statement adds the value in Number to a running total?
A Total ← Total + Number
B Total ← Number
3. A loop adds 4, 6 and 10 to a total that started at 0. What does OUTPUT Total show after the loop?
A 20
B 10
4. Which pseudocode correctly totals 5 numbers typed in by the user?
A FOR Count ← 1 TO 5 INPUT Number Total ← Total + Number OUTPUT Total NEXT Count
B Total ← 0 FOR Count ← 1 TO 5 INPUT Number Total ← Total + Number NEXT Count OUTPUT Total
5. When should OUTPUT Total be placed to show the final total only?
A After the loop has finished
B Inside the loop, before the next INPUT

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1Total ← 0 ATotal ← Value replaces the total with the latest value instead of adding to it, so nothing is totalled.
2Total ← Total + Value BFOR Count ← 1 TO 5 INPUT Value Total ← Total + Value NEXT Count
3Add each number the user enters to the running total inside a FOR loop CTotal is set to 0 inside the loop, so it is reset on every repeat and only the last value is kept.
4Output the final total once, after the loop has finished DAdds the current Value to the running total on each repeat of the loop.
5FOR Count ← 1 TO 5 Total ← 0 INPUT Value Total ← Total + Value NEXT Count EInitialises the running total to zero before the loop starts, so no old value is left in it.
6FOR Count ← 1 TO 5 INPUT Value Total ← Value NEXT Count FNEXT Count OUTPUT Total

Definitions

Total ← Total + Value

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
Total ← _ WHILE Count < 5 DO Total ← Total + Value
2.
FOR Count ← 1 TO 5 Total ← _____ + Value NEXT Count
3.
FOR Count ← 1 TO 5 INPUT Value Total ← Total _ Value
4.
Total ← Total + Value NEXT Count ______ Total
5.
INPUT Value Total ← Total + Value ____ Count

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A Total ← 1
B FOR Count ← 1 TO 5
C INPUT Value
D Total ← Total + Value
E NEXT Count
F OUTPUT Total
Question 2
A Total ← 0
B FOR Count ← 1 TO 4
C INPUT Total
D Total ← Total + Value
E NEXT Count
F OUTPUT Total
Question 3
A Total ← 0
B FOR Count = 1 TO 5
C INPUT Mark
D Total ← Total + Mark
E NEXT Count
F OUTPUT Total
Question 4
A Total = 0
B FOR Day ← 1 TO 7
C INPUT Steps
D Total ← Total + Steps
E NEXT Day
F OUTPUT Total
Question 5
A Total ← 0
B REPEAT
C OUTPUT Value
D Total ← Total + Value
E UNTIL Value = 0
F OUTPUT Total

Answer Key

Totalling in a loop (7a)
Section A: Quick Questions
Q1
B — Before the loop starts
Q2
A — Total ← Total + Number
Q3
A — 20
Q4
B — Total ← 0 FOR Count ← 1 TO 5 INPUT Number Total ← Total + Number NEXT Count OUTPUT Total
Q5
A — After the loop has finished
Section B: Match the Pairs
Q1
Total ← 0 → Initialises the running total to zero before the loop starts, so no old value is left in it.
Q2
Total ← Total + Value → Adds the current Value to the running total on each repeat of the loop.
Q3
Add each number the user enters to the running total inside a FOR loop → FOR Count ← 1 TO 5 INPUT Value Total ← Total + Value NEXT Count
Q4
Output the final total once, after the loop has finished → NEXT Count OUTPUT Total
Q5
FOR Count ← 1 TO 5 Total ← 0 INPUT Value Total ← Total + Value NEXT Count → Total is set to 0 inside the loop, so it is reset on every repeat and only the last value is kept.
Q6
FOR Count ← 1 TO 5 INPUT Value Total ← Value NEXT Count → Total ← Value replaces the total with the latest value instead of adding to it, so nothing is totalled.
Section C: Definitions
Q1
Total ← Total + Value — A running total: set Total to 0 before the loop, then add each Value to Total on every repeat. Output Total after the loop ends.
Section D: Fill in the Blanks
Q1
Total ← 0 WHILE Count < 5 DO Total ← Total + Value
Q2
FOR Count ← 1 TO 5 Total ← Total + Value NEXT Count
Q3
FOR Count ← 1 TO 5 INPUT Value Total ← Total + Value
Q4
Total ← Total + Value NEXT Count OUTPUT Total
Q5
INPUT Value Total ← Total + Value NEXT Count
Section E: Spot the Error
Q1
A — A running total must start at 0. Starting at 1 makes the final total 1 too big. It should be Total ← 0.
Q2
C — The input must be stored in Value. Typing into Total overwrites the running total each time. It should be INPUT Value.
Q3
B — A FOR loop sets its counter with the assignment arrow, not =. It should be FOR Count ← 1 TO 5.
Q4
A — Cambridge pseudocode uses the assignment arrow to give a variable a value. It should be Total ← 0.
Q5
C — Each repeat needs to read a new number with INPUT. OUTPUT only displays Value, so nothing new is added. It should be INPUT Value.