The while will continue to execute the statements in it's control block
as long as the boolean expression is true.
while ( boolean expression ) {
statement;
}
Example
/*
* execute the print statement until the counter hits zero
*/
int i = 10;
while ( i > 0 ) {
printf ( "In the while loop (i = %d)\n", i );
i--;
}
Output:
In the while loop (i = 10)
In the while loop (i = 9)
In the while loop (i = 8)
In the while loop (i = 7)
In the while loop (i = 6)
In the while loop (i = 5)
In the while loop (i = 4)
In the while loop (i = 3)
In the while loop (i = 2)
In the while loop (i = 1)
A do-while also exists which will execute the dependent statement prior
to checking the boolean clause
/*
* execute the print statement until the counter hits zero
*/
int i = 10;
do {
printf ( "In the do loop (i = %d)\n", i );
i--;
} while ( i > 0 );
which will produce the same output