Counting in a loop (7a)

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

Counting in a loop (7a)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where should a counter be set to 0?
A Inside the loop, at the start of each repeat
B Before the loop starts
2. Which line adds 1 to a counter called Count?
A Count ← Count + 1
B Count + 1 ← Count
3. A program counts how many marks entered are 50 or more. Where does the counting line go?
A Outside the IF, so it runs for every mark
B Inside an IF Mark >= 50 THEN
4. A counter is never set to 0 before the loop. What is the problem?
A It starts with an unknown value, so the final count may be wrong
B The loop will run one extra time
5. Which statement about a counter is true?
A It goes up by 1 each time the thing being counted happens
B It adds the value the user typed in each time

Match the Pairs

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

Left columnRight column
1Counter ← 0 ACounter ← Counter + 1 is outside the IF, so it counts every number entered, not only the numbers greater than 5.
2Counter ← Counter + 1 BAdds 1 to the counter each time it runs, so the counter goes up by one
3Count only the marks that are 50 or more by putting the increase inside an IF that is true only for those marks CInitialises the counter to zero before the loop starts, so counting begins from nothing
4Count only the valid entries by increasing the counter after a check shows the input is within the allowed range DCounter ← 0 is inside the loop, so the counter resets to zero on every repeat and the count is lost. It must be set before the loop starts.
5FOR Index ← 1 TO 10 Counter ← 0 INPUT Number IF Number > 5 THEN Counter ← Counter + 1 ENDIF NEXT Index EIF Age >= 11 AND Age <= 16 THEN ValidCount ← ValidCount + 1 ENDIF
6Counter ← 0 FOR Index ← 1 TO 10 INPUT Number Counter ← Counter + 1 IF Number > 5 THEN OUTPUT Number ENDIF NEXT Index FIF Mark >= 50 THEN Passes ← Passes + 1 ENDIF

Definitions

Counter ← Counter + 1

Fill in the Blanks

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

1.
Counter ← _ FOR Index ← 1 TO 10 INPUT Num
2.
IF Mark >= 50 THEN Passes ← Passes _ 1 ENDIF
3.
IF Score > 90 THEN Top ← ___ + 1 ENDIF
4.
__ Age >= 18 THEN Adults ← Adults + 1 ENDIF
5.
IF Num >= 1 ___ Num <= 10 THEN Valid ← Valid + 1 ENDIF

Spot the Error

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

Question 1
A Counter ← 0
B FOR Index ← 1 TO 5
C Counter ← Counter - 1
D NEXT Index
E OUTPUT Counter
Question 2
A DECLARE Counter : INTEGER
B Counter ← 1
C FOR Index ← 1 TO 10
D Counter ← Counter + 1
E NEXT Index
F OUTPUT Counter
Question 3
A Count ← 0
B INPUT Score
C WHILE Score <> -1 DO
D Counter ← Counter + 1
E INPUT Score
F ENDWHILE
Question 4
A Counter ← 0
B FOR Index ← 1 TO 20
C IF Index MOD 2 ← 0 THEN
D Counter ← Counter + 1
E ENDIF
F NEXT Index
Question 5
A DECLARE Attempts : INTEGER
B Attempts = 0
C REPEAT
D INPUT Age
E Attempts ← Attempts + 1
F UNTIL Age >= 0 AND Age <= 120

Answer Key

Counting in a loop (7a)
Section A: Quick Questions
Q1
B — Before the loop starts
Q2
A — Count ← Count + 1
Q3
B — Inside an IF Mark >= 50 THEN
Q4
A — It starts with an unknown value, so the final count may be wrong
Q5
A — It goes up by 1 each time the thing being counted happens
Section B: Match the Pairs
Q1
Counter ← 0 → Initialises the counter to zero before the loop starts, so counting begins from nothing
Q2
Counter ← Counter + 1 → Adds 1 to the counter each time it runs, so the counter goes up by one
Q3
Count only the marks that are 50 or more by putting the increase inside an IF that is true only for those marks → IF Mark >= 50 THEN Passes ← Passes + 1 ENDIF
Q4
Count only the valid entries by increasing the counter after a check shows the input is within the allowed range → IF Age >= 11 AND Age <= 16 THEN ValidCount ← ValidCount + 1 ENDIF
Q5
FOR Index ← 1 TO 10 Counter ← 0 INPUT Number IF Number > 5 THEN Counter ← Counter + 1 ENDIF NEXT Index → Counter ← 0 is inside the loop, so the counter resets to zero on every repeat and the count is lost. It must be set before the loop starts.
Q6
Counter ← 0 FOR Index ← 1 TO 10 INPUT Number Counter ← Counter + 1 IF Number > 5 THEN OUTPUT Number ENDIF NEXT Index → Counter ← Counter + 1 is outside the IF, so it counts every number entered, not only the numbers greater than 5.
Section C: Definitions
Q1
Counter ← Counter + 1 — A counter is a variable set to 0 before a loop, then increased by 1 with Counter ← Counter + 1 each time the thing being counted happens.
Section D: Fill in the Blanks
Q1
Counter ← 0 FOR Index ← 1 TO 10 INPUT Num
Q2
IF Mark >= 50 THEN Passes ← Passes + 1 ENDIF
Q3
IF Score > 90 THEN Top ← Top + 1 ENDIF
Q4
IF Age >= 18 THEN Adults ← Adults + 1 ENDIF
Q5
IF Num >= 1 AND Num <= 10 THEN Valid ← Valid + 1 ENDIF
Section E: Spot the Error
Q1
C — A counter goes up by 1 each time, so it needs Counter ← Counter + 1. Subtracting 1 makes the counter go down instead of counting.
Q2
B — A counter must be set to 0 before the loop starts. Starting at 1 makes the final count one too high. It should be Counter ← 0.
Q3
A — The variable initialised must have the same name as the counter used in the loop. Count is a different variable from Counter, so Counter is never set to 0. It should be Counter ← 0.
Q4
C — A condition compares values, so it must use the relational operator = and not the assignment arrow ←. It should be IF Index MOD 2 = 0 THEN.
Q5
B — Setting the starting value of the counter needs the assignment arrow, not the comparison =. With = the counter is never given the value 0. It should be Attempts ← 0.