Connecting Tech Pros Worldwide Forums | Help | Site Map

Hierarchy of nested "for" loops

MDR MDR is offline
Newbie
 
Join Date: Apr 2007
Posts: 14
#1: Apr 30 '07
Hello

I have three "for" loops, two nested into the outer one and they depend on each other, like this:

for (x=1; x<100; x++)
{
....
for (i=1; i<10; i++)
{....}
for (j=1; j<10; j++)
{...}
...
}

In the i loop we obtain a result that must be introduced in the j loop. I thought that it would do firstly the loop for i and then the loop for j, but it seems like if it were doing both loops at the same time, because when we cancell the j loop the result for the i one is correct, but with both working simoultaneously the result for i is perturbed by the second loop. Am I correct?

Thak you very much for your time and for your help!



Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Apr 30 '07

re: Hierarchy of nested "for" loops


Quote:

Originally Posted by MDR

Hello

I have three "for" loops, two nested into the outer one and they depend on each other, like this:

for (x=1; x<100; x++)
{
....
for (i=1; i<10; i++)
{....}
for (j=1; j<10; j++)
{...}
...
}

In the i loop we obtain a result that must be introduced in the j loop. I thought that it would do firstly the loop for i and then the loop for j, but it seems like if it were doing both loops at the same time, because when we cancell the j loop the result for the i one is correct, but with both working simoultaneously the result for i is perturbed by the second loop. Am I correct?

Thak you very much for your time and for your help!

The braces {} should tell you how your loops will behave.
MDR MDR is offline
Newbie
 
Join Date: Apr 2007
Posts: 14
#3: Apr 30 '07

re: Hierarchy of nested "for" loops


Quote:

Originally Posted by r035198x

The braces {} should tell you how your loops will behave.

ok, thanks but this doesn'tanswer my question.
My question is, which loop does it run before, "i" or "j", or they run simoultaneouslly? because they are at the same level within de "x" loop.
ilikepython's Avatar
Expert
 
Join Date: Feb 2007
Posts: 839
#4: Apr 30 '07

re: Hierarchy of nested "for" loops


Quote:

Originally Posted by MDR

ok, thanks but this doesn'tanswer my question.
My question is, which loop does it run before, "i" or "j", or they run simoultaneouslly? because they are at the same level within de "x" loop.

It runs whichever comes first. A for runs everything within its braces so it won't go to the second for loop until the first is finished.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#5: Apr 30 '07

re: Hierarchy of nested "for" loops


Quote:

Originally Posted by MDR

ok, thanks but this doesn'tanswer my question.
My question is, which loop does it run before, "i" or "j", or they run simoultaneouslly? because they are at the same level within de "x" loop.

Think about the answer I gave you again. Maybe it really does answer your question ...
Reply