File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes For Loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "For Loop " Watch "For Loop " New topic
Author

For Loop

Tiya Kaitha
Greenhorn

Joined: May 28, 2012
Posts: 2
I am new in java. And i am trying to understand nested for loop. But I have failed to do that. i just can not able to understand nested for loop. Here is a example.



output:
1
22
333
4444
55555

Can anyone tell me elaborately how its working? In which manner its working?? please tell me. please ...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
Welcome to the Ranch

Please avoid tabs for indenting, and use the code button. I have edited your post partially and you can see how much better it looks. If you use Eclipse (which is probably a bad idea at this beginning stage), you can set options to convert tabs to 4 spaces automatically. You are probably better off using a text editor, however.
I think you will only understand it if you go through it with a pencil and paper. Try this simplified version first, and you will see how a loop runs once.Once you have worked out how that simpler version prints 3 thrice, you will find it easier to work out how the two loops work.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

What part did you not understand? What did you expect that code to do?

Try this:


Does that help? The outer loop says, "Do whatever is in my body, 5 times." The body is printing i and then running the inner loop, which says, "do whatever is in my body, 3 times. And that body is simply printing j.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

nested for loops are like old-fashioned mechanical car odometers. The inner most one must go all the way around before the next outer one clicks over one...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

fred rosenberger wrote:nested for loops are like old-fashioned mechanical car odometers. The inner most one must go all the way around before the next outer one clicks over one...


New-fangled odometers are like that too, 'cep'n that it's electronomical instead of mechanicated.

Tiya Kaitha
Greenhorn

Joined: May 28, 2012
Posts: 2
Thanks a lot...... my brain is working now...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For Loop