Java: Summary - Control Flow

if Statement

If statement with a true clause
if (score > 70) {
    System.out.println("You pass");
}
If statement with true and false clauses
if (score > 70) {
    System.out.println("You pass");
} else {
    System.out.println("You fail");
}
If statements with many parallel tests
if (score >= 90) {
    grade = "A";
} else if (score >= 80) {
    grade = "B";
} else if (score >= 70) {
    grade = "C";
} else if (score >= 60) {
    grade = "D";
} else {
    grade = "F";
}
System.out.printlne("Your grade is " + grade);
   

switch Statement

switch chooses one case depending on an integer value. This is equivalent to a series of cascading if statements, but switch is easier to read if all comparisons are against one value. The break statement exits from the switch statement. If there is no break at the end of a case, execution falls thru into the next case, which is almost always an error.
   switch (expr) {
      case c1:
            statements // do these if expr == c1
            break;
      case c2: 
            statements // do these if expr == c2
            break;
      case c2:
      case c3:
      case c4:         //  Cases can simply fall thru.
            statements // do these if expr ==  any of c's
            break;
      . . .
      default:
            statements // do these if expr != any above
   }

while loop

The while statement tests the expression. If the expression evaluates to true, it executes the body of the while. If it is false, execution continues with the statement after the while body. Each time after the body is executed, execution starts with the test again. This continues until the expression is false or some other statement (break or return) stops the loop.
   while (testExpression) {
       statements
   }

Other loop controls: break and continue

All loop statements can be labeled, so that break and continue can be used from any nesting depth.
   break;       //exit innermost loop or switch
   break label; //exit from loop label
   continue;    //start next loop iteration
   continue label; //start next loop label
Put label followed by colon at front of loop.
outer: for (. . .) {
         . . .
               continue outer;
   

for loop

Many loop have an initialization before the loop, and some "increment" before the next loop. The for loop is the standard way of combining these parts.
   for (initialStmt; testExpr; incrementStmt) {
       statements
   }
This is the same as (except continue will increment):
   initialStmt;
   while (testExpr) {
       statements
       incrementStmt
   }

do-while loop

This is the least used of the loop statements, but sometimes a loop that executes one time before testing is used.
do {
    statements
} while (testExpression);

Other Flow Control Statements

Use try...catch for exceptions

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

throw

   throw exception-object;

Multiple catch clauses and finally clause

Executes first catch clause that specifies the exception class or a super class. The finally clause is always executed (regardless of whether there was an exception or not) so resources can be cleaned up (eg, closing a file).
   try {
       . . . // statements that might cause exceptions
   } catch (exception-type x) {
       . . . // statements to handle exception
   } catch (exception-type x) {
       . . . // statements to handle exception
   } finally (exception-type x) {
       . . . // statements that will always be executed, exception or not.
   }

Methods

Method Return

   return;      //no value for void method
   return expr; //method value to return

Threads

Not covered.

Copyright 2007 Fred Swartz