Java 2 Primer Plus

   

Java™ 2 Primer Plus

By Steven Haines, Steve Potts

Table of Contents
Appendix C.  Answers to Review Questions

A1:

The general form of the if statement is

if( boolean_value ) { // Execute these statements }

A2:

If the boolean value inside the if statement evaluates to false then an else block can be executed:

if( boolean_value ) { // boolean is true: Execute these statements } else { // boolean is false: Execute these statements }

A3:

There is not a limit to the number of else if statements that can follow an if statement.

A4:

The switch statement only evaluates int variables or variables that can be converted to int.

A5:

The do-while statement assures that the body of the loop is evaluated at least once.

A6:

An infinite loop is a loop that does not terminate; its loop control variable never evaluates to a value that allows the loop to exit. For example:

int a = 10; while( a > 0 ) { // Never change the value of a }

A7:

The most popular type of loop for iterating over a variable or set of variables is the for loop.

A8:

You can skip the statements in the current iteration of a loop by issuing the continue statement.

A9:

You can stop executing the statements in a loop and continue at the first line following the loop by issuing the break statement.

A10:

You can break out of a multiple nested loop by defining a label and then breaking to the label, for example:

outer: for( int a=0; a<10; a++ ) { for( int b=0; b<10; b++ ) { if( a * b == 25 ) break outer; } }


       
    Top
     

    Категории