+ 1

Kotlin: Infinite Loop

Can somebody explain to me why the code below triggers an infinite loop? Does it not just skip the 5 and then keep incrementing till 10? var i = 0 while (i < 10) { if(i == 5) { continue } i++ }

23rd Feb 2021, 1:25 PM
Mike
Mike - avatar
2 Réponses
+ 3
Mike because you put "continue" when i is 5, the value is forever 5 and it is always skipping the i++ part. Use a break instead or put the i++ to the top.
23rd Feb 2021, 1:29 PM
bernborgess
bernborgess - avatar
0
Understood. Thanks!
23rd Feb 2021, 2:24 PM
Mike
Mike - avatar