Jump to content

Nested loops


jasmin0510

Recommended Posts

Hi guys and Marry Christmas to all of you who are celebrating!

I'm traying to learn PHP on my own and I am stuck in traying to understand this concept of nested loops and using break/continue in them.

 So, can someone please explain to me how this code below is working and why I'm getting result that I don't expect.

Code is: 

for($i = 0; $i < 6; $i++) { 
    for($d = 0; $d < 4; $d++) { 
        if($d == $i) { 
            continue 2;
        }
        echo $d."d <br>";
        echo $i."i <br>";
        
        
    }
    
} 

When I was writing this code I was expecting that result, because of continue 2, will be 4i and 5i. But instead of that I got this 0d,1i,0d,2i,1d,2i,0d,3i,1d,3i,2d,3i,0d,4i,1d,4i,2d,4i,3d,4i,0d,5i,1d,5i,2d,5i,3d,5i. 

I really want to how I got this result, I spent whole day traying to explain this to myself and failed miserably.

 

Link to comment
Share on other sites

"continue" says "Stop processing this iteration and continue with the next. Just a single "continue (1)" would take you to the start of the inner loop and start with the next value of $d. Because you have "continue 2" it goes to the start of the outer loop and continues with the next value of $i.

image.png.7ca40fa8fa9081119deb3c6225e8b86e.png

Link to comment
Share on other sites

2 hours ago, Barand said:

"continue" says "Stop processing this iteration and continue with the next. Just a single "continue (1)" would take you to the start of the inner loop and start with the next value of $d. Because you have "continue 2" it goes to the start of the outer loop and continues with the next value of $i.

image.png.7ca40fa8fa9081119deb3c6225e8b86e.png

Thank you for this answer. 

Yeah, I understand that basic concept of continue and continue 2. But the results that I'm getting in this example are beyond my understanding and expectations. 

In first iteration $i = 0 and $d = 0, so the condition in if is TRUE and continue 2 is stopping further execution of code in this first iteration for both for loops, meaning that those two echo lines are not executed. So the iteration number 2 begins and now $i = 1 and $d = 1 and again condition in if is TRUE and continue is stopping that iteration. 

This is my understanding of things. What I don't understand is how I'm getting  this: 0d,1i,0d,2i,1d,2i,0d,3i,1d,3i,2d,3i,0d,4i,1d,4i,2d,4i,3d,4i,0d,5i,1d,5i,2d,5i,3d,5i.

I tried to resolve this with trace tables but that didn't help me. ☹️

Link to comment
Share on other sites

Get a pencil and read through the code line by line as if it were being executed.

For each iteration, note the values of i and d and the values that would be output.

You should get this (try it yourself before peeping)

ITERATIONS
 -----+------+---------
 i      d       Ouput
 -----+------+---------
 0      0        continue to next i
        1
        2
        3
 1      0        0d 1i      
        1        continue to next i
        2
        3 
 2      0        0d 2i
        1        1d 2i      
        2        continue to next i
        3
 3      0        0d 3i
        1        1d 3i
        2        2d 3i      
        3        continue to next i
 4      0        0d 4i
        1        1d 4i
        2        2d 4i
        3        3d 4i
 5      0        0d 5i
        1        1d 5i
        2        2d 5i
        3        3d 5i
 

  • Thanks 1
Link to comment
Share on other sites

44 minutes ago, Barand said:

Get a pencil and read through the code line by line as if it were being executed.

For each iteration, note the values of i and d and the values that would be output.

You should get this (try it yourself before peeping)

 

  Reveal hidden contents

 





ITERATIONS
 -----+------+---------
 i      d       Ouput
 -----+------+---------
 0      0        continue to next i
        1
        2
        3
 1      0        0d 1i      
        1        continue to next i
        2
        3 
 2      0        0d 2i
        1        1d 2i      
        2        continue to next i
        3
 3      0        0d 3i
        1        1d 3i
        2        2d 3i      
        3        continue to next i
 4      0        0d 4i
        1        1d 4i
        2        2d 4i
        3        3d 4i
 5      0        0d 5i
        1        1d 5i
        2        2d 5i
        3        3d 5i
 

 

 

 

You really got me motivated, and I was in despair. I told my self - if this man thinks that I should try again than I will do it. I just reduced the number of iterations, make trace table and I had that moment of revelation😄

I didn't look up your hidden content in post, and I am glad that I didn't. 

Thank you very much. Have a wonderful holidays.🤗

 

Edited by jasmin0510
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.