← ACARA Digital Technologies Years 7–8

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).

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 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.

Learning
Duo
Where does the if go so it is checked on every repeat of the loop?
Inside the loop's curly braces { }After the loop's closing brace }
Matches
for (let i = 1; i <= 10; i++) { if (i % 2 = 0) { count++; } }
↓
The if uses = (assignment) instead of === (comparison), so it does not test whether i is even. It should be i % 2 === 0.
Applying
Meanings
Term
for (...) { if (...) { ... } }
Meaning
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.
Practice
let evens = 0;
Testing
Gaps
(n > 3) { console.log(n); }
Check Answer
Spots
3 if (n % 2 === 0) {
4 matches + 1;

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

The 'Armadillo' badge — Shield Bearer, 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. Where does the if go so it is checked on every repeat of the loop?
A. After the loop's closing brace }
B. Inside the loop's curly braces { } ✓
An if inside the loop body is checked once per repeat. An if after the loop runs just once, when the loop has finished.
2. for (let i = 1; i <= 5; i++) { if (i > 3) { console.log(i); } } What does this print?
A. 4 then 5 ✓
B. 1 2 3 4 5
The if is checked on each pass. Only 4 and 5 pass the test i > 3, so only those numbers are printed.
3. Which condition checks if the loop counter i is an even number?
A. if (i % 2 === 0) ✓
B. if (i % 2 = 0)
% gives the remainder. An even number has remainder 0 when divided by 2. Use === to compare, because a single = assigns a value.
4. let count = 0; for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { count++; } } What is count at the end?
A. 10
B. 5 ✓
The loop runs 10 times, but count++ only runs when i is even (2, 4, 6, 8, 10). That is 5 times.
5. To count only the matches in a loop, where should count++ go?
A. Inside the if, so it only runs when the test is true ✓
B. Inside the loop but outside the if
If count++ is outside the if, it runs on every pass and counts everything, not just the matches.

Match the Pairs

for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { count++; } } → Checks every number from 1 to 10 and adds 1 to count only when the number is even.
for (let i = 1; i <= 5; i++) { if (i > 3) { console.log(i); } else { console.log("low"); } } → Prints low for 1, 2 and 3, then prints the number for 4 and 5, because each pass takes a different branch.
Count only the scores that are above 50 by putting an if inside the loop. → for (let i = 0; i < scores.length; i++) { if (scores[i] > 50) { passes++; } }
Use % 2 inside a loop to tell whether each number is even. → for (let n = 1; n <= 8; n++) { if (n % 2 === 0) { console.log(n); } }
for (let i = 1; i <= 10; i++) { if (i % 2 = 0) { count++; } } → The if uses = (assignment) instead of === (comparison), so it does not test whether i is even. It should be i % 2 === 0.
if (i % 2 === 0) { for (let i = 1; i <= 10; i++) { count++; } } → The if is outside the loop, so it is only checked once and every number gets counted. The if must go inside the loop body.

Key Term

for (...) { if (...) { ... } }
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.

Fill in the Blanks

1. for (let n = 1; n <= 6; n++) { if (n > 3) { console.log(n); } }
Hint: Type the keyword that checks a condition on every pass of the loop
2. for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { console.log(i); } }
Hint: Type the operator that gives the remainder after dividing, used to find even numbers
3. let count = 0; for (let n = 1; n <= 9; n++) { if (n > 5) { count++; } }
Hint: Type the name of the variable that counts only the matching numbers
4. for (let i = 1; i <= 4; i++) { if (i > 2) { console.log("big"); } else { console.log("small"); } }
Hint: Type the keyword that gives the other branch when the if condition is false
5. let total = 0; for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { total += i; } }
Hint: Type the loop counter variable so each even number is added to the total

Spot the Error

Question 1
A let matches = 0;
B for (let n = 1; n <= 8; n++) {
C if (n % 2 === 0) {
D matches + 1;
E }
F }
Line D — 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.
Question 2
A for (let i = 1; i <= 6; i++) {
B if (i % 2 = 0) {
C console.log(i);
D }
E }
Line B — A single = assigns a value instead of comparing. The condition should use === so each pass checks whether i is even: if (i % 2 === 0).
Question 3
A let total = 0;
B for (let i = 0; i < 10, i++) {
C if (i > 5) {
D total += i;
E }
F }
Line B — The parts of a for loop header are separated by semicolons, not commas. It should be for (let i = 0; i < 10; i++) {.
Question 4
A for (let i = 1; i <= 5; i++ {
B if (i % 2 === 1) {
C console.log("odd " + i);
D }
E }
Line A — The closing bracket of the for loop header is missing. It should be for (let i = 1; i <= 5; i++) {.
Question 5
A for (let i = 1; i <= 4; i++) {
B if (i % 2 === 0) {
C console.log("even);
D } else {
E console.log("odd");
F }
Line C — The string is missing its closing quote. It should be console.log("even"); so the loop can print even on the passes where the if is true.

Student with a class code? Join your class →