nested loop race track

gudii9
gudii9 used Ask the Experts™
on
Hi,

I am reading about nested loop here
http://study.com/academy/lesson/nested-for-loops-in-java.html

for (int i = 1; i <= 5; i++) {
 for (int j = 1; j <= i; j++) {
 }
}


Think of a nested loop as a race track within a race track. When you begin each lap on the main oval, you veer into another track inside the original (the nested track/loop). You may go around any number of times within the smaller track before looping around the main one. Then it starts over again until you've done the required number of laps on the main track. In all, you may go around a thousand times, or many more!



what it means by race track within a race track not able to visualize this image or picture
what it means by each lap on main oval and veer to another track inside original track??

please advise
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shawn McCloskeySystem Analyst
Commented:
OK, the description on the page that you referenced is pretty good but I will try to explain it a little better.  

It is easier to visualize how this works if you work from the inside out.  In the inside loop the break out condition is  'J <=i' or in word problem vernacular, continue increasing J as long as it is less than or equal to I.  Once J is greater than I, leave the loop.

Looking at the outside loop the condition is 'I <= 5' and this will be in word problem description, continue increasing I as long as it is less than or equal to 5, once it is it greater than 5 leave the loop,

So when this starts, I is 1,  and J is 1.  It enters into the loop, J, increases to 2, and it exits. That is the first iteration of the outer loop.
Now I gets increased to 2, and J gets reset to 1, it enters the inner loop, increases to 2, then increases once again to 3 and at this time it exits the loop.
It continues on again when I is 3, 4, then 5.  Each time it enters the inner loop the J loop increased by 1 longer round.

I think that what this is conveying is that if we are on loop 5 for I, then the visualization would be "we are going on the 5th outer lap for I, but before we do that, we need to do 5 inner laps for J"

I hope this helps you visualize it.
Commented:
Lets say that there is a wheel inside a wheel and the two wheels spins in the same direction. While the outer wheel makes one full loop the inside wheel makes i-full loops. For one full loop of the outer wheel the inner wheel makes i full loops.

Author

Commented:
Lets say that there is a wheel inside a wheel and the two wheels spins in the same direction. While the outer wheel makes one full loop the inside wheel makes i-full loops. For one full loop of the outer wheel the inner wheel makes i full loops.
any image or video to visualize this?
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
I think that what this is conveying is that if we are on loop 5 for I, then the visualization would be "we are going on the 5th outer lap for I, but before we do that, we need to do 5 inner laps for J"

I hope this helps you visualize it.
not completely any practical example on this?
Do you have a wristwatch? With hands? With a second hand ?

Author

Commented:
Do you have a wristwatch? With hands? With a second hand ?
perfect example. thank you
OK, so how do the minutes relate to the seconds ?

Author

Commented:
hours minutes and seconds are 3 hands of clock like 3 nested for loops
Right.

So whilst the clock is hard-wired to 60 (for minutes and seconds), loops can be any number inside any other number.

Commented:
loops inside loops in solar system
inner planets closest to the sun moves faster than outer planets
But the planets are all moving at the same time, which is not the case with loops.

Commented:
this is the case with nested loops. An outer planet is the outer loop, an inner planet is the inner loop. For one full loop of the outer planet around the sun, an inner planet (inner loop) can make 2,3 and much more loops.
Yes, I understand all that.
But planets are not a good analogy as they are all moving at the same time it gives a possible false impression that loops do too. With a clock, the movements are co reliant. If Neptune disappeared it would have no effect on Mercury.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial