Declare multiple variables in for loop Example

You can define more than one variables and use them inside the for loop.

 
public class Main {
  public static void main(String[] args) {
    for (int i = 0, j = 1, k = 2; i < 5; i++)
      System.out.println("I : " + i + ",j : " + j + ", k : " + k);
  }
}

The output:


I : 0,j : 1, k : 2
I : 1,j : 1, k : 2
I : 2,j : 1, k : 2
I : 3,j : 1, k : 2
I : 4,j : 1, k : 2
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.