Mega Code Archive

 
Categories / Java Book / 001 Language Basics
 

0056 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