Entrance Controlled While Loop

While loopThe entrance controlled while loop is structured as follows:

while (entry condition is satisfied)
{ // begin while
} // end while

If the condition after while is ‘True’, the statement(s) within the brackets are executed before the condition is tested again.  This process is repeated as long as the outcome of the test is ‘True’.  As soon as the outcome of the test is ‘False’, the computer exits the loop and continues to the next statement in the sequence.

With this version of the while loop, the condition is tested at the beginning (or entrance) of the loop. Due to this, it is possible that this loop is not executed at all, for example, in the code shown the loop will not be carried out if the user enters their first number as 0.

Example code:

var Number ;
var Total = 0 ;

Number = prompt('Type in a number (Type 0 to finish): ');
Number = parseInt(Number);

while (Number != 0) // while Number is not equal to 0
{ // begin while

Total = Total + Number;

alert ('Number = '+ Number + '\n' + 'Total = ' + Total) ;

Number = prompt('Type in a number (Type 0 to finish):');
Number = parseInt(Number);
} // end while

Next: Exit Controlled While Loop