Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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 (i=0; i <= 9; i++){
     document.write(linebreak);

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

Is there a way I can learn loops inside out?

share|improve this question
3  
Same as anything else. Practice. –  Joel Etherton May 24 '12 at 14:21
1  
Easiest way: pencil it out on paper. –  Steve Evers May 24 '12 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. –  Crazy Eddie May 24 '12 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. –  William Shakespeare May 24 '12 at 16:15

4 Answers 4

up vote 5 down vote accepted

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.

share|improve this answer
    
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 May 24 '12 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 May 24 '12 at 16:27
    
+1 TLDR: "It's pretty simple actually... you have to think" –  ioSamurai May 24 '12 at 16:28

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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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