Will this cause infinite loop ..If yes/no ..explain | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Will this cause infinite loop ..If yes/no ..explain

<? php $x=1; while (2> $x) { $x++; $x-=2; }

29th May 2017, 8:34 AM
Deepak Yadav
Deepak Yadav - avatar
2 Answers
+ 10
<? php $x=1; while (2> $x) { $x++; $x-=2; } can be rewritten as <? php $x=1; while ($x< 2) { $x-=1; } meaning $x is reduced by 1 in every iteration. So, $x always stays less than 2. Thus, it's an infinite loop.
29th May 2017, 9:41 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 7
😁 you soustrat x value end loop now $x is always 1
29th May 2017, 8:41 AM
Abbackr
Abbackr - avatar