Java summary: Statements

Control statements contain blocks of statements enclosed in curly braces {}. If only 1 statement, the braces may be omitted.??

if Statement

//----- if statement with only a true clause
if (expression) {
statements
}
//----- if statement with true and false clause
if (expression) {
statements
}else{
statements
}
//----- if statements with many parallel tests
if (expression1) {
statements
}else if (expression2) {
statements
}else if (expression3) {
statements
. . .
}else {
statements // if no expression was true
}

switch Statement

The effect of the switch statement is to choose some statements to execute depending on the integer value of an expression. The break statement exits from the switch statement. If there is no break at the end of a case, execution continues in the next case (fall-through)!!
   switch (expr) {
case c1:
statements
break;
case c2:
statements // if expr == c2
// no break, so the next statements are also executed!!
case c2, c3, c4: // multiple values
statements
break;
. . .
default:
statements // if none of above
}

while

   while (testExpression) {
statements
}

for

The for first executes the initialStmt, then the testExpr, if true executes the statements. Before testing the testExpr again, it executes the incrementStmt.
   for (initialStmt; testExpr; incrementStmt) {
statements
}

do

do {
statements
} while (testExpression);


Other loop controls

break;       //exit innermost loop or switch
break label; //exit from loop label
continue; //start next loop iteration
continue label; //start next loop label

Label a statement:

outerloop:
for (...)
for ()
break outerloop; // exits the outer for loop

Method Return

return;      //exits a void method
return expr; //exits method and returns value

try...catch exceptions (exception handling)

try {
. . . // statements that might cause exceptions
}catch (exception-type x) {
. . . // statements to handle exception
}