Sikau

JavaScript · Day 21 · about 25 min

break, continue, nested loops

Stopping early and grids.

Taking control of loops

Searching your bag for keys, you stop as soon as you find them. Sorting rice, you skip the stones. And a chessboard is rows *of* squares — a loop inside a loop. Today: break, continue and nested loops.

break and continue

for (let i = 1; i <= 10; i++) {
  if (i === 4) {
    break;
  }
  console.log(i);
}

for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    continue;
  }
  console.log("Room " + i);
}

break = "I'm done with this loop." continue = "skip this one, carry on."

The rest of this lesson

  • Loops inside loopsexplain
  • continue, and tracing nested loopsexplain
  • Predict the outputpredict
  • Pairspredict
  • Your turnwrite code
  • Challengechallenge
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 68 lessons of JavaScript