aspose file tools*
The moose likes Other Languages and the fly likes nested for loop not working properly ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Languages » Other Languages
Bookmark "nested for loop not working properly ?" Watch "nested for loop not working properly ?" New topic
Author

nested for loop not working properly ?

Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216


output is i=1 j=1
i=1 j=2
the problem is after it should have print i=2 j=1
i=2,j=2
but they dont so i want to know how nested loop is working in this.................


OCPJP 6 86%
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2854
    
  11

First, please use code tags and proper indentation when posting code. That makes it much easier to read. I've added the code tags for you here.

You initialize i and j before the loops, and never reinitialize j. So the process is:

i = 1, enter the outer loop
j = 1, enter the inner loop
print i=1, j=1
increment j
j = 2, enter the inner loop again
print i=1,j=2
increment j
j = 3, exit the inner loop
increment i
i = 2, enter the outer loop again
j = 3 (still) bypass inner loop
increment i
i = 3, exit outer loop

Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

yeah, i missed it, thank you so much Greg.
wonderful explanation.
and sorry for no code tags.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: nested for loop not working properly ?