FOR ... TO ... NEXT loop (7a)

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

FOR ... TO ... NEXT loop (7a)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. How many times does the body of FOR Count ← 1 TO 5 run?
A 5 times
B 4 times
2. Which line correctly ends the loop that starts with FOR Index ← 1 TO 30?
A ENDFOR Index
B NEXT Index
3. How many times does FOR Num ← 4 TO 4 run?
A Once
B Never
4. What happens when FOR Num ← 10 TO 1 is run?
A The body does not run at all
B It counts down from 10 to 1
5. Which data type must the loop variable in a FOR loop have?
A STRING
B INTEGER

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 AFOR Index ← 10 TO 1
2FOR Count ← 5 TO 5 BRuns the loop body exactly once, because the start value and end value are equal
3A FOR loop must be closed with NEXT followed by the same loop variable CFOR Index ← 1 TO 30 OUTPUT Index NEXT Index
4The loop does not run at all if the start value is bigger than the end value DA FOR loop must end with NEXT Index, not ENDFOR. ENDFOR is not Cambridge pseudocode.
5FOR Index ← 1 TO 30 OUTPUT Index ENDFOR ERepeats the statements below once for each integer from 1 to 30 inclusive, ending at NEXT Index
6FOR Index ← 1 TO 30 OUTPUT Index NEXT Count FThe NEXT line uses a different identifier (Count) from the loop variable (Index).

Definitions

FOR Index ← 1 TO 30

Fill in the Blanks

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

1.
FOR Index ← 1 __ 30 OUTPUT Index NEXT Index
2.
FOR Index ← 1 TO 30 OUTPUT Index ____ Index
3.
FOR Index _ 1 TO 30 OUTPUT Index NEXT Index
4.
DECLARE Index : _______ FOR Index ← 1 TO 30 NEXT Index
5.
FOR Index ← 1 TO _ OUTPUT "Hello" NEXT Index

Spot the Error

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

Question 1
A DECLARE Index : INTEGER
B FOR Index ← 1 TO 3
C OUTPUT Index
D ENDFOR
Question 2
A FOR Index ← 1 UNTIL 10
B OUTPUT Index * 2
C NEXT Index
Question 3
A DECLARE Total : INTEGER
B Total ← 0
C FOR Index = 1 TO 4
D Total ← Total + Index
E NEXT Index
Question 4
A DECLARE Index : INTEGER
B OUTPUT "Start"
C FOR Index ← 1.5 TO 5
D OUTPUT Index
E NEXT Index
Question 5
A DECLARE Num : INTEGER
B FOR Num ← 1 TO 3
C OUTPUT Index * Index
D NEXT Num

Answer Key

FOR ... TO ... NEXT loop (7a)
Section A: Quick Questions
Q1
A — 5 times
Q2
B — NEXT Index
Q3
A — Once
Q4
A — The body does not run at all
Q5
B — INTEGER
Section B: Match the Pairs
Q1
FOR Index ← 1 TO 30 → Repeats the statements below once for each integer from 1 to 30 inclusive, ending at NEXT Index
Q2
FOR Count ← 5 TO 5 → Runs the loop body exactly once, because the start value and end value are equal
Q3
A FOR loop must be closed with NEXT followed by the same loop variable → FOR Index ← 1 TO 30 OUTPUT Index NEXT Index
Q4
The loop does not run at all if the start value is bigger than the end value → FOR Index ← 10 TO 1
Q5
FOR Index ← 1 TO 30 OUTPUT Index ENDFOR → A FOR loop must end with NEXT Index, not ENDFOR. ENDFOR is not Cambridge pseudocode.
Q6
FOR Index ← 1 TO 30 OUTPUT Index NEXT Count → The NEXT line uses a different identifier (Count) from the loop variable (Index).
Section C: Definitions
Q1
FOR Index ← 1 TO 30 — Repeats the code once for each integer from 1 to 30 inclusive, up to NEXT Index. Runs once if both values are equal and not at all if the start value is bigger.
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 Index ← 1 TO 30 OUTPUT Index NEXT Index
Q4
DECLARE Index : INTEGER FOR Index ← 1 TO 30 NEXT Index
Q5
FOR Index ← 1 TO 1 OUTPUT "Hello" NEXT Index
Section E: Spot the Error
Q1
D — A FOR loop in Cambridge pseudocode ends with NEXT followed by the loop variable, not ENDFOR. It should be NEXT Index.
Q2
A — A FOR loop uses the keyword TO between the start and end values. UNTIL belongs to REPEAT loops. It should be FOR Index ← 1 TO 10.
Q3
C — The loop variable is given its start value with the assignment arrow ←. The = sign is a comparison operator. It should be FOR Index ← 1 TO 4.
Q4
C — The loop variable must be an INTEGER, so the start value cannot be a decimal such as 1.5. It should be FOR Index ← 1 TO 5.
Q5
C — The loop variable is Num, so the body must use Num. Index has not been declared or given a value. It should be OUTPUT Num * Num.