All subjects · Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

Repetition and iteration: while, do...while, for and for...of

Edexcel IAL Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

Loops repeat code. The specification lists repetition and iteration separately (spec 9.3.2 e and f): here, repetition means condition-controlled loops and iteration means count-controlled loops and loops through a data structure.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Repetition: condition-controlled loops

let balance = 100;
let years = 0;
while (balance < 150) {
  balance = balance * 1.1;
  years++;
}
console.log(years);
let n = 10;
do {
  console.log("Runs once even though n is " + n);
} while (n < 5);

Output:

5
Runs once even though n is 10

Iteration: count-controlled loops

for (let i = 1; i <= 5; i++) {
  console.log(i * 3);
}

Output:

3
6
9
12
15

The for loop has three parts: start (let i = 1), condition checked before each pass (i <= 5) and update after each pass (i++). It suits a known number of passes. A loop counting down uses i-- and break leaves a loop early.

Iterating over data

const names = ["Ali", "Bea", "Cai"];
for (let i = 0; i < names.length; i++) {
  console.log(i + ": " + names[i]);
}
for (const name of names) {
  console.log(name.toUpperCase());
}
let total = 0;
[4, 6, 10].forEach(function (x) {
  total += x;
});
console.log(total);

Output:

0: Ali
1: Bea
2: Cai
ALI
BEA
CAI
20

Indexes start at 0, so the loop runs while i < names.length (using <= would read past the end and get undefined). Loop to the length of the data, not to a typed number, so the code still works when data is added.

Worked example

Write a loop that adds up the numbers 1 to 100 and displays the total.

  1. Count-controlled: for (let i = 1; i <= 100; i++).
  2. Keep a running total that starts at 0 and add i on each pass.
  3. Display after the loop: 5050

Answer: ```js let total = 0; for (let i = 1; i <= 100; i++) { total += i; } console.log(total); ```

Key idea: while checks first (may run zero times); do...while checks after (runs at least once); for counts (start; condition; update); for...of and forEach visit each item. Indexes run from 0 to length - 1; avoid infinite loops.

Check yourself

  1. What is displayed when this code runs? ```js let s = ""; for (let i = 0; i < 4; i++) { s = s + i; } console.log(s); ```
    Show answer

    0123. s starts as a string, so each i is joined on: 0123.

  2. What is displayed when this code runs? ```js let n = 1; let count = 0; while (n < 50) { n = n * 3; count++; } console.log(count); ```
    Show answer

    4. n goes 3, 9, 27, 81: four passes, so 4.

  3. What is displayed when this code runs? ```js const marks = [12, 7, 20, 9]; let total = 0; for (const m of marks) { if (m >= 10) { total += m; } } console.log(total); ```
    Show answer

    32. Only 12 and 20 are at least 10: 12 + 20 = 32.

  4. What is displayed when this code runs? ```js let x = 5; do { console.log(x); x++; } while (x < 3); ```
    • Nothing is displayed
    • 3
    • 5 / 6
    • 5
    Show answer

    5. do...while runs the body once before checking, so 5 is displayed; then 6 < 3 is false.

  5. This code should display pen ink pad but it does not. Which line number contains the error? ``` 1 const items = ["pen", "ink", "pad"]; 2 let out = ""; 3 for (let i = 0; i <= items.length; i++) { 4 out = out + items[i] + " "; 5 } 6 console.log(out.trim()); ```
    Show answer

    3. i <= items.length runs once too often and reads items[3], which is undefined. The fix is for (let i = 0; i < items.length; i++) {

  6. What is displayed when this code runs? ```js let out = []; for (let i = 10; i > 0; i -= 3) { out.push(i); } console.log(out.join(",")); ```
    Show answer

    10,7,4,1. i takes 10, 7, 4, 1, then -2 fails the test: 10,7,4,1.

  7. Which loop always runs its body at least once?
    • while
    • do...while
    • for
    • for...of
    Show answer

    do...while. do...while tests the condition after the body.

  8. What is displayed when this code runs? ```js const nums = [3, 8, 15, 4, 22]; let found = -1; for (let i = 0; i < nums.length; i++) { if (nums[i] > 10) { found = i; break; } } console.log(found); ```
    Show answer

    2. The first value over 10 is 15, at index 2; break stops the loop, so 2.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.