Counting in a loop (8.1b)

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

Counting in a loop (8.1b)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where should Count <- 0 be placed when counting in a loop?
A Before the loop starts
B Inside the loop, at the top
2. Which statement adds 1 to a counter?
A Count + 1 <- Count
B Count <- Count + 1
3. A loop runs 5 times and contains Count <- Count + 1 (not inside an IF). Count started at 0. What is Count at the end?
A 1
B 5
4. To count only the marks that are 50 or more, where does Count <- Count + 1 go?
A Inside an IF Mark >= 50 THEN
B Outside the IF, directly in the loop
5. Which loop body counts only the values that are greater than 10?
A IF Num > 10 THEN Count <- Count + 1 ENDIF
B Count <- Count + 1 IF Num > 10 THEN ENDIF

Match the Pairs

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

Left columnRight column
1Count <- 0 AAdds 1 to the counter each time the thing being counted happens
2Count <- Count + 1 BIF Mark >= 50 THEN Count <- Count + 1 ENDIF
3To count only the marks that are 50 or more, put the counter increase inside an IF inside the loop CCount is set to 0 inside the loop, so it resets on every repeat and never counts beyond 1. Count <- 0 must go before the loop.
4To count every repeat of a loop, the counter increase goes in the loop with no IF DThe counter increase is outside any IF, so it counts every repeat instead of only values that meet a condition. It should be placed inside an IF.
5FOR N <- 1 TO 10 Count <- 0 Count <- Count + 1 NEXT N EFOR N <- 1 TO 10 Count <- Count + 1 NEXT N
6Count <- 0 WHILE Count < 5 DO Count <- Count + 1 ENDWHILE FSets the counter to zero before the loop starts, so counting begins from nothing

Definitions

Count <- Count + 1

Fill in the Blanks

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

1.
Count <- _ FOR Num <- 1 TO 10 Count <- Count + 1
2.
Count <- 0 WHILE Num < 10 DO Count <- Count _ 1
3.
FOR Num <- 1 TO 10 __ Num > 5 THEN Count <- Count + 1
4.
INPUT Mark IF Mark __ 50 THEN Count <- Count + 1
5.
IF Score = 100 THEN Count <- _____ + 1 ENDIF

Spot the Error

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

Question 1
A DECLARE Count : INTEGER
B Count <- 1
C FOR i <- 1 TO 5
D Count <- Count + 1
E NEXT i
F OUTPUT Count
Question 2
A Count = 0
B FOR i <- 1 TO 8
C Count <- Count + 1
D NEXT i
E OUTPUT Count
Question 3
A Count <- 0
B FOR i <- 1 UNTIL 10
C IF Mark[i] >= 50 THEN
D Count <- Count + 1
E ENDIF
F NEXT i
Question 4
A Count <- 0
B WHILE Count < 5 THEN
C INPUT Num
D Count <- Count + 1
E ENDWHILE
F OUTPUT Count
Question 5
A Count <- 0
B FOR i <- 1 TO 20
C Count <- Count + i
D NEXT i
E OUTPUT Count

Answer Key

Counting in a loop (8.1b)
Section A: Quick Questions
Q1
A — Before the loop starts
Q2
B — Count <- Count + 1
Q3
B — 5
Q4
A — Inside an IF Mark >= 50 THEN
Q5
A — IF Num > 10 THEN Count <- Count + 1 ENDIF
Section B: Match the Pairs
Q1
Count <- 0 → Sets the counter to zero before the loop starts, so counting begins from nothing
Q2
Count <- Count + 1 → Adds 1 to the counter each time the thing being counted happens
Q3
To count only the marks that are 50 or more, put the counter increase inside an IF inside the loop → IF Mark >= 50 THEN Count <- Count + 1 ENDIF
Q4
To count every repeat of a loop, the counter increase goes in the loop with no IF → FOR N <- 1 TO 10 Count <- Count + 1 NEXT N
Q5
FOR N <- 1 TO 10 Count <- 0 Count <- Count + 1 NEXT N → Count is set to 0 inside the loop, so it resets on every repeat and never counts beyond 1. Count <- 0 must go before the loop.
Q6
Count <- 0 WHILE Count < 5 DO Count <- Count + 1 ENDWHILE → The counter increase is outside any IF, so it counts every repeat instead of only values that meet a condition. It should be placed inside an IF.
Section C: Definitions
Q1
Count <- Count + 1 — Adds 1 to the counter each time the thing being counted happens. Set Count <- 0 before the loop, and put this inside the loop (inside an IF to count only some values).
Section D: Fill in the Blanks
Q1
Count <- 0 FOR Num <- 1 TO 10 Count <- Count + 1
Q2
Count <- 0 WHILE Num < 10 DO Count <- Count + 1
Q3
FOR Num <- 1 TO 10 IF Num > 5 THEN Count <- Count + 1
Q4
INPUT Mark IF Mark >= 50 THEN Count <- Count + 1
Q5
IF Score = 100 THEN Count <- Count + 1 ENDIF
Section E: Spot the Error
Q1
B — The counter must start at 0 before the loop. Starting at 1 makes the final count one too high. It should be Count <- 0.
Q2
A — Count = 0 is a comparison, not an assignment. To set the counter to 0 you must write Count <- 0.
Q3
B — A FOR loop uses the keyword TO for its end value, not UNTIL. It should be FOR i <- 1 TO 10.
Q4
B — A WHILE loop header ends with DO, not THEN (THEN belongs to IF). It should be WHILE Count < 5 DO.
Q5
C — Adding i makes a running total, not a count. A counter must add exactly 1 each time: Count <- Count + 1.