+ 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.
27 Antworten
+ 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.
+ 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
+ 4
You have to increase i *before* the if statement. Adjust the starting value accordingly.
+ 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:)
+ 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.
+ 2
ChaoticDawg 👏👏👏👏👏👍☺️
+ 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.☺️
+ 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.
+ 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)
+ 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)
+ 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).
+ 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.
+ 1
ChaoticDawg as he said, for the first iteration the loop will get complete. Y caz when continue triggered then increment operation get skipped.
+ 1
It does not reach to i increment statement that is why it's infinite.
+ 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.
+ 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.
0
Vasiliy in your code i is only increased if i is odd.
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)
}
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}
0
Statement