SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Perth W.A.
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    SitePoint Member Serj's Avatar
    Join Date
    Nov 2011
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for loops are simple, but in php they are set up kind of strange, here is a while loop that does the same thing:

    Code:
    $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:

    Code:
    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

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Perth W.A.
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    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

    Code PHP:
    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?)

  4. #4
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    PHP Code:
    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

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Perth W.A.
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    PHP Code:
    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\n";
    ********}
    ****}

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

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Argent View Post
    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
    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.


    PHP Code:
    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 />"

  7. #7
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $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

  8. #8
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Perth W.A.
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •