Using the Comma

Java allows two or more variables to control a for loop.

And you can include multiple statements in both the initialization and iteration portions of the for.
Each statement is separated from the next by a comma.
Here is an example:
public class Main {
  public static void main(String args[]) {

    for (int a = 1, b = 4; a < b; a++, b--) {
      System.out.println("a = " + a);
      System.out.println("b = " + b);

    }
  }
}

The program generates the following output:


a = 1 
b = 4 
a = 2 
b = 3
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.