Exit Controlled While Loop

Do while loopThe exit controlled while loop is structured as follows:

do
{ // begin do while

statement(s)

} // end do while
while (exit condition is not satisfied)

Notice that the two words do and while mark the beginning and end of the loop, so that several statements can appear in the loop.

The statements after do are executed once and then if the condition following while is ‘False’, this sequence is repeated.  As soon as the outcome of the condition is ‘True’, the loop is exited and the computer exits the loop and continues to the next statement in the sequence.

This implementation of the while loop tests the condition at the end of the loop. This means that the code within the loop is carried out at least once.

Example code:

var Number ;
var Total = 0 ;

do
{ // begin do while

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

Number = parseInt(Number);

Total = Total + Number;

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

} // end do while
while (Number != 0) ;

Next: Count Controlled For Loop