STEP in a FOR loop

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

STEP in a FOR loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop counts DOWN from 10 to 1?
A FOR Count <- 10 TO 1
B FOR Count <- 10 TO 1 STEP -1
2. FOR Number <- 0 TO 100 STEP 10 What values does Number take?
A 0, 10, 20, ... 100
B 0, 1, 2, ... 100
3. A FOR loop has no STEP. How much does the counter change each repeat?
A It goes up by 1
B It goes up by 0
4. How many times does this loop repeat? FOR Number <- 0 TO 100 STEP 10
A 11 times
B 10 times
5. Which STEP value makes the counter count down?
A STEP 1
B STEP -1

Match the Pairs

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

Left columnRight column
1FOR Number <- 0 TO 100 STEP 10 ACounts down from 10 to 1, taking away 1 each repeat, giving 10 repeats
2FOR Count <- 10 TO 1 STEP -1 BSTEP must be followed directly by the step value. The extra = sign is not valid in Cambridge pseudocode.
3If STEP is left out, the counter goes up by 1 each repeat CCounts up in tens: 0, 10, 20 and so on up to 100, giving 11 repeats
4A negative STEP is needed when the start value is bigger than the end value DFOR Lap <- 8 TO 2 STEP -2
5FOR Count <- 10 TO 1 OUTPUT Count NEXT Count EFOR Index <- 1 TO 5
6FOR Number <- 0 TO 100 STEP = 10 FThere is no STEP -1, so the counter goes up by 1 by default. The start (10) is already past the end (1), so the loop body never runs.

Definitions

FOR Count <- 10 TO 1 STEP -1

Fill in the Blanks

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

1.
FOR Number <- 0 TO 100 ____ 10 OUTPUT Number NEXT Number
2.
FOR Count <- 10 TO 1 STEP __ OUTPUT Count NEXT Count
3.
// Outputs 0, 20, 40, 60, 80, 100 FOR Number <- 0 TO 100 STEP __ NEXT Number
4.
// Same as having no STEP at all FOR Index <- 1 TO 10 STEP _ NEXT Index
5.
// Repeats 6 times: 0, 10, 20, 30, 40, 50 FOR Count <- 0 TO __ STEP 10 NEXT Count

Spot the Error

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

Question 1
A FOR Count <- 10 TO 1 STEP -1
B OUTPUT Count
C ENDFOR
Question 2
A DECLARE Num : INTEGER
B OUTPUT "Counting down"
C FOR Num <- 10 TO 1
D OUTPUT Num
E NEXT Num
Question 3
A OUTPUT "Evens"
B FOR Num <- 2 TO 10 STEP 2
C OUTPUT Num
D NEXT Count
Question 4
A DECLARE Count : INTEGER
B FOR Count <- 10 TO 0 STEP 2
C OUTPUT Count
D NEXT Count
Question 5
A Total <- 0
B FOR Num <- 0 TO 100 STEP 10
C Total <- Total + Num
D NEXT Num STEP 10
E OUTPUT Total

Answer Key

STEP in a FOR loop
Section A: Quick Questions
Q1
B — FOR Count <- 10 TO 1 STEP -1
Q2
A — 0, 10, 20, ... 100
Q3
A — It goes up by 1
Q4
A — 11 times
Q5
B — STEP -1
Section B: Match the Pairs
Q1
FOR Number <- 0 TO 100 STEP 10 → Counts up in tens: 0, 10, 20 and so on up to 100, giving 11 repeats
Q2
FOR Count <- 10 TO 1 STEP -1 → Counts down from 10 to 1, taking away 1 each repeat, giving 10 repeats
Q3
If STEP is left out, the counter goes up by 1 each repeat → FOR Index <- 1 TO 5
Q4
A negative STEP is needed when the start value is bigger than the end value → FOR Lap <- 8 TO 2 STEP -2
Q5
FOR Count <- 10 TO 1 OUTPUT Count NEXT Count → There is no STEP -1, so the counter goes up by 1 by default. The start (10) is already past the end (1), so the loop body never runs.
Q6
FOR Number <- 0 TO 100 STEP = 10 → STEP must be followed directly by the step value. The extra = sign is not valid in Cambridge pseudocode.
Section C: Definitions
Q1
FOR Count <- 10 TO 1 STEP -1 — STEP sets how much the FOR counter changes each repeat. Without STEP the counter goes up by 1. A negative STEP, such as -1, makes the counter count down.
Section D: Fill in the Blanks
Q1
FOR Number <- 0 TO 100 STEP 10 OUTPUT Number NEXT Number
Q2
FOR Count <- 10 TO 1 STEP -1 OUTPUT Count NEXT Count
Q3
// Outputs 0, 20, 40, 60, 80, 100 FOR Number <- 0 TO 100 STEP 20 NEXT Number
Q4
// Same as having no STEP at all FOR Index <- 1 TO 10 STEP 1 NEXT Index
Q5
// Repeats 6 times: 0, 10, 20, 30, 40, 50 FOR Count <- 0 TO 50 STEP 10 NEXT Count
Section E: Spot the Error
Q1
C — A FOR loop in Cambridge pseudocode ends with NEXT followed by the counter name, not ENDFOR. Line 3 should be NEXT Count.
Q2
C — Without STEP the counter goes up by 1, so it can never count down from 10 to 1 and the loop would not run. It needs STEP -1: FOR Num <- 10 TO 1 STEP -1.
Q3
D — NEXT must name the same counter as the FOR line. The counter is Num, so line 4 should be NEXT Num.
Q4
B — The counter starts at 10 and must go down to 0, so the STEP has to be negative. A positive STEP 2 would move away from 0. It should be STEP -2.
Q5
D — STEP belongs on the FOR line only. The NEXT line just names the counter, so line 4 should be NEXT Num.