Count-controlled loop: FOR … TO … NEXT

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

Count-controlled loop: FOR … TO … NEXT

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly starts a loop that runs 30 times using Index?
A FOR Index ← 1 TO 30
B FOR Index = 1 TO 30 DO
2. Which line correctly closes the loop that began FOR Index ← 1 TO 30?
A ENDFOR Index
B NEXT Index
3. In FOR Index ← 1 TO 30, how does Index change after each repeat?
A It increases by 1 each time
B It stays at 1 until the loop ends
4. Where do the statements that repeat belong?
A Written after NEXT Index
B Indented between FOR and NEXT
5. Which NEXT line matches FOR Index ← 1 TO 30?
A NEXT Index
B NEXT Count

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 AThe statement inside the loop is not indented. The body should be indented, as in ' OUTPUT Index', so it is clear which lines repeat.
2NEXT Index BCloses the loop, adds 1 to Index and goes back to the FOR line
3The statements inside the loop are indented so you can see which lines repeat on each pass CFOR Index ← 1 TO 30 OUTPUT Index NEXT Index
4A loop that runs 30 times, with Index going up by 1 each time, from 1 to 30 DFOR Index ← 1 TO 30
5FOR Index ← 1 TO 30 OUTPUT Index NEXT Count EStarts a loop that repeats the indented statements once for each value of Index, from 1 up to 30
6FOR Index ← 1 TO 30 OUTPUT Index NEXT Index FNEXT uses a different name (Count) from the loop variable Index. NEXT must repeat the same variable name as the FOR line, so it should be NEXT Index.

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 Scores[Index] ← 0 NEXT Index
2.
FOR Index ← 1 __ 30 OUTPUT Scores[Index] NEXT Index
3.
FOR Index ← 1 TO 30 OUTPUT Scores[Index] NEXT _____
4.
FOR Index ← 1 TO 30 Total ← Total + Scores[Index] ____ Index
5.
FOR Index _ 1 TO 30 Scores[Index] ← 0 NEXT Index

Spot the Error

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

Question 1
A FOR Index ← 1 TO 30
B Score[Count] ← 0
C NEXT Index
Question 2
A FOR Index ← 1 TO 10
B OUTPUT Mark[Index
C NEXT Index
Question 3
A FOR Index ← 1 TO 5
B INPUT Num[Index]
C NEXT Num
Question 4
A Total ← 0
B FOR Index ← 1 TO 30
C Total ← Total + Mark[Index]
D ENDFOR
Question 5
A FOR Index = 1 TO 30
B Mark[Index] ← 0
C NEXT Index

Answer Key

Count-controlled loop: FOR … TO … NEXT
Section A: Quick Questions
Q1
A — FOR Index ← 1 TO 30
Q2
B — NEXT Index
Q3
A — It increases by 1 each time
Q4
B — Indented between FOR and NEXT
Q5
A — NEXT Index
Section B: Match the Pairs
Q1
FOR Index ← 1 TO 30 → Starts a loop that repeats the indented statements once for each value of Index, from 1 up to 30
Q2
NEXT Index → Closes the loop, adds 1 to Index and goes back to the FOR line
Q3
The statements inside the loop are indented so you can see which lines repeat on each pass → FOR Index ← 1 TO 30 OUTPUT Index NEXT Index
Q4
A loop that runs 30 times, with Index going up by 1 each time, from 1 to 30 → FOR Index ← 1 TO 30
Q5
FOR Index ← 1 TO 30 OUTPUT Index NEXT Count → NEXT uses a different name (Count) from the loop variable Index. NEXT must repeat the same variable name as the FOR line, so it should be NEXT Index.
Q6
FOR Index ← 1 TO 30 OUTPUT Index NEXT Index → The statement inside the loop is not indented. The body should be indented, as in ' OUTPUT Index', so it is clear which lines repeat.
Section C: Definitions
Q1
FOR Index ← 1 TO 30 — A count-controlled loop. It repeats the indented statements once for each value of Index, from 1 up to 30, going up by 1 each time. NEXT Index closes the loop.
Section D: Fill in the Blanks
Q1
FOR Index ← 1 TO 30 Scores[Index] ← 0 NEXT Index
Q2
FOR Index ← 1 TO 30 OUTPUT Scores[Index] NEXT Index
Q3
FOR Index ← 1 TO 30 OUTPUT Scores[Index] NEXT Index
Q4
FOR Index ← 1 TO 30 Total ← Total + Scores[Index] NEXT Index
Q5
FOR Index ← 1 TO 30 Scores[Index] ← 0 NEXT Index
Section E: Spot the Error
Q1
B — The array index must use the loop variable Index, which changes each time round. Count is not the loop variable. It should be Score[Index] ← 0.
Q2
B — The closing square bracket is missing. The statement inside the loop should be OUTPUT Mark[Index].
Q3
C — NEXT must repeat the loop variable name from the FOR line. Num is the array, not the loop variable. It should be NEXT Index.
Q4
D — In IGCSE pseudocode a FOR loop is closed with NEXT followed by the loop variable, not ENDFOR. It should be NEXT Index.
Q5
A — The loop variable is given its start value with the assignment arrow ←, not =. It should be FOR Index ← 1 TO 30.