Error: variable not set to zero first

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

Error: variable not set to zero first

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program adds up 10 marks in a FOR loop but never sets Total to 0 first. What is the problem?
A Total starts from an unknown value, so the final answer may be wrong
B Total will always end as 0 because it was never set
2. Which line correctly prepares a variable to count how many students passed?
A Count = 0
B Count ← 0
3. Where should Total ← 0 be placed in a program that adds numbers in a loop?
A Inside the loop, on the first line of the body
B Before the loop starts
4. FOR Index ← 1 TO 5 / Total ← 0 / Total ← Total + Number / NEXT Index. What happens?
A Total is reset to 0 each repeat, so it only holds the last number
B Total correctly holds the sum of all five numbers
5. A program counts how many scores are above 50, but Count ← 0 is missing before the loop. Which statement is true?
A Count may start at an unknown value, so the final count could be wrong
B Count will start at 1 automatically, so only a small error occurs

Match the Pairs

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

Left columnRight column
1Total ← 0 ASets the total to 0 before the loop so it starts from a known value
2Count ← 0 BCount ← 0 WHILE Count < 10 DO Count ← Count + 1 ENDWHILE
3Correct: set the total to 0 once, before the loop starts CTotal ← 0 FOR Index ← 1 TO 5 INPUT Num Total ← Total + Num NEXT Index
4Correct: set the counter to 0 before the loop so it is not reset each time DSets the counter to 0 before the loop so it counts from zero
5FOR Index ← 1 TO 5 INPUT Num Total ← Total + Num NEXT Index ETotal is never set to 0 before the loop, so it starts from an unknown value. Fix: add Total ← 0 before the FOR line.
6FOR Index ← 1 TO 5 Count ← 0 Count ← Count + 1 NEXT Index FCount ← 0 is inside the loop, so it resets every time and always ends as 1. Fix: move Count ← 0 above the loop.

Definitions

Total ← 0

Fill in the Blanks

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

1.
Total ← _ FOR Index ← 1 TO 5
2.
_____ ← 0 WHILE Num <> 0 DO Count ← Count + 1
3.
Total ← 0 ___ Index ← 1 TO 5 Total ← Total + Num
4.
Count ← _ REPEAT Count ← Count + 1
5.
Total _ 0 WHILE Total < 20 DO Total ← Total + 5

Spot the Error

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

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

Answer Key

Error: variable not set to zero first
Section A: Quick Questions
Q1
A — Total starts from an unknown value, so the final answer may be wrong
Q2
B — Count ← 0
Q3
B — Before the loop starts
Q4
A — Total is reset to 0 each repeat, so it only holds the last number
Q5
A — Count may start at an unknown value, so the final count could be wrong
Section B: Match the Pairs
Q1
Total ← 0 → Sets the total to 0 before the loop so it starts from a known value
Q2
Count ← 0 → Sets the counter to 0 before the loop so it counts from zero
Q3
Correct: set the total to 0 once, before the loop starts → Total ← 0 FOR Index ← 1 TO 5 INPUT Num Total ← Total + Num NEXT Index
Q4
Correct: set the counter to 0 before the loop so it is not reset each time → Count ← 0 WHILE Count < 10 DO Count ← Count + 1 ENDWHILE
Q5
FOR Index ← 1 TO 5 INPUT Num Total ← Total + Num NEXT Index → Total is never set to 0 before the loop, so it starts from an unknown value. Fix: add Total ← 0 before the FOR line.
Q6
FOR Index ← 1 TO 5 Count ← 0 Count ← Count + 1 NEXT Index → Count ← 0 is inside the loop, so it resets every time and always ends as 1. Fix: move Count ← 0 above the loop.
Section C: Definitions
Q1
Total ← 0 — Set a total or counter to 0 before the loop. If you forget, it starts from an unknown value; if you put it inside the loop, it resets every time.
Section D: Fill in the Blanks
Q1
Total ← 0 FOR Index ← 1 TO 5
Q2
Count ← 0 WHILE Num <> 0 DO Count ← Count + 1
Q3
Total ← 0 FOR Index ← 1 TO 5 Total ← Total + Num
Q4
Count ← 0 REPEAT Count ← Count + 1
Q5
Total ← 0 WHILE Total < 20 DO Total ← Total + 5
Section E: Spot the Error
Q1
A — Total ← Total does not set the total to 0. It copies an unknown starting value, so every later sum is wrong. It must be Total ← 0 before the loop.
Q2
C — Count is used on the right-hand side before it has been given a value, so it starts from an unknown value. Line 3 must be Count ← 0 to set the counter before the loop.
Q3
B — DECLARE only creates the variable. It does not give Count a value, so it starts from an unknown value. It must be Count ← 0 before the loop.
Q4
D — Using 0 inside the loop resets the total on every repeat, so it only holds the last mark. The loop must add to the running total: Total ← Total + Mark, with Total ← 0 once before the loop.
Q5
C — Resetting the counter inside the loop means Count is always 1, however many numbers are entered. The loop line must be Count ← Count + 1. The 0 belongs before the loop.