File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested for loop; Easy example, still confused

 
Karen Haq
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
*
*
*
*

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Karen Haq
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1214
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
 
salvin francis
Bartender
Pie
Posts: 1214
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice diagrams, but they have got my head spinning.
 
salvin francis
Bartender
Pie
Posts: 1214
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
take a deep breath....
there you go...
now everything has stopped spinning has it ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not yet. I have stopped spinning but the world is still going round.
 
Karen Haq
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I LOVE the illustrations; they cement the concept perfectly, thanks. I'm a visual learner...
 
salvin francis
Bartender
Pie
Posts: 1214
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic