Forums Register Login

Nested for loop; Easy example, still confused

+Pie Number of slices to send: Send
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:

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

+Pie Number of slices to send: Send
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:
*
*
*
*

+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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 *.
+Pie Number of slices to send: Send
 

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

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 )
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
Nice diagrams, but they have got my head spinning.
+Pie Number of slices to send: Send
take a deep breath....
there you go...
now everything has stopped spinning has it ?
+Pie Number of slices to send: Send
Not yet. I have stopped spinning but the world is still going round.
+Pie Number of slices to send: Send
I LOVE the illustrations; they cement the concept perfectly, thanks. I'm a visual learner...
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
(1 cow) 1
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
This helped me tremendously. Thanks for taking the time to explain.
Let's get him boys! We'll make him read this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 17056 times.
Similar Threads
Question about continue in while loop
New SCJP mock exam
Flow Control
why does 'break' traverse one more time
labeled continue statement?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:45:07.