FOR loop with STEP (7a)

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

FOR loop with STEP (7a)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which FOR loop counts up in twos?
A FOR Count ← 2 TO 10 STEP 2
B FOR Count ← 2 TO 10 STEP 10
2. Which FOR loop counts down from 5 to 1?
A FOR Count ← 5 TO 1 STEP -1
B FOR Count ← 5 TO 1 STEP 1
3. What does STEP do in a FOR loop?
A It sets how many times the loop body is written in the code
B It sets how much the loop variable changes each time
4. What values does FOR X ← 1 TO 9 STEP 3 give?
A 1, 4, 7, 10
B 1, 4, 7
5. A loop starts at 10 and ends at 0. Which STEP is needed?
A STEP -2
B STEP 2

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 ← 10 TO 0 STEP -1
2FOR Count ← 5 TO 1 STEP -1 BThe step is negative, so the value goes down from 1 and never reaches 9. It should be STEP 2 to count up in twos.
3To count down from 10 to 0 in a FOR loop, the step must be negative CCount goes 5, 4, 3, 2, 1 because the negative step makes it decrease by 1 each time
4To output only the even numbers from 2 to 20, the loop uses a step of 2 DFOR Number ← 2 TO 20 STEP 2
5FOR Count ← 10 TO 1 STEP 1 EThe loop is meant to count down, but STEP 1 makes the value go up, so it moves away from the end value. It should use STEP -1.
6FOR Count ← 1 TO 9 STEP -2 FCount goes 2, 4, 6, 8, 10 because the step makes it increase by 2 each time

Definitions

STEP

Fill in the Blanks

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

1.
FOR Count ← 0 TO 10 ____ 2 OUTPUT Count NEXT Count
2.
FOR Count ← 10 TO 1 STEP __ OUTPUT Count NEXT Count
3.
FOR Num ← 2 TO 10 STEP _ OUTPUT Num NEXT Num
4.
FOR Count ← 5 TO 1 STEP -1 OUTPUT Count 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 FOR Count ← 0 TO 10 STEPS 2
B OUTPUT Count
C NEXT Count
Question 2
A OUTPUT "Countdown"
B FOR Num ← 10 TO 1 STEP 1
C OUTPUT Num
D NEXT Num
Question 3
A Total ← 0
B FOR Num ← 2 TO 10 STEP 2
C Total ← Total + Num
D NEXT Total
Question 4
A DECLARE Num : INTEGER
B FOR Num ← 10 TO 0 STEP -2
C OUTPUT Number
D NEXT Num
Question 5
A Total ← 0
B FOR Count ← 1 TO 9 STEP 2
C Total ← Total + STEP
D NEXT Count

Answer Key

FOR loop with STEP (7a)
Section A: Quick Questions
Q1
A — FOR Count ← 2 TO 10 STEP 2
Q2
A — FOR Count ← 5 TO 1 STEP -1
Q3
B — It sets how much the loop variable changes each time
Q4
B — 1, 4, 7
Q5
A — STEP -2
Section B: Match the Pairs
Q1
FOR Count ← 2 TO 10 STEP 2 → Count goes 2, 4, 6, 8, 10 because the step makes it increase by 2 each time
Q2
FOR Count ← 5 TO 1 STEP -1 → Count goes 5, 4, 3, 2, 1 because the negative step makes it decrease by 1 each time
Q3
To count down from 10 to 0 in a FOR loop, the step must be negative → FOR Number ← 10 TO 0 STEP -1
Q4
To output only the even numbers from 2 to 20, the loop uses a step of 2 → FOR Number ← 2 TO 20 STEP 2
Q5
FOR Count ← 10 TO 1 STEP 1 → The loop is meant to count down, but STEP 1 makes the value go up, so it moves away from the end value. It should use STEP -1.
Q6
FOR Count ← 1 TO 9 STEP -2 → The step is negative, so the value goes down from 1 and never reaches 9. It should be STEP 2 to count up in twos.
Section C: Definitions
Q1
STEP — Used in a FOR loop to set how much the loop variable changes each time. STEP 2 counts in twos and STEP -1 counts down. The loop stops when the value goes past the end.
Section D: Fill in the Blanks
Q1
FOR Count ← 0 TO 10 STEP 2 OUTPUT Count NEXT Count
Q2
FOR Count ← 10 TO 1 STEP -1 OUTPUT Count NEXT Count
Q3
FOR Num ← 2 TO 10 STEP 2 OUTPUT Num NEXT Num
Q4
FOR Count ← 5 TO 1 STEP -1 OUTPUT Count NEXT Count
Q5
FOR Count ← 20 TO 0 STEP -5 OUTPUT Count NEXT Count
Section E: Spot the Error
Q1
A — The keyword is STEP, not STEPS. The line should be FOR Count ← 0 TO 10 STEP 2.
Q2
B — To count down from 10 to 1 the step must be negative. With STEP 1 the value starts past the end, so the loop never runs. It should be STEP -1.
Q3
D — NEXT must name the loop variable, which is Num, not Total. The line should be NEXT Num.
Q4
C — The loop variable is Num, so Number is a different identifier that was never declared. The line should be OUTPUT Num.
Q5
C — STEP is a keyword that sets the size of the change. It is not a value you can add. The loop variable Count holds the current value, so it should be Total ← Total + Count.