JavaScript / Loops

while (2): conditions inside loops and continue

Put an if statement inside a while loop, and skip the rest of the current turn with continue. Baccalaureate

Reminder: a while loop repeats as long as its condition is true. We walk laps of 6 km and print the total after each lap, until it passes 30 km:

JavaScript
let distance = 0;
while (distance < 30) {
  distance = distance + 6;
  console.log("Walked a total of " + distance + "km");
}
Console
Walked a total of 6km
Walked a total of 12km
Walked a total of 18km
Walked a total of 24km
Walked a total of 30km
"Until it passes 30" is the same as "while it is less than 30", so the condition is distance < 30. A loop states when to keep going, not when to stop.

if inside while

A bank deposit of 10000 is multiplied by 1.2 every two years. After how many years does it pass 50000? We add a year each turn, but multiply the money only in even years. A year is even when its remainder after dividing by 2 is zero: year % 2 == 0.

JavaScript
let money = 10000;
let year = 0;
while (money < 50000) {
  year++;
  if (year % 2 == 0) {
    money = money * 1.2;
  }
}
console.log("It will pass 50000 in " + year + " years");
Console
It will pass 50000 in 18 years
The if inside the loop is checked on every turn, but its body runs only when its condition is true.

continue

The World Cup is held every 4 years starting in 2026. Suppose the 2050 tournament is cancelled and we do not want to count it. The word continue skips the rest of the current turn and goes straight back to the condition for the next turn.

JavaScript
let year = 2026;
let count = 0;
while (year <= 2100) {
  if (year == 2050) {
    year = year + 4;
    continue;
  }
  count++;
  year = year + 4;
}
console.log("There will be " + count + " tournaments by 2100");
Console
There will be 18 tournaments by 2100
Increase year before continue. If you forget, year stays 2050, the loop skips everything on every turn, and never ends. Also, continue does not stop the loop; it skips just one turn.

Practice

1Fill in the blankCurriculum practice

Complete the loop: as long as distance is less than 100, add 6 to it, then print the total: Walked a total of 6km, then Walked a total of 12km, and so on up to Walked a total of 102km.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let distance = 0;
___ (___) {
  ___;
  console.log("Walked a total of " + distance + "km");
}
Console
Press Run to see the result.
2Fill in the blankCurriculum practice

The deposit starts at 20000 and is multiplied by 1.2 every two years. Complete it: repeat as long as money is less than 50000, and add 1 to year each turn.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let money = 20000;
let year = 0;
while (___) {
  ___;
  if (year % 2 == 0) {
    money = money * 1.2;
  }
}
console.log("It will pass 50000 in " + year + " years");
Console
Press Run to see the result.
3Fill in the blankCurriculum practice

Print the Olympic years from 2024 to 2060: a year is an Olympic year if its remainder after dividing by 4 is 0.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let year = 2024;
while (year <= 2060) {
  if (___) {
    console.log(year + " is an Olympic year");
  }
  year += 1;
}
Console
Press Run to see the result.
4Fill in the blankCurriculum practice

Add 7 to num as long as it is less than 100, count the turns in count, then print count.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let num = 0;
let count = 0;
while (___) {
  num += 7;
  count++;
}
console.log(count);
Console
Press Run to see the result.
5Edit the programCurriculum practice

At the start of the loop, add: if a == 5, add 1 to a, then skip the rest of the turn with continue. It should print a: 11 times then b: 10 times.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let a = 0;
let b = 0;
while (a <= 10) {

  b = b + 1;
  a = a + 1;
}
console.log("a: " + a + " times");
console.log("b: " + b + " times");
Console
Press Run to see the result.
6Write the programExtra practice

Students 1 to 8 line up for the class photo, but students 3 and 6 are absent. Use while and continue to print Student 1, Student 2 ... for those present only.

HintIncrease student at the top of the loop, so the increase never gets skipped by continue.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let student = 0;
Console
Press Run to see the result.
7Write the programExtra practice

Mariam saves 50 pounds every day, but on every seventh day (a day whose remainder after dividing by 7 is 0) she spends 30. Repeat while money is less than 500, then print Day 11 (the day she reached it).

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let money = 0;
let day = 0;
Console
Press Run to see the result.

Key points

  • You can put an if inside a while; it is checked on every turn.
  • "Until it passes X" is written in a while as "as long as it is less than X".
  • continue skips the rest of the current turn and starts the next one; change the condition variable before it.