+ 1

Why the result is 43 instead of 11?

I’m sorry, I know Ima bad with math https://code.sololearn.com/W8kER5mzSkfH/?ref=app

9th Mar 2021, 9:42 PM
Rick Graymane
Rick Graymane - avatar
2 odpowiedzi
+ 3
It is 4+5+7+8+9+10 which is 43. The "continue;" doesn't break the loop. It just continues it. In other words, "continue" just skips the rest of the current iteration in the loop rather than breaking out of the loop as "break" does. Your code is: x=0 for(i=4; i<=10; i++) { if (i==6) { continue; } x +=i; } That works the same as: x=0 for(i=4; i<=10; i++) { if (i!=6) { x += i; } } Even if you replaced "continue" with "break", the result still won't be 11. 4 + 5 = 9 and the 6 won't be added because i is 5 the last time x += i runs.
9th Mar 2021, 9:46 PM
Josh Greig
Josh Greig - avatar
0
thanks!! ^^
9th Mar 2021, 10:51 PM
Rick Graymane
Rick Graymane - avatar