# A Basic For Loop??

Jacob Michaels

Ranch Hand

Posts: 35

posted 13 years ago

Please help me understand how this could be.

I understand that the first for loop results to true and then moves to the second for loop and that results to true. The if statement resluts to true and the continue statement skips the println. It loops back to the second for loop and j++ increments the value to 1. The if statement results false so the println prints:

i = 0 j = 1

It then loops back to the second for loop and j++ now incrents to 2; 2 is less than 3 so once again the if statement is false and the println prints:

i = 0 j = 2

It then loops back to the second for loop and j++ now increments to 3; 3 is not less than 3 so the statement is false.

It then loops back to the first loop and i++ is now incremented to 1; 1 < 2; so

why does the value of j return to 0; and how does the value of j increment to 2; for the last println.

Thanks for any response

public class forTest

{

public static void main(String[] args)

{

for(int i = 0; i < 2; i++)

{

for(int j = 0; j < 3; j++)

{

if(i == j)

{

continue;

}

System.out.println("i = " + i + " j = " + j);

}

}

}

}

/* Answer

i = 0 j = 1

i = 0 j = 2

i = 1 j = 0

i = 1 j = 2

*/

I understand that the first for loop results to true and then moves to the second for loop and that results to true. The if statement resluts to true and the continue statement skips the println. It loops back to the second for loop and j++ increments the value to 1. The if statement results false so the println prints:

i = 0 j = 1

It then loops back to the second for loop and j++ now incrents to 2; 2 is less than 3 so once again the if statement is false and the println prints:

i = 0 j = 2

It then loops back to the second for loop and j++ now increments to 3; 3 is not less than 3 so the statement is false.

It then loops back to the first loop and i++ is now incremented to 1; 1 < 2; so

why does the value of j return to 0; and how does the value of j increment to 2; for the last println.

Thanks for any response

public class forTest

{

public static void main(String[] args)

{

for(int i = 0; i < 2; i++)

{

for(int j = 0; j < 3; j++)

{

if(i == j)

{

continue;

}

System.out.println("i = " + i + " j = " + j);

}

}

}

}

/* Answer

i = 0 j = 1

i = 0 j = 2

i = 1 j = 0

i = 1 j = 2

*/

Gabriel White

Ranch Hand

Posts: 233

posted 13 years ago

Hey Jacob!

I will walk you through this "Nested for loop."

The first for loop is the one that will keep incrementing regardless. So you declare that the first for loop counts from 0 to 1 (<2). It then goes to the nested for loop which counts until it reaches its end before returning to the original for loop (i loop). In this case your nested loop (j loop) goes from 0 to 2. Therefore on the first pass i=j right off the bat because 0=0 it then continues the j loop for the next number which is 1. That is why your output for the first println is i=0 j=1 because the j loop has not completed its counting yet. As you can see 0 does not equal 1 so it runs the println statement. Your j loop continues on counting to 2 where it is not equal to i again. Thus your second line of output is i=0 j=2. Now that your j loop is done counting, it returns out to the original i loop which is incremented by 1. Therefore i does not equal j (1 and 0) so that is your 3rd output: i=1 j=0. Next is your equal. When the j loop keeps counting it hits a 1 and then i=j so it goes to the next number in the j loop which is 2. Once again, i does not equal j and output is i=1 j=2. This completes the i loop so there is no more counting.

Thats it.

Hope this helps

I will walk you through this "Nested for loop."

The first for loop is the one that will keep incrementing regardless. So you declare that the first for loop counts from 0 to 1 (<2). It then goes to the nested for loop which counts until it reaches its end before returning to the original for loop (i loop). In this case your nested loop (j loop) goes from 0 to 2. Therefore on the first pass i=j right off the bat because 0=0 it then continues the j loop for the next number which is 1. That is why your output for the first println is i=0 j=1 because the j loop has not completed its counting yet. As you can see 0 does not equal 1 so it runs the println statement. Your j loop continues on counting to 2 where it is not equal to i again. Thus your second line of output is i=0 j=2. Now that your j loop is done counting, it returns out to the original i loop which is incremented by 1. Therefore i does not equal j (1 and 0) so that is your 3rd output: i=1 j=0. Next is your equal. When the j loop keeps counting it hits a 1 and then i=j so it goes to the next number in the j loop which is 2. Once again, i does not equal j and output is i=1 j=2. This completes the i loop so there is no more counting.

Thats it.

Hope this helps

Jacob Michaels

Ranch Hand

Posts: 35

Gabriel White

Ranch Hand

Posts: 233

posted 13 years ago

Hi Jacob,

Like I said before, it is a nested for loop. The j loop depends on the value of the i loop. If the i loop is still counting then it goes into the j loop. The j loop will count all the way through without stopping. When the j loop is finished counting from 0 to 2 it will then jump back out to the i loop (which is incremented now to 1) and then it goes back into the j loop and starts over. Once again, the j loop depends on the i loop. You can think of the i loop as the smart loop and the j loop as the dumb loop. When it is nested like that the i loop has priority over the j loop so the j loop will always start at its initial value regardless, because the i loop controls it.

[ March 27, 2003: Message edited by: Steve Wysocki ]

I am trying to figure out how

j = 2 and then it equals 0. What makes the value of j go back to zero.

Therefore i does not equal j (1 and 0) so that is your 3rd output: i=1 j=0. Next is your equal. When the j loop keeps counting it hits a 1 and then i=j so it goes to the next number in the j loop which is 2. Once again, i does not equal j and output is i=1 j=2.

Like I said before, it is a nested for loop. The j loop depends on the value of the i loop. If the i loop is still counting then it goes into the j loop. The j loop will count all the way through without stopping. When the j loop is finished counting from 0 to 2 it will then jump back out to the i loop (which is incremented now to 1) and then it goes back into the j loop and starts over. Once again, the j loop depends on the i loop. You can think of the i loop as the smart loop and the j loop as the dumb loop. When it is nested like that the i loop has priority over the j loop so the j loop will always start at its initial value regardless, because the i loop controls it.

[ March 27, 2003: Message edited by: Steve Wysocki ]

Jacob Michaels

Ranch Hand

Posts: 35

It is sorta covered in the JavaRanch Style Guide. |