Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

Flow Control and Exception Handling - for Statement

Syntax (JLS §14.13)

    for( initialization; boolean expression; iteration ) {
        statement(s);
    }

  • executes some initialization code, then repeatedly executes a boolean expression and some iteration code until the expression is false
  • all three parts are optional ie the following examples are legal
    for( intialization; ; )
    for( ; expression; iteration )
    for( ; ; iteration)
    for( ; ; )                 // endless loop    

Initialization

  • initializes variables used within the loop
  • if variables are declared within the loop, they are discarded after the loop completes
  • For example, in the following code the initialization variable i is declared outside the for loop; so it's value is still available once the loop completes
    int i;
    for ( i=0; i<10 ; i++ ) {
        // do something
    }
    System.out.println("value of i: " + i );
  • In the following code, x is declared and initialized inside the for-loop and is therefore only accessible within the loop
    for ( int x=0; x<10 ; x++ ) {
        // do something
    }
    // compile-error, cannot resolve symbol: x
    System.out.println("value of i: " + x );
  • can be more than one initialization statement but the variables must either be declared outside the for-loop or the type for the variables must be declared at the beginning
Following compiles and runs ok:

    for( int x=10, y=0; x>y; x--, y++){
        System.out.println( x + "\t" + y);
    }
    
Following produces compile error    

    int x;
    
    for( x=10, int y=0; x>y; x--, y++){
        System.out.println( x + "\t" + y);
    }    

Boolean expression

  • if the expression evaluates to true the loop continues; otherwise, the loop is exited and execution continues after the loop statment block

Iteration

  • if the expression evaluates to true, the block statements are executed and then the iteration occurs
  • if the expression evaluates to false, iteration does not occur

Break statement

  • you can use a break statement to exit a for-loop at any time
  • the break forces processing to the line following the for-loop statement block
    for( i=0; i<10; i++ ){
        if( i==5 ) break;
    }
    // process continues here after the break    

Continue statement

  • you can use continue to force processing to the next loop iteration
    for( i=0; i<10; i++){
        if( i==5)
            continue;   // skip printing 5
        else
            System.out.println(i);
    }

Example Code

Tips

  • all sections of the for() loop are optional

Traps

  • attempting to access a variable declared in the initialization outside of the for-loop
  • incorrect initialization expression
  • non-boolean expression

Statements if switch for while do
Labels Exceptions Handling Exceptions try-catch-finally