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

Nested for loop; Easy example, still confused

 
Karen Haq
Greenhorn
Posts: 22
  • 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
  • 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
  • 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
  • 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: 12097
30
Chrome Java Linux
  • 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
  • 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
Posts: 1268
10
Eclipse IDE Google Web Toolkit Java
  • 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
Posts: 1268
10
Eclipse IDE Google Web Toolkit Java
  • 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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice diagrams, but they have got my head spinning.
 
salvin francis
Bartender
Posts: 1268
10
Eclipse IDE Google Web Toolkit Java
  • 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
Posts: 48652
56
  • 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
  • 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
Posts: 1268
10
Eclipse IDE Google Web Toolkit Java
  • 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.
 
Lee Baldwin
Greenhorn
Posts: 2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis, I know this topic is old, but the images you posted for it are no longer showing. Could you either post them again or send them to me? I would greatly appreciate the java loop visuals.

Thank you,
Lee
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lee Baldwin,

Welcome to the Ranch!

If you want a visual, just imagine a car odometer (one of those old analog kinds, not the new-fangled digital ones) with only two counter wheels. The wheel on the left side is like the outer for-loop. It counts 10s of miles traveled. The wheel on the right is like the inner for-loop. It counts single miles traveled and turns more times than the 10-mile wheel.

The single mile wheel will tick through the digits 0 to 9 (10 digits in all). Then it will come back around to 0 again. Every time it goes back to 0, the 10-mile wheel on the left ticks forward one digit. The 10-mile wheel also ticks through the digits 0-9 (10 digits in all). If this odometer were to start at 0-0, and we were to drive the car so that the odometer went all the way to 9-9, then rolled back over to 0-0, then we would have traveled a total of 10 (number of digits ticked through on the left wheel) times 10 (number of digits ticked through on the right wheel), or 100 miles total.

So in the case of the outer loop iterating from 0 to 3 and the inner loop 0 to 2, just imagine that the wheels on your odometer have only the numbers 0-3 on the left wheel and 0-2 on the right wheel. If this odometer were to tick through 0-0 all the way to 3-2, then roll back over to 0-0, the car would have traveled a total of 4 (number of digits ticked through on the left wheel) times 3 (number of digits ticked through on the right wheel) or 4 x 3 = 12 miles total. In the case of the nested for-loop, total number of iterations performed by the inner for-loop would also be 12.

The same idea and math would apply to a triple-, quadruple-, N-nested for-loop structure. You just take the number of times each loop will iterate and multiply all those together to get the total number of iterations that the innermost loop will perform. Just be aware that the more nested loops you have, the longer it will take to get all the way back around to 0-0-...-0. I've had my old Toyota Sienna for more than 15 years now and I only have 250,000+ miles on it. It's going to take a while to get that back to 000,000.

Does this help?
 
Liutauras Vilda
Bartender
Pie
Posts: 1838
44
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a very simple practical example though. Never seen similar to this here.
I think you'll have to take care of one more cow in your farm, that could be challenging now
 
Lee Baldwin
Greenhorn
Posts: 2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This helped me tremendously. Thanks for taking the time to explain.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic