← ACARA Digital Technologies Years 7–8

Combining sequence, selection and iteration in one program

A complete program sets up variables first, repeats with a loop, uses an if inside the loop to make decisions, then prints the result after the loop ends.

Understanding Combining sequence, selection and iteration in one program in ACARA Digital Technologies Years 7–8

What it does: The variable is created and set to 0 on a line above the loop, so the loop can safely update it each time. The if runs on each pass of the loop, and console.log sits after the closing brace so it prints once, when the loop is done. The variable must exist before the loop uses it. The result is printed once the loop has finished. The if goes inside the loop so each number is checked. Selection inside iteration lets each repeat make a decision. Starting a counter at 0 gives you a number to add to. Without a starting value it is undefined, and undefined + 1 gives NaN.

Common mistakes: The counter must start at 0 before the loop. Starting at 1 makes the final count one too high. It should be let count = 0;. A total that changes inside the loop must be declared with let. A const cannot be reassigned, so total += i causes a TypeError. It should be let total = 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 635–666) and word search (Modules 635–695).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Where should a total variable be set to 0 in a program that adds up numbers in a loop?
Inside the loop bodyBefore the loop starts
Matches
for (let i = 1; i <= 5; i++) { total += i; } let total = 0;
↓
The total is set up after the loop. It must be created and set to 0 before the loop, otherwise the loop uses a variable that does not exist yet and gives an error.
Applying
Meanings
Term
sequence, selection, iteration
Meaning
A complete program sets up variables first, repeats with a loop, uses an if inside the loop to make decisions, then prints the result after the loop ends.
Practice
let evens = 0;
Testing
Gaps
let total = ;
Check Answer
Spots
1let count = 1;
2for (let i = 1; i <= 8; i++) {

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

The 'Platinum Fox' badge — Tundra 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. Where should a total variable be set to 0 in a program that adds up numbers in a loop?
A. Inside the loop body
B. Before the loop starts ✓
Setting the variable first (sequence) means it keeps its value across repeats. If you set it to 0 inside the loop, it resets every time.
2. Which program order matches sequence, iteration, then output?
A. let count = 0; for loop that updates count; console.log(count); ✓
B. for loop that updates count; let count = 0; console.log(count);
The variable must exist before the loop uses it. The result is printed once the loop has finished.
3. Where should console.log(total) go if you want to print only the final total?
A. After the closing } of the loop ✓
B. Inside the loop, before the closing }
Code inside the loop runs on every repeat. Code after the loop runs once, when the loop is done.
4. Which code counts how many numbers from 1 to 10 are bigger than 5?
A. if (i > 5) { for (let i = 1; i <= 10; i++) { count++; } }
B. for (let i = 1; i <= 10; i++) { if (i > 5) { count++; } } ✓
The if goes inside the loop so each number is checked. Selection inside iteration lets each repeat make a decision.
5. Which line sets up a counter correctly before a loop?
A. let evens = 0; ✓
B. let evens;
Starting a counter at 0 gives you a number to add to. Without a starting value it is undefined, and undefined + 1 gives NaN.

Match the Pairs

let count = 0; for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { count++; } } → Sets a counter to 0 first, loops through the numbers 1 to 10, and uses an if inside the loop to count only the even numbers
console.log("Total: " + total); → Prints the final total after the loop has finished, so it only shows the finished answer once
Set a total to 0 before the loop so it has a starting value to add to → let total = 0; while (total < 20) { total += 5; }
An if inside a for loop makes a decision on every repeat, and the result is printed after the loop → let big = 0; for (let n = 1; n <= 5; n++) { if (n > 3) { big++; } } console.log(big);
for (let i = 1; i <= 5; i++) { total += i; } let total = 0; → The total is set up after the loop. It must be created and set to 0 before the loop, otherwise the loop uses a variable that does not exist yet and gives an error.
let count = 0; for (let i = 1; i <= 5; i++) { count++; console.log(count); } → The console.log is inside the loop, so it prints five times instead of printing the final result once. It should go after the closing brace.

Key Term

sequence, selection, iteration
A complete program sets up variables first, repeats with a loop, uses an if inside the loop to make decisions, then prints the result after the loop ends.

Fill in the Blanks

1. let total = 0; for (let i = 1; i <= 5; i++) { total += i; } console.log(total);
Hint: Type the starting value a total should have before the loop begins adding to it.
2. for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { evens++; } }
Hint: Type the arithmetic operator that gives the remainder, so the if inside the loop can check for even numbers.
3. let total = 0; for (let i = 1; i <= 3; i++) { total += i; } console.log(total);
Hint: Type the function that prints the result in the console once the loop has finished.
4. let n = 1; let big = 0; while (n <= 5) { if (n > 3) big++; n++; }
Hint: Type the update to the loop variable that makes the while loop eventually stop.
5. let count = 0; for (let i = 1; i <= 8; i++) { count++; } console.log(count);
Hint: Type the keyword that sets up a counter variable whose value can change, before the loop starts.

Spot the Error

Question 1
A let count = 1;
B for (let i = 1; i <= 8; i++) {
C if (i % 2 === 0) count++;
D }
E console.log(count);
Line A — The counter must start at 0 before the loop. Starting at 1 makes the final count one too high. It should be let count = 0;
Question 2
A const total = 0;
B for (let i = 1; i <= 9; i++) {
C if (i > 5) total += i;
D }
E console.log(total);
Line A — A total that changes inside the loop must be declared with let. A const cannot be reassigned, so total += i causes a TypeError. It should be let total = 0;
Question 3
A let sum = 0;
B for (let i = 1; i <= 5; i++) {
C if i !== 3 sum += i;
D }
E console.log(sum);
Line C — The condition of an if statement must be inside brackets. It should be if (i !== 3) sum += i;
Question 4
A let total = 0;
B for (let i = 1; i <= 9; i++) {
C if (i % 2 === 0)
D total = i;
E }
F console.log(total);
Line D — Using = replaces the total each time instead of adding to it. To build a running total it should be total += i;
Question 5
A let big = 0;
B for (let i = 1, i <= 7, i++) {
C if (i > 4) big++;
D }
E console.log(big);
Line B — The three parts of a for loop header are separated by semicolons, not commas. It should be for (let i = 1; i <= 7; i++) {

Student with a class code? Join your class →