Conditionals and Loops Break and Continue | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Conditionals and Loops Break and Continue

So I'm on the question that the code is: var sum=0; for(i=4; i<8; i++) { if (i == 6) { continue; } sum += i; } document.write(sum); And so if "i" starts at 4 and the loop continues as long as i is less than 8 and for as long as that is not the case "i" is incremented, then how come the result doesn't look like... "457"? And how come the answer is 16? When the sum is 0 and how come i is 8?

3rd Jun 2020, 10:43 PM
Jackson Davies
Jackson Davies - avatar
2 Answers
+ 1
Each time the for loop run, i is added to sum ( this is sum+=i , you could have write it sum=sum+i) So first loop i=4 --> sum=4 2nd loop i=5 --> sum =4+5=9 3rd loop i=6 --> continue 4th loop i=7 --> sum=9+7=16
4th Jun 2020, 12:38 AM
EmmanueLZ.
EmmanueLZ. - avatar
+ 2
It's an int and not a string.
3rd Jun 2020, 10:49 PM
Alex
Alex - avatar