Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For loop Containing another For loop

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am having 2.5 years of Experience in Java /J2EE development .
But in my career till now i never came across of a situation where writing For loop with in another for loop .


Please let me know in what cases , do we require to write a for loop in another for loop .

Please share your ideas .
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Just a simple example. I doubt that you have never seen this.>
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks , i will work on this and post more questions related to this .

(I think you changed your signature quote after this question )
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to write a code to parse some information from a TPF system where the data was in the form of a 2D array. Trivial, but a real life example nonetheless
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think after 2.5 years of Java you MUST have seen this. Nesting loops is the brute force way to iterate through nth degree arrays when you are doing a blind search for some element. It is the quintessential beginners search method. It is sometimes helpful to know if it is row-major or column-major.

Original:

The rest:


 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For my sudoku-solver I had a 5 times nested for loop
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think after 2.5 years of Java you MUST have seen this.



I think the OP meant that he hasn't had any reason to use one until now.
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I see, yes, yes, that makes sense.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nested loops are used quite a bit: collections of collections, particularly when recursion doesn't make sense, and any time there's a grid-like data structure being the most common in my experience. It can also be used a lot for XML processing, although often inner loops can (and should) be refactored into their own methods.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never understood nested For Loop Concepts in my study days .

But with Eclipse Debugger how simple it can be .

I suggest newbies to use Eclipse Debugger for this to understand .

A sample program :



The above program is for getting the following output

1
12
123
1234
12345
123456
1234567
12345678
123456789
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jake benn wrote:OIC, yes, yes, that makes sense.
OIC? We don't use such abbreviations here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic