Semantics of the while statement
Here is the while statement, again:
while ( condition )
loop body // a statement or block statement
statment after the loop
Some notes on the semantics:
- When the condition is true,
the loop body is exectued.
- When the condition is false,
the loop body is skipped, and the
statment after the loop is executed.
- Once execution has passed to the statment after the loop,
the
while statement is finished, at least for now.
- If the condition is false the very first time it
is evaluated, the loop body will not be executed even once.
Here is the while loop from the example program:
int count = 1; // start count out at one
while ( count <= 3) // loop while count is <= 3
{
System.out.println( "count is:" + count );
count = count + 1; // add one to count
}
System.out.println( "Done with the loop" ); // statement after the loop
This loop makes use of the variable count.
It is started out at 1, then incremented by one until the condition is false.
Then the statement after the loop is executed.
|