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

Answer:

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:

int count = 0;                      // initialize count to 0

do
{
  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.

Notice how the do and the while bracket the statements that form the loop body. The condition that is tested follows the while.

QUESTION 2:

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