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 Containing another For loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "For loop Containing another For loop" Watch "For loop Containing another For loop" New topic
Author

For loop Containing another For loop

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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 .


Save India From Corruption - Anna Hazare.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700



Just a simple example. I doubt that you have never seen this.>


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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

Joined: Mar 20, 2007
Posts: 317
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

Joined: Jun 05, 2009
Posts: 82
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

Joined: Oct 25, 2008
Posts: 2700

For my sudoku-solver I had a 5 times nested for loop
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
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

Joined: Jun 05, 2009
Posts: 82
Oh I see, yes, yes, that makes sense.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Apr 18, 2009
Posts: 2234

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

Joined: Oct 13, 2005
Posts: 39773
    
  28
jake benn wrote:OIC, yes, yes, that makes sense.
OIC? We don't use such abbreviations here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For loop Containing another For loop