Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

For Loop  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Marshal
Posts: 63426
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 12724
51
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot...... my brain is working now...
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!