How can this loop is infinite? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can this loop is infinite?

fun main(args: Array<String>) { var sum = 0 var i = 1 while (i<=100) { if(i%2 != 0) { continue } i++ sum += i } println(sum) } I know, continue statment will skip all the odd numbers and only do sum of even numbers. But what is happening in loop?. Why it is endless?. It should stop when i = 100.

12th Dec 2021, 4:45 AM
kev_coding
kev_coding - avatar
27 Answers
+ 12
i is never incremented, because it reaches the continue statement first. So your while condition is always 1 <= 100 which is true, then the if condition is always true, because 1 % 2 equals 1. once continue or break is reached in a loop, none of the code beyond it in the loop is ran. It immediately goes to the next iteration.
12th Dec 2021, 4:51 AM
ChaoticDawg
ChaoticDawg - avatar
+ 5
You can also look at it from the opposite direction such that the condition checks whether the number was even (rather than odd) and only add to <sum> when condition is satisfied. if <i> mod 2 equal zero add <i> to <sum> end if increment <i> by 1 Alternatively, you can choose to initiate <i> by 2 - then increment <i> by 2 on each loop iteration. That way no odd/even check will be necessary because <i> will jump on even numbers only. let <i> = 2 while <i> <= 100 add <i> to <sum> increment <i> by 2
12th Dec 2021, 5:19 AM
Ipang
+ 4
You have to increase i *before* the if statement. Adjust the starting value accordingly.
12th Dec 2021, 4:58 AM
Simon Sauter
Simon Sauter - avatar
+ 3
Simon Sauter ChaoticDawg I got it. So, 'continue' statement will just skip the whole iteration. I thought it would skip values of odd numbers and will go on to i++ and sum+=i. Thank you both:)
12th Dec 2021, 5:06 AM
kev_coding
kev_coding - avatar
+ 3
Vasiliy because now you're only incrementing i once in the loop to the value of 2 then the if condition isn't met and i stays at the value of 2 resulting in a similar infinite loop.
12th Dec 2021, 5:31 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
ChaoticDawg 👏👏👏👏👏👍☺️
12th Dec 2021, 5:41 AM
Solo
Solo - avatar
+ 2
Simon Sauter, i agree with you, but why is there no output in the code I suggested, because there should be access to print i.☺️
12th Dec 2021, 6:41 AM
Solo
Solo - avatar
+ 2
Simon Sauter 👏👏👏👏👏👍, I'm glad that my efforts to push you to this conclusion were not in vain. ☺️ It remains only to find out the reason for this behavior from Kotlin. 🤔 But this is for the sake of curiosity. The most important thing that we learned is that in Kotlin you can freely play with loops, unlike JavaScript, where, due to an infinite loop, the SoloLearn application immediately hangs, to the point that the phone hangs, which has to be rebooted. So if you learn JavaScript and are not well versed in the correct construction of loops, then it is better to do it in Kotlin, because their syntax is identical, with the exception of data output.
12th Dec 2021, 7:37 AM
Solo
Solo - avatar
+ 1
ChaoticDawg 😏 Ok, but if 1% 2 = 1, then why doesn't this code work? var sum = 0 var i = 1 while (i<=10) { if(i%2 != 0){ i++ continue } sum += i } println(sum)
12th Dec 2021, 5:25 AM
Solo
Solo - avatar
+ 1
😏Okay, but will this code work? var sum = 0 var i = 1 while (i<=10) { if(i%2 != 0){ println(i++) continue } sum += i } println(sum)
12th Dec 2021, 5:53 AM
Solo
Solo - avatar
+ 1
No. In while loops you have to make sure that the counter is incremented in every iteration. So whenever you use conditionals inside a while loop you have to make sure that the counter is incremented whether the condition is true or not (or in if - else if constructions no matter which of the conditions is true).
12th Dec 2021, 6:28 AM
Simon Sauter
Simon Sauter - avatar
+ 1
It seems that the code isn't executed at all if it includes an infinite loop. I don't know if that's a Kotlin thing or a sololearn thing. Even if you put a print statement *before* the while loop there is no output.
12th Dec 2021, 6:57 AM
Simon Sauter
Simon Sauter - avatar
+ 1
ChaoticDawg as he said, for the first iteration the loop will get complete. Y caz when continue triggered then increment operation get skipped.
12th Dec 2021, 7:44 PM
Mallikarjunagoud A
Mallikarjunagoud A - avatar
+ 1
It does not reach to i increment statement that is why it's infinite.
13th Dec 2021, 2:32 AM
Abdurrahman Noori
Abdurrahman Noori - avatar
+ 1
You should also do an increment operation inside the if statement: if(i%2 != 0){ i++ continue } Because continue does not mean you skip the if conditional check and go to the next lines of code... it actually loops back to the start of the while loop and thus passes the value to the if conditional statement again. If you did not change it before the continue statement, the loop becomes infinite.
13th Dec 2021, 6:27 AM
Bob_Li
Bob_Li - avatar
+ 1
https://code.sololearn.com/czYCJIwlmgk3/?ref=app Perhaps something like this? The println helps you trace the flow of the variable i. But one problem with this is the initial value of i=1 is not passed down. You should think of a better way to structure this program.
13th Dec 2021, 11:50 AM
Bob_Li
Bob_Li - avatar
0
Vasiliy in your code i is only increased if i is odd.
12th Dec 2021, 5:28 AM
Simon Sauter
Simon Sauter - avatar
0
Your code is stuck at i = 1 iteration; fun main(args: Array<String>) { var sum = 0 var i = 1 while (i<=100) { if(i++%2 != 0) { continue } sum += i } println(sum) }
12th Dec 2021, 12:28 PM
Ifeanyichukwu Otiwa
0
The only one mistake u did is that u incremented i outside the loop Because of which the value of i is not incremented and i remains one. syntax of if is.. if{ condition statement increment}
13th Dec 2021, 8:01 AM
Sonali Kala
Sonali Kala - avatar
0
Statement
13th Dec 2021, 10:00 AM
Raja Deekshith