Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

nested loop race track

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
SOLUTION
Avatar of Shawn McCloskey
Shawn McCloskey
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

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?
Avatar of gudii9

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

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 ?
Avatar of gudii9

ASKER

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