JavaScript tutorial:
while statement

 

The while statement executes a statement until a specified condition is false.

Syntax

while (expression)
   statement

The while statement syntax has these parts:

Part

Description

expression

A Boolean expression checked before each iteration of the loop. If expression is true, the loop is executed. If expression is false, the loop is terminated.

statement

The statement to be executed if expression is true. Can be a compound statement.

Example

The while statement checks expression before a loop is first executed. If expression is false at this time, the loop is never executed.

The following example illustrates the use of the while statement:

function executeUntil(end)
{
var i=0 ;
while(i< 100)
{
if(i==end)
break ;
document.write("Java is fun");
i++;
}
return(i);
}
document.write(executeUntil(10));

To run the code, paste it into JavaScript Editor, and click the Execute button.

See also: break statement, continue statement, do...while statement, for statement ,for...in statement