An if statement inside a loop
An if inside a loop body is checked again on every repeat, so each pass can take a different branch, such as counting only the matches.
Understanding An if statement inside a loop in ACARA Digital Technologies Years 7–8
What it does: The if is inside the loop body, so each score is tested on its own and passes only increases when the condition is true. n % 2 gives the remainder 0 for even numbers. Because the if is checked on every repeat, only the even numbers are printed. If count++ is outside the if, it runs on every pass and counts everything, not just the matches. A single = tries to assign a value instead of comparing, so the check on each repeat is wrong. === compares the remainder to 0.
Common mistakes: matches + 1; works out a value but never stores it, so the counter stays at 0. It should be matches++; to add 1 each time the if is true. A single = assigns a value instead of comparing. The condition should use === so each pass checks whether i is even: if (i % 2 === 0).
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 599–610) and word search (Modules 583–613).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
for (...) { if (...) { ... } }Fill in the Blanks
Spot the Error
Student with a class code? Join your class →