0

Could someone explain this to me? I don't understand this one.

var sum=0; for(i=4; i<8; i++) { if (i == 6) { continue; } sum += i; } document.write(sum); why does the result of this code is : 16?? Help me pleaseeee....

18th Mar 2020, 1:34 PM
John Mark
John Mark - avatar
2 Antworten
+ 4
Variable i assumes values 4, 5, 6, 7 for each iteration which executes loop body. If i is 6, we proceed to the next iteration without doing anything, else, the program adds i to the sum variable. Hence sum is 4+5+7 16
18th Mar 2020, 1:39 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Continue keyword skips the rest of the code inside the loop and the control is transferred back to the beginning of the for loop. So when i=6 the 'sum' is not updated. The values added up are 4,5,7 and their addition is 16.
18th Mar 2020, 1:49 PM
Avinesh
Avinesh - avatar