← ACARA Digital Technologies Years 7–8

Totals and counters in a loop

Adds each value to a running total inside a loop. Start with let total = 0 before the loop, and use count++ to count how many times something happens. Print the results after the loop.

Understanding Totals and counters in a loop in ACARA Digital Technologies Years 7–8

What it does: total += i adds the current loop value onto the total on every pass through the loop. Printing after the closing brace of the loop shows only the final answer, not every step along the way. count++ adds 1 to count each time it runs. count = 1 just sets it to 1 again and again. Total and count change as the loop runs, so they need let. A const variable cannot be reassigned and gives a TypeError. count goes up by 1 on each of the 5 repeats: 0, 1, 2, 3, 4, 5. It ends at 5. count++ is shorthand for count = count + 1 and is used inside a loop to count events.

Common mistakes: i-- makes the counter go down, so i <= 5 is always true and the loop never ends. It should be i++ so the loop counts up and finishes. A total must start at 0 before the loop. Starting at 1 makes the final answer 1 too big. 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 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 should you set let total = 0 when adding up values in a loop?
Before the loop startsInside the loop body
Matches
for (let i = 1; i <= 5; i++) { let total = 0; total += i; }
↓
let total = 0 is inside the loop, so the total is reset to 0 on every pass and never builds up. It must be set before the loop.
Applying
Meanings
Term
total += value;
Meaning
Adds each value to a running total inside a loop. Start with let total = 0 before the loop, and use count++ to count how many times something happens. Print the results after the loop.
Practice
let total = 0;
Testing
Gaps
let total = ;
Check Answer
Spots
1let total = 0;
2for (let i = 1; i <= 5; i--) {

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

The 'Moon Jellyfish' badge — Orbital Streak, 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 you set let total = 0 when adding up values in a loop?
A. Before the loop starts ✓
B. Inside the loop body
Set total to 0 before the loop. If it is inside the loop, it resets to 0 on every repeat and the running total is lost.
2. Which line adds the value of i to a running total?
A. total = i;
B. total += i; ✓
total += i adds i to what is already in total. total = i replaces the total each time, so only the last value is kept.
3. Which line counts one more time something happens?
A. count = 1;
B. count++; ✓
count++ adds 1 to count each time it runs. count = 1 just sets it to 1 again and again.
4. A loop adds the numbers 1 to 4 to total, which starts at 0. What is total after the loop?
A. 10 ✓
B. 4
0 + 1 + 2 + 3 + 4 = 10. The total keeps every value added. The number 4 is only the last value.
5. Where should console.log(total) go if you want to show the final total only once?
A. After the closing brace of the loop ✓
B. Inside the loop, before the closing brace
Printing after the loop ends shows the finished total once. Inside the loop it prints a new partial total on every repeat.

Match the Pairs

let total = 0; → Sets the running total to 0 before the loop starts, so every value can be added onto a clean start.
count++; → Adds 1 to the counter each time it runs, so it counts how many times something happened.
Add each value in the loop to the running total → total += i;
Show the final total once, after the loop has finished → console.log(total);
for (let i = 1; i <= 5; i++) { let total = 0; total += i; } → let total = 0 is inside the loop, so the total is reset to 0 on every pass and never builds up. It must be set before the loop.
let count = 0; for (let i = 1; i <= 5; i++) { count = 1; } → count = 1 sets the counter to 1 every time instead of adding 1, so it never goes above 1. It should be count++.

Key Term

total += value;
Adds each value to a running total inside a loop. Start with let total = 0 before the loop, and use count++ to count how many times something happens. Print the results after the loop.

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.
2. let total = 0; for (let i = 1; i <= 5; i++) total += i; console.log(total);
Hint: Type the shorthand operator that adds each value to the running total.
3. let count = 0; for (let i = 0; i < 5; i++) count++; console.log(count);
Hint: Type the expression that adds 1 to the counter each time the loop repeats.
4. let total = 0; for (let i = 1; i <= 4; i++) total += i; console.log(total);
Hint: Type the function that prints the result once, after the loop has finished.
5. let total = 0; for (let i = 1; i <= 3; i++) total += i * 2; console.log(total);
Hint: Type the name of the variable that holds the running total.

Spot the Error

Question 1
A let total = 0;
B for (let i = 1; i <= 5; i--) {
C total += i;
D }
E console.log(total);
Line B — i-- makes the counter go down, so i <= 5 is always true and the loop never ends. It should be i++ so the loop counts up and finishes.
Question 2
A let marks = [50, 80, 90];
B let total = 1;
C for (let m of marks) {
D total += m;
E }
F console.log(total);
Line B — A total must start at 0 before the loop. Starting at 1 makes the final answer 1 too big. It should be let total = 0;
Question 3
A const count = 0;
B let marks = [40, 75, 90, 55];
C for (let m of marks) {
D if (m >= 50) count++;
E }
F console.log(count);
Line A — A counter changes every time count++ runs, so it can't be a const. It should be let count = 0; or the program crashes with a TypeError.
Question 4
A let total = 0;
B let n = 1;
C while (n >= 5) {
D total += n;
E n++;
F }
Line C — n starts at 1, so n >= 5 is false straight away and the loop body never runs. The total stays 0. It should be while (n <= 5) {
Question 5
A let count = 0;
B for (let i = 0; i < 10; i++) {
C if (i > 6) count + 1;
D }
E console.log(count);
Line C — count + 1 works out a number but never stores it, so count stays 0. Use count++ (or count += 1) to update the counter.

Student with a class code? Join your class →