Java Syntax for Decision Statements

if-else

if ( boolean expression )

        if block

else

        else block

 

Notes:

*A block is either a single statement: statement or a compound statement:

{ as many statements as you want }

*The else (with the else block) is optional

 

switch

switch ( arithmetic expression ) {

        case label 1 : case body 1

        case label 2 : case body 2

        ...

        case label n : case body n

}

 

Notes:

*The data type of arithmetic expression must be char, byte, short, or int. case label i has the form  case constant” or “default”

*Execution starts in the matching case and continues either until the end of the switch or until a break statement is encountered.

 

 

 

Java Syntax for Loops

while

while ( boolean expression )

        block

 

do-while

do {

        block

} while ( boolean expression );

 

for

for ( initialization ; boolean expression ; increment )

        block