← IGCSE Computer Science Paper 2

Selection inside a loop (8.1b)

An IF or CASE placed in a loop body is tested again on every repeat, so each pass can take a different branch depending on the current value.

Understanding Selection inside a loop (8.1b) in IGCSE Computer Science Paper 2

What it does: The IF is in the loop body, so each Mark entered is tested separately and Passes only increases for marks of 50 or more. Day changes on every pass of the loop, so the CASE is re-evaluated each time and outputs Weekend or Weekday as appropriate. MOD gives the remainder. An even number has remainder 0 when divided by 2. DIV gives the whole-number part, so it cannot test for even. Only the statement inside the IF runs when the condition is true. A statement after ENDIF runs on every pass, so it would add all the marks. The IF...ENDIF must sit completely inside the FOR...NEXT, so ENDIF is written before NEXT i.

Common mistakes: A count-controlled loop uses TO to set the end value, so it must be FOR Count ← 1 TO 10. UNTIL belongs to REPEAT…UNTIL. A FOR loop does not use DO. DO belongs to WHILE loops. The line should be FOR N ← 1 TO 20.

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 389–398) and word search (Modules 379–398).

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 checked?
10 times, once on every repeat of the loopOnce, before the loop starts
Matches
FOR i <- 1 TO 10 NEXT i IF i MOD 2 = 0 THEN Count <- Count + 1 ENDIF
↓
The IF is placed after NEXT, so it is outside the loop. It is only tested once, not on every pass. The IF must be moved inside the loop, before NEXT i.
Applying
Meanings
Term
IF inside a loop
Meaning
An IF or CASE placed in a loop body is tested again on every repeat, so each pass can take a different branch depending on the current value.
Practice
Count ← 0
Testing
Gaps
IF Num 2 = 0 THEN
Check Answer
Spots
1FOR Count ← 1 UNTIL 10
2 IF Count MOD 2 = 0 THEN

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

The 'Cosmic Dragon' badge — Orbit Master, 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 checked?
A. 10 times, once on every repeat of the loop ✓
B. Once, before 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 code counts the even numbers from 1 to 20?
A. FOR i <- 1 TO 20 IF i MOD 2 = 0 THEN Count <- Count + 1 ENDIF NEXT i ✓
B. FOR i <- 1 TO 20 IF i DIV 2 = 0 THEN Count <- Count + 1 ENDIF NEXT i
MOD gives the remainder. An even number has remainder 0 when divided by 2. DIV gives the whole-number part, so it cannot test for even.
3. Which loop adds only the marks that are 50 or more to Total?
A. Total <- Total + Mark outside the IF, after ENDIF
B. Total <- Total + Mark inside an IF Mark >= 50 THEN ... ENDIF ✓
Only the statement inside the IF runs when the condition is true. A statement after ENDIF runs on every pass, so it would add all the marks.
4. Where must the ENDIF be placed when an IF is inside a FOR loop?
A. Before NEXT, so the IF is closed inside the loop body ✓
B. After NEXT, so the IF wraps the whole loop
The IF is inside the loop, so it must be closed with ENDIF before the loop ends. Putting ENDIF after NEXT makes the structure invalid.
5. In a REPEAT...UNTIL loop, an IF inside the body checks the number just entered. When is the IF tested?
A. Only on the first pass, then the result is reused
B. On every pass, using the value entered on that pass ✓
The loop body repeats, including the IF. Each pass reads a new value, so the IF can choose a different branch each time.

Match the Pairs

FOR i <- 1 TO 10 IF i MOD 2 = 0 THEN Count <- Count + 1 ENDIF NEXT i → Counts the even numbers from 1 to 10. The IF is tested on every pass, so Count only goes up when i is even.
WHILE Num <> -1 IF Num > 50 THEN Total <- Total + Num ENDIF INPUT Num ENDWHILE → Keeps reading numbers until -1 is entered and adds only the numbers over 50 to Total.
A selection inside a REPEAT...UNTIL loop is checked on every repeat, so each input can be handled differently, such as counting only passes. → REPEAT INPUT Mark IF Mark >= 50 THEN Passes <- Passes + 1 ENDIF UNTIL Mark = -1
A CASE statement inside a FOR loop chooses a different branch on each pass, depending on the current value of the counter. → FOR Day <- 1 TO 7 CASE OF Day 6, 7 : OUTPUT "Weekend" OTHERWISE : OUTPUT "Weekday" ENDCASE NEXT Day
FOR i <- 1 TO 10 NEXT i IF i MOD 2 = 0 THEN Count <- Count + 1 ENDIF → The IF is placed after NEXT, so it is outside the loop. It is only tested once, not on every pass. The IF must be moved inside the loop, before NEXT i.
FOR i <- 1 TO 10 IF i MOD 2 = 0 THEN Total <- Total + i NEXT i ENDIF → The loop and the selection overlap. NEXT i is inside the IF, so the ENDIF must come before NEXT i. A structure inside a loop must be fully closed before the loop ends.

Key Term

IF inside a loop
An IF or CASE placed in a loop body is tested again on every repeat, so each pass can take a different branch depending on the current value.

Fill in the Blanks

1. FOR Num ← 1 TO 20 IF Num MOD 2 = 0 THEN Count ← Count + 1
Hint: Type the operator that gives the remainder after division, used to test whether Num is even.
2. WHILE Count < 5 DO INPUT Mark IF Mark >= 50 THEN
Hint: Type the keyword that comes after the condition of an IF inside the loop.
3. REPEAT INPUT Num IF Num > 0 THEN Total ← Total + Num
Hint: Type the variable holding the value just entered, so only positive values are added to the total.
4. FOR Day ← 1 TO 7 CASE OF Day 1 : OUTPUT "Monday"
Hint: Type the keyword that links CASE to the variable being tested on each repeat.
5. FOR Num ← 1 TO 10 IF Num > 5 THEN Total ← Total + Num NEXT Num
Hint: Type the name of the loop counter that closes the FOR loop.

Spot the Error

Question 1
A FOR Count ← 1 UNTIL 10
B IF Count MOD 2 = 0 THEN
C Evens ← Evens + 1
D ENDIF
E NEXT Count
Line A — A count-controlled loop uses TO to set the end value, so it must be FOR Count ← 1 TO 10. UNTIL belongs to REPEAT…UNTIL.
Question 2
A Evens ← 0
B FOR N ← 1 TO 20 DO
C IF N MOD 2 = 0 THEN
D Evens ← Evens + 1
E ENDIF
F NEXT N
Line B — A FOR loop does not use DO. DO belongs to WHILE loops. The line should be FOR N ← 1 TO 20.
Question 3
A WHILE Num > 0 DO
B IF Num DIV 2 = 0 THEN
C Evens ← Evens + 1
D ENDIF
E Num ← Num - 1
F ENDWHILE
Line B — DIV gives the whole-number quotient, not the remainder. To test for an even number on each pass, use MOD: IF Num MOD 2 = 0 THEN.
Question 4
A REPEAT
B INPUT Mark
C IF Mark >= 50
D Passes ← Passes + 1
E ENDIF
F UNTIL Mark = -1
Line C — An IF statement needs THEN after its condition. The line should be IF Mark >= 50 THEN.
Question 5
A FOR N ← 1 TO 5
B INPUT Num
C IF Num > 0 THEN
D Total ← Total + 1
E ENDIF
F NEXT N
Line D — To total only the positive values, add the number entered on this pass. Adding 1 just counts them. It should be Total ← Total + Num.

Student with a class code? Join your class →