go to previous page   go to home page   go to next page highlighting


Yes — Java has the if statement (both with and without an else), the switch statement, and the conditional statement.

Of these, the if statement (with an else) is by far the most useful. The other two types of branches are not logically needed, although they are sometimes useful.

The do Statement

The do statement is similar to the while statement with an important difference: the do statement performs a test after each execution of the loop body. Here is a counting loop that prints integers from 0 to 9:

Notice how the do and the while bracket the statements that form the loop body.

To start, the statements in the loop body are executed. Then the condition after the while is tested. If it is true the loop body is executed again. Otherwise, control passes to the statement after the test.

int count = 0;                      // initialize count to 0

  System.out.println( count );      // loop body: includes code to
  count++  ;                        // change the count
while ( count < 10 );               // test if the loop body should be
                                    // executed again.


Does the code fragment include the three things that all loops must do?

go to previous page   go to home page   go to next page