← IGCSE Computer Science Paper 2

Selection inside a loop (7c)

An IF placed inside a loop body is tested again on every pass, so each pass can take a different branch. It is often used to count items that meet a condition.

Understanding Selection inside a loop (7c) in IGCSE Computer Science Paper 2

What it does: The IF is tested for every element in the array. Passes increases only when the mark meets the condition, so it counts the items that qualify. Rain[Day] has a different value on each pass, so the IF can choose THEN on one pass and ELSE on another. Each pass can therefore output a different message. The ENDIF must close the IF inside the loop body, before NEXT. The IF is fully inside the loop. Count ← Count + 1 adds to the running total. Count ← 1 just resets the value to 1 each time, so it never counts more than one.

Common mistakes: A FOR loop sets its counter with the assignment arrow ←, not =. The line should be FOR Count ← 1 TO 5. Count is set back to 1 each time the IF is true, so it never builds up. To count items it must add 1 each time: Count ← Count + 1.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 176–187) and word search (Modules 166–187).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A FOR loop runs 10 times and has an IF inside its body. How many times is the IF condition tested?
10 times, once on every pass of the loopOnce, the first time the loop starts
Matches
FOR Index ← 1 TO 10 Count ← 0 IF Score[Index] > 80 THEN Count ← Count + 1 ENDIF NEXT Index
↓
Count ← 0 is inside the loop, so the counter is reset on every pass and the final count is wrong. Set it to zero once before the loop.
Applying
Meanings
Term
IF inside a loop
Meaning
An IF placed inside a loop body is tested again on every pass, so each pass can take a different branch. It is often used to count items that meet a condition.
Practice
Pass ← 0
Testing
Gaps
Score[Index] > 50 THEN
Check Answer
Spots
1Pass ← 0
2FOR Count = 1 TO 5

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Blue-Ringed Octopus' badge — Reef Legend, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. A FOR loop runs 10 times and has an IF inside its body. How many times is the IF condition tested?
A. 10 times, once on every pass of the loop ✓
B. Once, the first time the loop starts
The IF is part of the loop body, so it is tested again on every pass. Each pass can take a different branch.
2. Which pseudocode correctly counts the marks that are 50 or more?
A. FOR Index ← 1 TO 10 IF Mark[Index] >= 50 THEN Count ← Count + 1 NEXT Index ENDIF
B. FOR Index ← 1 TO 10 IF Mark[Index] >= 50 THEN Count ← Count + 1 ENDIF NEXT Index ✓
The ENDIF must close the IF inside the loop body, before NEXT. The IF is fully inside the loop.
3. In a loop, where should the counting line Count ← Count + 1 go so that it only counts items meeting the condition?
A. Outside the IF but inside the loop, so it runs on every pass
B. Inside the IF, so it only runs when the condition is true ✓
If the line is outside the IF, it adds 1 on every pass and counts every item, not just the ones that meet the condition.
4. A WHILE loop reads numbers and has an IF inside it. What is true about the IF?
A. It is tested again each time the loop body runs ✓
B. It is only tested once, before the loop starts
An IF inside a WHILE loop body is part of the body. It is checked on every pass, as long as the loop keeps running.
5. Which loop body counts how many Age values are over 17, if Count starts at 0?
A. IF Age[Index] > 17 THEN Count ← Count + 1 ENDIF ✓
B. IF Age[Index] > 17 THEN Count ← 1 ENDIF
Count ← Count + 1 adds to the running total. Count ← 1 just resets the value to 1 each time, so it never counts more than one.

Match the Pairs

FOR Count ← 1 TO 10 IF Count MOD 2 = 0 THEN OUTPUT Count ENDIF NEXT Count → The IF is tested on every pass of the FOR loop, so only the even numbers are output
WHILE Number <> 0 DO IF Number > 50 THEN Total ← Total + 1 ENDIF ENDWHILE → On each pass of the WHILE loop the IF checks the condition and adds 1 to the counter only when it is true
To count how many marks are 50 or more, put an IF inside the loop that adds 1 to a counter when the condition is true → FOR Index ← 1 TO 30 IF Mark[Index] >= 50 THEN Passes ← Passes + 1 ENDIF NEXT Index
Each pass of the loop can take a different branch because the IF is tested again every time round → FOR Day ← 1 TO 7 IF Rain[Day] > 10 THEN OUTPUT "Wet" ELSE OUTPUT "Dry" ENDIF NEXT Day
FOR Index ← 1 TO 10 Count ← 0 IF Score[Index] > 80 THEN Count ← Count + 1 ENDIF NEXT Index → Count ← 0 is inside the loop, so the counter is reset on every pass and the final count is wrong. Set it to zero once before the loop.
FOR Index ← 1 TO 10 IF Score[Index] > 80 THEN Count ← Count + 1 NEXT Index → The ENDIF is missing, so the IF inside the loop is not closed before NEXT. Add ENDIF after the THEN statement.

Key Term

IF inside a loop
An IF placed inside a loop body is tested again on every pass, so each pass can take a different branch. It is often used to count items that meet a condition.

Fill in the Blanks

1. FOR Index ← 1 TO 10 IF Score[Index] > 50 THEN Passes ← Passes + 1
Hint: Type the keyword that starts a selection, which is tested again on every pass of the loop.
2. FOR Index ← 1 TO 10 IF Score[Index] > 50 THEN Passes ← Passes + 1
Hint: Type the name of the counter variable that is increased each time the condition is true.
3. WHILE Index <= 10 DO IF Temp[Index] > 30 THEN Hot ← Hot + 1
Hint: Type the keyword that comes after the condition and introduces the statements that run when it is true.
4. FOR Num ← 1 TO 20 IF Num MOD 2 = 0 THEN Evens ← Evens + 1
Hint: Type the comparison operator that checks whether the remainder equals zero.
5. FOR Index ← 1 TO 10 IF Mark[Index] >= 40 AND Mark[Index] <= 60 THEN Middle ← Middle + 1
Hint: Type the logical operator that makes both parts of the range condition true together.

Spot the Error

Question 1
A Pass ← 0
B FOR Count = 1 TO 5
C IF Mark[Count] >= 50 THEN
D Pass ← Pass + 1
E ENDIF
F NEXT Count
Line B — A FOR loop sets its counter with the assignment arrow ←, not =. The line should be FOR Count ← 1 TO 5.
Question 2
A Count ← 0
B FOR N ← 1 TO 10
C IF Score[N] > 80 THEN
D Count ← 1
E ENDIF
F NEXT N
Line D — Count is set back to 1 each time the IF is true, so it never builds up. To count items it must add 1 each time: Count ← Count + 1.
Question 3
A WHILE Index <= 10 DO
B IF Temp[Index] < 30 THEN
C Hot ← Hot + 1
D ENDIF
E Index ← Index + 1
F ENDWHILE
Line B — The program counts hot days, so the condition must test for temperatures above 30. The wrong operator < counts cold days. It should be IF Temp[Index] > 30 THEN.
Question 4
A Evens ← 0
B FOR Num ← 1 TO 20
C IF Num MOD 2 = 0 THEN
D Evens ← Evens + Num
E ENDIF
F NEXT Num
Line D — Adding Num makes a total of the even numbers, not a count. Counting adds 1 each time the condition is true: Evens ← Evens + 1.
Question 5
A FOR Count ← 1 TO 10
B INPUT Age
C IF Age >= 18
D OUTPUT "Adult"
E ENDIF
F NEXT Count
Line C — The IF line is missing the keyword THEN. It should be IF Age >= 18 THEN.

Student with a class code? Join your class →