Skip to content

Latest commit

 

History

History
29 lines (24 loc) · 768 Bytes

File metadata and controls

29 lines (24 loc) · 768 Bytes
chapter 7
pageNumber 66
description While loops repetitively execute a block of code as long as a specified condition is true.

While

While loops repetitively execute a block of code as long as a specified condition is true. It provides a way to automate repetitive tasks and perform iterations based on the condition's evaluation.

while (condition) {
  // do it as long as condition is true
}

For example, the loop in this example will repetitively execute its block of code as long as the variable i is less than 5:

var i = 0,
x = "";
while (i < 5) {
  x = x + "The number is " + i;
  i++;
}

{% hint style="warning" %} Be careful to avoid infinite looping if the condition is always true! {% endhint %}