# Nested for loop; Easy example, still confused

Karen Haq

Greenhorn

Posts: 22

Nicola Garofalo

Ranch Hand

Posts: 308

Nicola Garofalo

Ranch Hand

Posts: 308

posted 6 years ago

I tell you how it works:

We start from the outer loop.

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 <= 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 <= 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

I still count

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 overI still count

**12 ***.Bye,

Nicola

posted 6 years ago

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

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

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

Posts: 22

posted 6 years ago

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 )

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 )

I agree. Here's the better link: Salvin.in

posted 6 years ago

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...

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

I agree. Here's the better link: Salvin.in

Campbell Ritchie

Sheriff

Posts: 48406

56

posted 6 years ago

take a deep breath....

there you go...

now everything has stopped spinning has it ?

there you go...

now everything has stopped spinning has it ?

I agree. Here's the better link: Salvin.in

Campbell Ritchie

Sheriff

Posts: 48406

56

Karen Haq

Greenhorn

Posts: 22

posted 1 month ago

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

Thank you,

Lee

posted 1 month ago

- 1

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?

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?

Junilu - [How to Ask Questions] [How to Answer Questions]

posted 1 month ago

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

I think you'll have to take care of one more cow in your farm, that could be challenging now