+ 2

Why infinite loop gets created when num is decremented after continue statement?

Code attached below https://code.sololearn.com/cv6BNEnZ09wE/?ref=app

14th Jan 2019, 12:44 PM
Amit Joshi
Amit Joshi - avatar
2 Réponses
+ 4
At first, num is 5. Then it is decremented to 4 (num--) and 3 (num--). Then, when num equals 3, the loop will continue (i.e. jump back to the beginning of the loop) and num-- won't be executed anymore. num will stay at 3, the condition while(num > 0) will stay true and there will be an infinite loop.
14th Jan 2019, 1:03 PM
Anna
Anna - avatar
+ 2
Have you tried to debug the code, this usually gives more insight to the code. when num == 3, you use the continue statement which kind of works like a break statement. but instead of breaking the loop or whatever it will go to the next iteration without subtracting num, thus creating an infinite loop. Hope this helps!
14th Jan 2019, 1:04 PM
Joery De Loose
Joery De Loose - avatar