Mega Code Archive

 
Categories / Java Book / 001 Language Basics
 

0058 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