+ 2
javascript break and continue
can someone explain to me please how the output of this code equal to 16 var sum=0; for(i=4; i<8; i++) { if (i == 6) { continue; } sum += i; } document.write(sum);
4 Respostas
+ 4
Ohhh i didn't know that, thank you so much
+ 1
Let's dig deeper and see how this code works!
i = 4, 4 < 8, 4 != 6, sum = 0, sum = sum + i = 0 + 4 = 4;
i = 5, 5 < 8, 5 != 6, sum = 4, sum = sum + i = 4 + 5 = 9;
i = 6, 6 < 8, 6 == 6, sum = 9, sum = sum + i - skipped because of continue;
i = 7, 7 < 8, 7 != 6, sum =9 , sum = sum + i = 9 + 7 = 16;
i = 8, 8 == 8 code stops working.
0
the numbers will be 4,5,7.
so sum= 4+5+7=16
0
Inderdaad it is 16 because we overstappen 6 and than continue with 7