FOR ... TO ... NEXT

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

FOR ... TO ... NEXT

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which FOR loop runs its statements exactly 30 times?
A FOR Index ← 1 TO 30
B FOR Index ← 1 TO 29
2. Which line correctly ends a loop that starts with FOR Count ← 1 TO 10?
A NEXT Count
B ENDFOR Count
3. The loop variable Index in FOR Index ← 1 TO 30 changes ...
A only when you write Index ← Index + 1 inside the loop
B automatically each time round the loop
4. What data type should the loop variable in a FOR loop be?
A INTEGER
B STRING
5. Which pair of lines is written correctly?
A FOR Index ← 1 TO 5 ... NEXT Count
B FOR Index ← 1 TO 5 ... NEXT Index

Match the Pairs

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

Left columnRight column
1FOR Index ← 1 TO 30 ANEXT uses a different identifier from the FOR line. It must repeat the same loop variable, so it should be NEXT Index.
2NEXT Index BStarts a loop that repeats the statements below once for each whole number from 1 to 30 inclusive
3The loop variable changes by itself, so you never write a line to add 1 to it CThe loop variable is given its start value with = instead of the assignment arrow. It should be FOR Index ← 1 TO 30.
4The loop variable is an INTEGER, so it must be declared as one before the loop DFOR Count ← 1 TO 5 OUTPUT Count NEXT Count
5FOR Index ← 1 TO 30 OUTPUT Index NEXT Count EDECLARE Index : INTEGER FOR Index ← 1 TO 30 OUTPUT Index NEXT Index
6FOR Index = 1 TO 30 OUTPUT Index NEXT Index FEnds the FOR loop and names the same loop variable that the FOR line used

Definitions

FOR Index ← 1 TO 30

Fill in the Blanks

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

1.
___ Index ← 1 TO 30 OUTPUT Index NEXT Index
2.
FOR Index ← 1 __ 30 OUTPUT Index NEXT Index
3.
FOR Number ← 1 TO 10 OUTPUT Number * 2 ____ Number
4.
FOR Count ← 1 TO 5 OUTPUT Count NEXT _____
5.
FOR Index _ 1 TO 30 OUTPUT Index NEXT Index

Spot the Error

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

Question 1
A Total ← 0
B FOR Index = 1 TO 30
C Total ← Total + Index
D NEXT Index
Question 2
A FOR Count ← 1 TO 5
B OUTPUT "Hello"
C OUTPUT Count
D NEXT Counter
Question 3
A Sum ← 0
B FOR Num ← 1 UNTIL 20
C Sum ← Sum + Num
D NEXT Num
Question 4
A DECLARE Index : INTEGER
B FOR Index ← 1.5 TO 30
C OUTPUT Index
D NEXT Index
Question 5
A Total ← 0
B FOR Index ← 1 TO 10
C Total ← Total + Number
D NEXT Index
E OUTPUT Total

Answer Key

FOR ... TO ... NEXT
Section A: Quick Questions
Q1
A — FOR Index ← 1 TO 30
Q2
A — NEXT Count
Q3
B — automatically each time round the loop
Q4
A — INTEGER
Q5
B — FOR Index ← 1 TO 5 ... NEXT Index
Section B: Match the Pairs
Q1
FOR Index ← 1 TO 30 → Starts a loop that repeats the statements below once for each whole number from 1 to 30 inclusive
Q2
NEXT Index → Ends the FOR loop and names the same loop variable that the FOR line used
Q3
The loop variable changes by itself, so you never write a line to add 1 to it → FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
Q4
The loop variable is an INTEGER, so it must be declared as one before the loop → DECLARE Index : INTEGER FOR Index ← 1 TO 30 OUTPUT Index NEXT Index
Q5
FOR Index ← 1 TO 30 OUTPUT Index NEXT Count → NEXT uses a different identifier from the FOR line. It must repeat the same loop variable, so it should be NEXT Index.
Q6
FOR Index = 1 TO 30 OUTPUT Index NEXT Index → The loop variable is given its start value with = instead of the assignment arrow. It should be FOR Index ← 1 TO 30.
Section C: Definitions
Q1
FOR Index ← 1 TO 30 — Repeats the statements below once for each whole number from 1 to 30 inclusive. Index is an INTEGER that changes automatically each time. Close the loop with NEXT Index.
Section D: Fill in the Blanks
Q1
FOR Index ← 1 TO 30 OUTPUT Index NEXT Index
Q2
FOR Index ← 1 TO 30 OUTPUT Index NEXT Index
Q3
FOR Number ← 1 TO 10 OUTPUT Number * 2 NEXT Number
Q4
FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
Q5
FOR Index ← 1 TO 30 OUTPUT Index NEXT Index
Section E: Spot the Error
Q1
B — A FOR loop gives its loop variable a starting value with the assignment arrow ←, not =. It should be FOR Index ← 1 TO 30.
Q2
D — NEXT must repeat the same identifier that follows FOR. The loop variable is Count, so the line should be NEXT Count.
Q3
B — The end value of a FOR loop is introduced with TO, not UNTIL. UNTIL belongs to REPEAT loops. It should be FOR Num ← 1 TO 20.
Q4
B — The loop variable is an INTEGER, so it must start at a whole number. 1.5 is a REAL. It should be FOR Index ← 1 TO 30.
Q5
C — Number has never been set. The loop variable is Index, which changes automatically each time round, so the line should be Total ← Total + Index.