The for() Loop and the Comma Separator : for « Statements « SCJP

Home
SCJP
1.Java Source And Data Type
2.Operators
3.Modifiers
4.Type Casting
5.Statements
6.Object Oriented
7.Thread
8.Utility Classes
9.File
SCJP » Statements » for 
5.3.4.The for() Loop and the Comma Separator
public class MainClass {
  public static void main(String[] argv) {
    int j, k;
    for (j = 1, k = 2; j + k < 20; j++, k += 2) {
      System.out.println("j is " + j + " k is " + k);
    }
  }
}
j is 1 k is 2
j is 2 k is 4
j is 3 k is 6
j is 4 k is 8
j is 5 k is 10
j is 6 k is 12
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.