Thursday, July 18, 2013

Declaring multiple variables in for loop

Hello Now let we see about declaring multiple variables inside the for loop.

PROGRAM:
~~~~~~~

import java.io.*;
public class aaa {

public static void main(String[] args) {

/*
* Multiple variables can be declared in declaration block of for loop.
*/

for(int i=1, j=2, k=3 ; i<6; i++,j++,k++)
System.out.println("I : " + i + ",j : "+ j + ", k : " + k);


}
}

OUTPUT
~~~~~~
I : 1,j : 2, k : 3
I : 2,j : 3, k : 4
I : 3,j : 4, k : 5
I : 4,j : 5, k : 6
I : 5,j : 6, k : 7

NOTE:
~~~~~
the following type of declaration won't compile.

1)We can only use the variables with different datatype inside for loop i.e..,

for(int i=0, float j; i < 5; i++)

2)we cannot declare more than one condition inside the for loop
for(int i=1, j=2, k=3 ; i<6,j<10,; i++,j++,k++)

No comments:

Post a Comment

Animated Social Gadget - Blogger And Wordpress Tips