+ 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);

4th May 2020, 2:59 PM
zinwar zardasht
zinwar zardasht - avatar
4 Respostas
+ 4
Ohhh i didn't know that, thank you so much
4th May 2020, 3:07 PM
zinwar zardasht
zinwar zardasht - avatar
+ 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.
12th Jan 2023, 6:30 AM
Giorgi Datuashvili
0
the numbers will be 4,5,7. so sum= 4+5+7=16
27th May 2021, 10:16 PM
ARJUN MK
ARJUN MK - avatar
0
Inderdaad it is 16 because we overstappen 6 and than continue with 7
7th Aug 2021, 3:20 PM
Kayombya Jean