Causes of Early Loop Termination : for « Statements « SCJP






Code in Loop              What Happens

break                     Execution jumps immediately to the 1st statement after the for loop.

return                    Execution jumps immediately back to the calling method.

System.exit()             All program execution stops; the VM shuts down.


public class MainClass{
    public static void main(String[] argv){
        System.out.println();
    }
    static boolean doStuff() {
      for (int x = 0; x < 3; x++) {
        System.out.println("in for loop");
        return true;
      }
      return true;
    }
    
}








5.3.for
5.3.1.The for() Loop
5.3.2.You are allowed to declare variables in the statement part.
5.3.3.Empty for() Loops
5.3.4.The for() Loop and the Comma Separator
5.3.5.You cannot mix expressions with variable declarations
5.3.6.You cannot have multiple declarations of different types.
5.3.7.Using comma to separate multiple declarations of a single type
5.3.8.A loop counter declared in the initialization section has a scope restricted to the loop.
5.3.9.Bare minimum for loop, with "{}" representing an empty code block:
5.3.10.Here are two more practical examples of for loops with missing parts:
5.3.11.skips the initialization section because the variable x is already initialized
5.3.12.You can have only one test expression.
5.3.13.Causes of Early Loop Termination
5.3.14.for loop may act like a while loop.
5.3.15.A for loop with multiple variables
5.3.16.The three expressions in the for statement don't need to operate on the same variables