To print a word ten times you could copy the line ten times, but that is tiring and harder to change. A for loop writes the command once and repeats it as many times as you say:
for (let i = 0; i < 3; i++) {
console.log("Red lorry, yellow lorry");
}Red lorry, yellow lorry Red lorry, yellow lorry Red lorry, yellow lorry
The brackets hold three parts separated by semicolons. They control how many times the loop runs, using a variable called the counter:
| Part | In the example | Meaning |
|---|---|---|
| Start | let i = 0 | The counter starts at 0 |
| Condition | i < 3 | The loop keeps going while this is true |
| Update | i++ | The counter goes up by 1 after each round |
i++ is short for i = i + 1. The counter is an ordinary variable, so you can print it to see its value each round:
for (let i = 0; i < 5; i++) {
console.log(i);
}0 1 2 3 4
To count from 1, start at 1 and use <=, and join the counter to text with +:
for (let lap = 1; lap <= 4; lap++) {
console.log("Lap " + lap);
}Lap 1 Lap 2 Lap 3 Lap 4
i < 10 repeats 10 times. Starting at 1 with i < 3 repeats only twice (1 and 2). Always check the start and the condition together.switch inside for
In the switch lesson we put one student into a group using the remainder. Now one loop repeats the same check for every student in the class:
for (let num = 1; num <= 8; num++) {
switch (num % 4) {
case 1:
console.log("Student " + num + " is in Group A");
break;
case 2:
console.log("Student " + num + " is in Group B");
break;
case 3:
console.log("Student " + num + " is in Group C");
break;
case 0:
console.log("Student " + num + " is in Group D");
break;
}
}Student 1 is in Group A Student 2 is in Group B Student 3 is in Group C Student 4 is in Group D Student 5 is in Group A Student 6 is in Group B Student 7 is in Group C Student 8 is in Group D
for does not exist outside the loop; using it afterwards causes an error:for (let i = 0; i < 2; i++) {
console.log(i);
}
console.log(i);0 1 ReferenceError: i is not defined
Practice
Fill in the brackets so silk is printed ten times, with a counter named count starting at 0.
for (___) {
console.log("silk");
}Press Run to see the result. Print Good morning seven times with a for loop, counter i starting at 0, using i++.
Press Run to see the result. Fill in the two blanks so the program prints Hello three times (counter i starts at 0).
___ (___) {
console.log("Hello");
}Press Run to see the result. Students 1 to 10 are shared between three groups by the remainder of i divided by 3. Fill in the blanks.
___ (let i = 1; i <= 10; i++) {
___ (___) {
case 0:
console.log("Student " + i + " is in Group C");
break;
case 1:
console.log("Student " + i + " is in Group A");
break;
case 2:
console.log("Student " + i + " is in Group B");
break;
}
}Press Run to see the result. A test has 5 questions. Print Question 1 of 5, then Question 2 of 5, and so on up to Question 5 of 5.
Hint
Start the counter at 1 and use<=.Press Run to see the result. Print the multiples of 5 from 5 to 50, one per line. Make the update i = i + 5 instead of i++.
Press Run to see the result. Key points
for (let i = 0; i < n; i++) { … }repeats the commands n times.i++meansi = i + 1, and the counter is a variable you can print and use.- A counter declared in
forcannot be used outside the loop.