aspose file tools*
The moose likes Beginning Java and the fly likes Nested for loop; Easy example, still confused Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested for loop; Easy example, still confused" Watch "Nested for loop; Easy example, still confused" New topic
Author

Nested for loop; Easy example, still confused

Karen Haq
Greenhorn

Joined: Mar 17, 2010
Posts: 22
Hi,
Each iteration of outer loop executes one iteration of inner loop, right? Why is the output twelve astericks instead of seven?

Source code:


Output:

*
*
*
*
*
*
*
*
*
*
*
*

Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Well three times four is exactly twelve

You execute 3 times the outer loop. Each outer loop you execute 4 times the inner loop...

first outer:
*
*
*
*
second outer:
*
*
*
*
third outer:
*
*
*
*


Bye,
Nicola
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Karen Haq wrote: Each iteration of outer loop executes one iteration of inner loop, right?


No,How ? inner for loop execute continuously until the condition fails
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
I tell you how it works:

We start from the outer loop. j=1
j=1 <= 3? YES

we jump into the inner loop: k=1
(k=1 <= 4 ?) YES, ok we print * and we go on, k++


(k=2 <=4 ?) YES, ok we print * and we go on, k++

(k=3 <=4 ?) YES, ok we print * and we go on, k++

(k=4 <=4 ?) YES, ok we print * and we go on, k++

(k=5 <=4 ?) NO. Inner loop is over. We continue with the outer loop

j=2
j=2 <= 3? YES, we jump again in the inner loop
(k=1 <= 4 ?) YES, ok we print * and we go on, k++


(k=2 <=4 ?) YES, ok we print * and we go on, k++

(k=3 <=4 ?) YES, ok we print * and we go on, k++

(k=4 <=4 ?) YES, ok we print * and we go on, k++

(k=5 <=4 ?) NO. Inner loop is over. We continue with the outer loop

j=3
j=3 <= 3? YES, we jump again in the inner loop
(k=1 <= 4 ?) YES, ok we print * and we go on, k++


(k=2 <=4 ?) YES, ok we print * and we go on, k++

(k=3 <=4 ?) YES, ok we print * and we go on, k++

(k=4 <=4 ?) YES, ok we print * and we go on, k++

(k=5 <=4 ?) NO. Inner loop is over. We continue with the outer loop

j=4
j=4 <= 3? NO the outer loop is over

I still count 12 *.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

Each iteration of outer loop executes one iteration of inner loop, right?
Nope.

Each iteration of the outer loop executes the entire inner loop. A great way to see what's happening would be to change this:

to this:

(or something like that - my syntax may be off a bit

this way you can see that the full inner loop runs for each iteration of the outer loop


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Karen Haq
Greenhorn

Joined: Mar 17, 2010
Posts: 22
Okay, I'll work on understanding nested loops. I hate them, don't be surprised if I post again in the future. Thanks for the help.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Here is an idea that came to my mind when reading your post

The Big 'cog' represents outer loop and the small 'cog' represents inner loop.


When the Big 'cog' rotates once, the smaller cog rotates several times....




(I am not that good at drawing )


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Taking the concept to a 3 level loop:



Think of how many times the last one will turn if the outer turns one complete time...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39865
    
  28
Nice diagrams, but they have got my head spinning.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

take a deep breath....
there you go...
now everything has stopped spinning has it ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39865
    
  28
Not yet. I have stopped spinning but the world is still going round.
Karen Haq
Greenhorn

Joined: Mar 17, 2010
Posts: 22
I LOVE the illustrations; they cement the concept perfectly, thanks. I'm a visual learner...
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Designing graphical stuff was and always will be my hobby (check my site)

I am planning to make some good illustrations for stuff like design patterns, etc. do let me know if you have ideas too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested for loop; Easy example, still confused