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.
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- while checks the condition before each pass, so it may run zero times.
- do...while checks after each pass, so it always runs at least once (useful for 'ask until valid').
- Something inside the loop must eventually make the condition false, or it is an infinite loop that freezes the page.
Iteration: count-controlled loops
for (let i = 1; i <= 5; i++) {
console.log(i * 3);
}Output:
3
6
9
12
15The 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
20Indexes 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.
- Count-controlled: for (let i = 1; i <= 100; i++).
- Keep a running total that starts at 0 and add i on each pass.
- Display after the loop: 5050
Answer: ```js let total = 0; for (let i = 1; i <= 100; i++) { total += i; } console.log(total); ```
Check yourself
- 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.
- 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.
- 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.
- 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.
- 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++) {
- 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.
- 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.
- 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.
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Information Technology specification (first teaching 2018), Unit 2 Topics 7 to 11
- Pearson qualifications: Edexcel International Advanced Level Information Technology (2018)
- Pearson WIT12/01 mark scheme, June 2024 series
Last updated 8 November 2026. Spotted a mistake? Tell us.