jasmin0510 Posted December 25, 2020 Share Posted December 25, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311922-nested-loops/ Share on other sites More sharing options...
Barand Posted December 25, 2020 Share Posted December 25, 2020 "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. Quote Link to comment https://forums.phpfreaks.com/topic/311922-nested-loops/#findComment-1583394 Share on other sites More sharing options...
jasmin0510 Posted December 25, 2020 Author Share Posted December 25, 2020 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. 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. ☹️ Quote Link to comment https://forums.phpfreaks.com/topic/311922-nested-loops/#findComment-1583396 Share on other sites More sharing options...
Barand Posted December 25, 2020 Share Posted December 25, 2020 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 1 Quote Link to comment https://forums.phpfreaks.com/topic/311922-nested-loops/#findComment-1583398 Share on other sites More sharing options...
jasmin0510 Posted December 25, 2020 Author Share Posted December 25, 2020 (edited) 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 December 25, 2020 by jasmin0510 Quote Link to comment https://forums.phpfreaks.com/topic/311922-nested-loops/#findComment-1583400 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.