• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A Basic For Loop??

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
*/
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jacob Michaels
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That does help. I am trying to figure out how
j = 2 and then it equals 0. What makes the value of j go back to zero.
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jacob,

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think a Light Bulb went on in my head!
Thank you for a great explanation!
[ March 27, 2003: Message edited by: Jacob Michaels ]
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem, please come back if you have any further questions.
Peace out
Steve
 
reply
    Bookmark Topic Watch Topic
  • New Topic