Loops within loops (For Loops)

Hi,

I found a great resource for studying PHP - but I’m stuck on how to interpret “Loops within loops”.
The code I refer to is at the following page:

http://www.tuxradar.com/practicalphp/2/6/13

Can someone please explain the control flow? - I understand for (initialise, test, action) but not how this works with nested loops.

Any help appreciated.

for loops are simple, but in php they are set up kind of strange, here is a while loop that does the same thing:



$x = 0;
while($x < 10) {
    $x++;
    // Your other code
}

for($y = 0; $y < 10; $y++) {
    // Your other code
}


It’s more of a convenience thing…

So in nested loops the instructions continue as normal until they encounter the loop, then they continue in that loop, until they encounter the next, so:


for($y = 0; $y < 10; $y++) {
   echo 'got to a';

   for($y = 0; $y < 10; $y++) {
       echo 'got to c';

       for($y = 0; $y < 10; $y++) {
            got to 'e';
       }

       echo 'got to d';
   }

   echo 'got to b';
}

Your output will be:
got to a
got to c
got to e
got to d
got to b

Serj,
Thanks for that.
Below is my output from your code example. (I added an ‘echo’ for ‘go to e’ line - did you mean to leave it out?) and some <br / > tags

for($y = 0; $y < 10; $y++) {
   echo 'got to a <br />';

   for($y = 0; $y < 10; $y++) {
       echo 'got to c <br />';

       for($y = 0; $y < 10; $y++) {
		   echo 'got to e <br />';
       }

       echo 'got to d <br />';
   }

   echo 'got to b';
}

got to a
got to c
got to e
got to e
got to e
got to e
got to e
got to e
got to e
got to e
got to e
got to e
got to d
got to b

Can you tell me if I have this right?
for (initialise variable, test condition - if true execute statement, then increment this first instance or move on to the next nested loop).
Sorry I still don’t understand this and don’t know what questions to ask (isn’t that the definition of complete confusion?)

You would normally use a different variable for each for loop. In the above example, the 'got to e’s show 10 times because $y is reinitialised upon entering the loop, however the others are then not run due to $y already being above 10.

for($x = 0; $x < 10; $x++) {
   echo 'got to a <br />';
 
   for($y = 0; $y < 10; $y++) {
       echo 'got to c <br />';
 
       for($z = 0; $z < 10; $z++) {
		   echo 'got to e <br />';
       }
 
       echo 'got to d <br />';
   }
 
   echo 'got to b';
}

Is more practical, and is how it is done in the tutorial you mentioned. Individually however, your loops are perfect :slight_smile:

Thanks,

Could you please help me analyse the original code I posted. I would be interested to know if I have understood the flow correctly.

line 1: 	for ($i = 1; $i < 3; $i = $i + 1) {
Line 2:	****for ($j = 1; $j < 4; $j = $j + 1) {
Line 3:	********for ($k = 1; $k < 3; $k = $k + 1) {
line 4:************print "I: $i, J: $j, K: $k\
";
********}
****}
}

The output of that script is:

I: 1, J: 1, K: 1
I: 1, J: 1, K: 2
I: 1, J: 2, K: 1
I: 1, J: 2, K: 2
I: 2, J: 1, K: 1
I: 2, J: 1, K: 2
I: 2, J: 2, K: 1
I: 2, J: 2, K: 2

I understand how lines 1, 2 and 3 are output to read: I: 1, J: 1, K: 1 and how in this same iteration $k is incremented to the value of 2.
We then loop back to line 1 and follow the same method to output: I: 1, J: 1, K: 2

Don’t we then increment $k to 3 and in doing so render this loop inert as three is not less than three? - where do we go from here?

I don’t understand the next line of output: I: 1, J: 2, K: 1 (how does J become 2) and $k become 1 again.?

No, we don’t loop back to line 1. We stay inside line 3. The third loop isn’t exited while condition ($k < 3) is true.
So the first time you enter the ‘line 3’ loop, i = 1, j = 1 and k = 1.
Then k gets incremented to 2. 2 < 3 so the line 3 loop is executed again (i = 1, j = 1, k = 2).
Then k gets incremented to 3. 3 is not less than 3, so we exit the ‘line 3’ loop and go back to the ‘line 2’ loop.
j gets incremented to 2. 2 < 4 so the line 2 loop is executed again. $k is initialized at 1 (1 < 3) and the ‘line 3’ loop is executed (i = 1, j = 2, k = 1).

etc etc.

Once j gets incremented to 4 the line 2 loop is exited, and we go back to the line 1 loop. i is incremented to 2 (2 < 3) so the line 1 loop is executed again.
etc etc.


echo "start<br />";
for ($i = 1; $i < 3; $i = $i + 1) {
  echo "first loop, i = $i <br />";
  for ($j = 1; $j < 4; $j = $j + 1) {
    echo "second loop, j = $j <br />";
    for ($k = 1; $k < 3; $k = $k + 1) {
      echo "third loop, k = $k <br />";
      print "I: $i, J: $j, K: $k<br/>"; 
    }
    echo "third loop exited because k = $k<br/>";
  }
  echo "second loop exited because j = $j<br/>";
}
echo "first loop exited because i = $i<br/>";
echo "end<br />";

$i and $k wont equal 3 because your loop specifies less than… However with the code you posted it should display $j=3, I’m not sure why your output is different?

With the third output, when $j=1, $k loops through 1 and 2. Then $j is set to 2 and iterates through the k loop again. Seeing as the start parameter is $k=1, every time the loop is called it will reset at 1, no matter how high $j is set, $k will go through the exact same loop for every $j value

Thanks guys for all your input!
Guido,
I very much appreciate you taking the time to lay it all out for me, I understand now.
Travo,
Thank you for picking up the error:

However with the code you posted it should display $j=3, I’m not sure why your output is different?

I’ll have to inform the author http://www.tuxradar.com/practicalphp/2/6/13 of his error - wouldn’t you think he would test his code before publishing?
Thanks again guys!