FOR loop with STEP (7c)

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

FOR loop with STEP (7c)

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. Which loop outputs only the even numbers 2, 4, 6, 8, 10?
A FOR Num ← 2 TO 10 STEP 2
B FOR Num ← 2 TO 10 STEP 1
3. How many times does FOR N ← 1 TO 10 STEP 3 run?
A 4 times (N is 1, 4, 7, 10)
B 3 times (N is 3, 6, 9)
4. Which statement about STEP is correct?
A STEP can be negative to count down
B STEP must always be a positive number
5. FOR X ← 1 TO 10 STEP 2: what is the last value of X used inside the loop?
A 10
B 9

Match the Pairs

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

Left columnRight column
1FOR Count ← 2 TO 10 STEP 2 AFOR Number ← 3 TO 1 STEP -1 OUTPUT Number NEXT Number
2FOR Count ← 5 TO 1 STEP -1 BThis outputs 1, 3, 5, 7, 9 (odd numbers) not the even numbers. To output even numbers it should start at 2: FOR Count ← 2 TO 10 STEP 2.
3STEP sets how much the loop variable changes each time round, so STEP 2 gives the even numbers 2, 4, 6, 8, 10 CThere is no STEP -1, so the loop counts up by 1 by default. Because 10 is already past 1, the loop body never runs. It should be FOR Count ← 10 TO 1 STEP -1.
4A negative STEP counts down, and the loop ends when the value goes past value2 (below 1 here) DFOR Number ← 2 TO 10 STEP 2 OUTPUT Number NEXT Number
5FOR Count ← 10 TO 1 OUTPUT Count NEXT Count ECounts in twos: Count takes the values 2, 4, 6, 8, 10, so the loop runs 5 times
6FOR Count ← 1 TO 10 STEP 2 OUTPUT Count NEXT Count FCounts down: Count takes the values 5, 4, 3, 2, 1, so the loop runs 5 times

Definitions

STEP

Fill in the Blanks

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

1.
FOR Num ← 2 TO 10 ____ 2 OUTPUT Num NEXT Num
2.
FOR Count ← 10 TO 1 STEP __ OUTPUT Count NEXT Count
3.
FOR Num ← 0 TO 10 STEP _ OUTPUT Num NEXT Num
4.
FOR Num ← 10 TO 0 STEP -2 OUTPUT Num NEXT ___
5.
FOR Count ← 20 __ 0 STEP -5 OUTPUT Count NEXT Count

Spot the Error

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

Question 1
A OUTPUT "Even numbers"
B FOR Number ← 2 TO 10 STEP 2
C OUTPUT Number
D NEXT Count
Question 2
A OUTPUT "Countdown"
B FOR Count ← 5 TO 1 STEP 1
C OUTPUT Count
D NEXT Count
Question 3
A DECLARE Num : INTEGER
B FOR Num ← 10 TO 0 STEP -2
C Num ← Num - 2
D NEXT Num
Question 4
A Total ← 0
B FOR Num ← 2 TO 10 STEP 2
C Total ← Num
D NEXT Num
E OUTPUT Total
Question 5
A DECLARE Odd : INTEGER
B FOR Odd ← 1 TO 9 BY 2
C OUTPUT Odd
D NEXT Odd

Answer Key

FOR loop with STEP (7c)
Section A: Quick Questions
Q1
B — FOR Count ← 10 TO 1 STEP -1
Q2
A — FOR Num ← 2 TO 10 STEP 2
Q3
A — 4 times (N is 1, 4, 7, 10)
Q4
A — STEP can be negative to count down
Q5
B — 9
Section B: Match the Pairs
Q1
FOR Count ← 2 TO 10 STEP 2 → Counts in twos: Count takes the values 2, 4, 6, 8, 10, so the loop runs 5 times
Q2
FOR Count ← 5 TO 1 STEP -1 → Counts down: Count takes the values 5, 4, 3, 2, 1, so the loop runs 5 times
Q3
STEP sets how much the loop variable changes each time round, so STEP 2 gives the even numbers 2, 4, 6, 8, 10 → FOR Number ← 2 TO 10 STEP 2 OUTPUT Number NEXT Number
Q4
A negative STEP counts down, and the loop ends when the value goes past value2 (below 1 here) → FOR Number ← 3 TO 1 STEP -1 OUTPUT Number NEXT Number
Q5
FOR Count ← 10 TO 1 OUTPUT Count NEXT Count → There is no STEP -1, so the loop counts up by 1 by default. Because 10 is already past 1, the loop body never runs. It should be FOR Count ← 10 TO 1 STEP -1.
Q6
FOR Count ← 1 TO 10 STEP 2 OUTPUT Count NEXT Count → This outputs 1, 3, 5, 7, 9 (odd numbers) not the even numbers. To output even numbers it should start at 2: FOR Count ← 2 TO 10 STEP 2.
Section C: Definitions
Q1
STEP — Sets how much the FOR loop variable changes each time round. STEP 2 counts in twos and STEP -1 counts down. The loop stops when the value goes past the end value.
Section D: Fill in the Blanks
Q1
FOR Num ← 2 TO 10 STEP 2 OUTPUT Num NEXT Num
Q2
FOR Count ← 10 TO 1 STEP -1 OUTPUT Count NEXT Count
Q3
FOR Num ← 0 TO 10 STEP 2 OUTPUT Num NEXT Num
Q4
FOR Num ← 10 TO 0 STEP -2 OUTPUT Num NEXT Num
Q5
FOR Count ← 20 TO 0 STEP -5 OUTPUT Count NEXT Count
Section E: Spot the Error
Q1
D — NEXT must name the same loop variable that the FOR line uses. The loop variable is Number, so the line should be NEXT Number.
Q2
B — Counting down from 5 to 1 needs a negative step. STEP 1 counts up, so the value is already past the end value and the loop never runs. It should be STEP -1.
Q3
C — STEP -2 already lowers Num by 2 each time round, so the loop body must not change it by hand. The body should output the value with OUTPUT Num.
Q4
C — This replaces Total with the current value each time round instead of adding to it. To total the even numbers it should be Total ← Total + Num.
Q5
B — The keyword that sets the change in the loop variable is STEP, not BY. The line should be FOR Odd ← 1 TO 9 STEP 2.