Totalling in a loop (8.1b)

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

Totalling in a loop (8.1b)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where should Total <- 0 be written when totalling values in a loop?
A Inside the loop, as the first line
B Before the loop starts
2. Which line adds the new value Number to the running total?
A Total <- Total + Number
B Number <- Total + Number
3. A student forgets to set Total <- 0 before the loop. What is the problem?
A Total has no starting value, so the answer may be wrong
B The loop will run one time too many
4. Which FOR loop correctly totals 5 numbers typed in by the user?
A Total <- 0 / FOR Count <- 1 TO 5 / INPUT Number / Total <- Total + Number / NEXT Count
B FOR Count <- 1 TO 5 / INPUT Number / Total <- 0 / Total <- Total + Number / NEXT Count
5. Where should OUTPUT Total be placed to show the final total of all the values?
A Inside the loop, before the total is added
B After the loop (after NEXT or ENDWHILE)

Match the Pairs

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

Left columnRight column
1Total <- 0 ATotal <- 0 INPUT Number WHILE Number <> -1 DO Total <- Total + Number INPUT Number ENDWHILE OUTPUT Total
2Total <- Total + Number BTotal <- 0 is inside the loop, so the total is reset to zero on every repeat and only the last number is kept. It should be placed before the loop.
3Totalling ten values typed in by the user: set the total to 0, then add each input inside a FOR loop CAdds the new value to the running total and stores the result back in Total each time round the loop
4Totalling until a rogue value is entered, then showing the total after the loop has finished DTotal <- Number replaces the total with the latest value instead of adding to it. It should be Total <- Total + Number.
5FOR Count <- 1 TO 5 Total <- 0 INPUT Number Total <- Total + Number NEXT Count ETotal <- 0 FOR Count <- 1 TO 10 INPUT Number Total <- Total + Number NEXT Count
6Total <- 0 FOR Count <- 1 TO 5 INPUT Number Total <- Number NEXT Count FSets the running total to zero before the loop starts, so the adding begins from a known value

Definitions

Total <- Total + Number

Fill in the Blanks

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

1.
Total <- _ FOR Count <- 1 TO 5 INPUT Number
2.
FOR Count <- 1 TO 5 Total <- Total + ______ NEXT Count
3.
INPUT Number Total <- _____ + Number NEXT Count
4.
_____ Number <> -1 DO Total <- Total + Number INPUT Number
5.
NEXT Count OUTPUT "Sum: ", _____

Spot the Error

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

Question 1
A Total <- 0
B FOR Count <- 1 TO 5
C INPUT Number
D Total <- Number
E NEXT Count
F OUTPUT Total
Question 2
A Total <- 0
B FOR Count <- 1 UNTIL 3
C INPUT Number
D Total <- Total + Number
E NEXT Count
F OUTPUT Total
Question 3
A DECLARE Total : INTEGER
B Total <- 1
C FOR Count <- 1 TO 5
D INPUT Number
E Total <- Total + Number
F NEXT Count
Question 4
A Total <- 0
B INPUT Number
C WHILE Number <> -1 DO
D Total + Number <- Total
E INPUT Number
F ENDWHILE
Question 5
A Total <- 0
B WHILE
C INPUT Number
D Total <- Total + Number
E UNTIL Total >= 100
F OUTPUT Total

Answer Key

Totalling in a loop (8.1b)
Section A: Quick Questions
Q1
B — Before the loop starts
Q2
A — Total <- Total + Number
Q3
A — Total has no starting value, so the answer may be wrong
Q4
A — Total <- 0 / FOR Count <- 1 TO 5 / INPUT Number / Total <- Total + Number / NEXT Count
Q5
B — After the loop (after NEXT or ENDWHILE)
Section B: Match the Pairs
Q1
Total <- 0 → Sets the running total to zero before the loop starts, so the adding begins from a known value
Q2
Total <- Total + Number → Adds the new value to the running total and stores the result back in Total each time round the loop
Q3
Totalling ten values typed in by the user: set the total to 0, then add each input inside a FOR loop → Total <- 0 FOR Count <- 1 TO 10 INPUT Number Total <- Total + Number NEXT Count
Q4
Totalling until a rogue value is entered, then showing the total after the loop has finished → Total <- 0 INPUT Number WHILE Number <> -1 DO Total <- Total + Number INPUT Number ENDWHILE OUTPUT Total
Q5
FOR Count <- 1 TO 5 Total <- 0 INPUT Number Total <- Total + Number NEXT Count → Total <- 0 is inside the loop, so the total is reset to zero on every repeat and only the last number is kept. It should be placed before the loop.
Q6
Total <- 0 FOR Count <- 1 TO 5 INPUT Number Total <- Number NEXT Count → Total <- Number replaces the total with the latest value instead of adding to it. It should be Total <- Total + Number.
Section C: Definitions
Q1
Total <- Total + Number — Adds each new value to the running total inside a loop. Set Total <- 0 before the loop starts, then OUTPUT Total after the loop ends.
Section D: Fill in the Blanks
Q1
Total <- 0 FOR Count <- 1 TO 5 INPUT Number
Q2
FOR Count <- 1 TO 5 Total <- Total + Number NEXT Count
Q3
INPUT Number Total <- Total + Number NEXT Count
Q4
WHILE Number <> -1 DO Total <- Total + Number INPUT Number
Q5
NEXT Count OUTPUT "Sum: ", Total
Section E: Spot the Error
Q1
D — Total <- Number replaces the total with the latest value, so earlier values are lost. It should be Total <- Total + Number to add each value to the running total.
Q2
B — A FOR loop uses the keyword TO for its end value, not UNTIL. It should be FOR Count <- 1 TO 3.
Q3
B — The total must start at 0 before the loop. Starting at 1 makes every final total one too big. It should be Total <- 0.
Q4
D — The assignment is the wrong way round. The variable being updated goes on the left of <-. It should be Total <- Total + Number.
Q5
B — A loop that ends with UNTIL must begin with REPEAT, not WHILE. Line 2 should be REPEAT.