0

I know how to do simple loops but I don't know what's going on when many loops are working together.

For example:

for (var i=0; i <= 9; i++){
     document.write(i);

  for (var m=0; m <= 9; m++){
     document.write(i);
    }
}

Is there a way I can learn loops inside out?

4
  • 3
    Same as anything else. Practice. Commented May 24, 2012 at 14:21
  • 2
    Easiest way: pencil it out on paper. Commented May 24, 2012 at 14:32
  • 1
    People are used enough to seeing i < 10 when the intent is to loop through the range [0,9] that when we see i <= 9 we have to pause and wonder WTF is going on. Prefer the former unless there's a good reason not to. Commented May 24, 2012 at 15:21
  • Have you tried running that code? You should be able to see exactly what happens. And if it still doesn't make sense, try stepping through it in a debugger -- it's pretty straightforward.
    – Caleb
    Commented May 24, 2012 at 16:15

4 Answers 4

6

It's pretty simple actually... you have to think step by step.

You enter your first loop with i = 0. The condition is clear, it will not stop to execute what's inside {} while i is inferior or equals 9.

Let's see what comes next... you call document.write(). I guess you know exactly what it does.

Then... HUHO! ANOTHER LOOP! chill out, it's exactly the same thing.

You start your second loop with m = 0 and won't stop while m is inferior or equals 9. Then you write what's inside i using document.write() You're at the end of your second loop context so you re-evaluate what's inside m. m is now equals to m+1 (due to the m++). m equals 1 and as 1 is inferior or equal to 9 it will loop again (it will then write again what's inside i, then add 1 to m). Now m is equals to m+1 (2)... etc. etc.

When m will equal 10 then m will not be inferior or equal to 9 anymore so you will stop looping in your second loop, and continue your first loop execution.

So now i is equals to 1 (due to i++), i is inferior or equal to 9 so you will execute your first loop again. Inside your first loop there is the document.write() call but there is the second loop. You enter your second loop with m set to 0... and it goes on and on...

But as you always write what's inside of i, you will have ten 0 displayed, then a linebreak then ten 1 displayed, then a linebreak... and so on until 10.

4
  • Are you a teacher? if not you should look into it. Explosions in brain just cleared up after i read your simple breakdown of the loop.
    – AppSensei
    Commented May 24, 2012 at 15:39
  • Glad you master the nested loops now. I am no teacher, but as I said, there is nothing hard with nested loop... only some tricks for the brain.
    – lvictorino
    Commented May 24, 2012 at 16:27
  • +1 TLDR: "It's pretty simple actually... you have to think"
    – MetaGuru
    Commented May 24, 2012 at 16:28
  • One trick is to visualise the nested loops as two dimensional matrix with row and column as outer and inner indices respectively.
    – CodeYogi
    Commented Apr 21, 2018 at 21:33
2

getting more familiar with nested loops involves using them more first of all, which can be tricky because it best to avoid nesting loops when you can. Other things that help are using better (more descriptive) names for your loop variables. Taking time to manually step through code can really help increase your understanding of code, go through line by line and figure out what each variable will be at the time and write what any output would be.

1

The best way to learn about loops without doubt is debugging. Just observe your loop variables such as counters and conditions.

Observe the process step by step, check conditions and follow counters. You will get it very quickly.

1

You say you understand simple loops; well break the nested loop into simple loops. Think of the inner loop as a function call. So what's the outer loop do? Call that function for each iteration of the loop. Now think of that 'function' - it's just another simple loop. So for each call to that 'function', you run that loop then 'return' from the function. Now you're back in your simple outer loop... etc. etc.

Not the answer you're looking for? Browse other questions tagged or ask your own question.