Hi all,
I was trying out this for loop example and i notice that the for loop only works for the printing of x.The printing of y value did not work at all until the loop ends. I am trying to understand how for loop actually works. Am i right to say that for loop will only work for the first line of statement after the loop statement?I understand that if i insert this {},everything in this code works fine because it is looping as a block of code each time. Please help me in understanding the concept of this,Thanks in advance.
class ForLoop
{
public static void main(String args[])
{
int x,y;
y = 20;
for (x = 0; x < 5; x++)
System.out.println("This is x:" + x);
System.out.println("This is y:" + y);
y = y - 2;
}
}
My output of the code above:
This is x:0
This is x:1
This is x:2
This is x:3
This is x:4
This is y:20