Sikau

JavaScript · Day 18 · about 25 min

while loops

Keep stirring until it thickens.

Do it again… and again

Making kheer, you keep stirring while the milk is thin. You don't write "stir" a hundred times in the recipe — you write the rule once. A loop lets code repeat the same way.

The while loop

A while loop repeats its block as long as the condition is true. Each time through is called an iteration.

let count = 1;

while (count <= 3) {
  console.log("Stir " + count);
  count = count + 1;
}
console.log("Kheer is ready!");

The rest of this lesson

  • Shortcuts for countingexplain
  • When to use whileexplain
  • Predict the outputpredict
  • Halvingpredict
  • 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