0

Break and continue

Hey guys im not sure to understand this one.. you can help? What is the output of this code? var sum=0; for(i=4; i<8; i++) { if (i == 6) { continue; } sum += i; } document.write(sum); 1

25th Jan 2019, 9:08 AM
Francis Bayouk
Francis Bayouk - avatar
1 Odpowiedź
+ 1
At the beginning: sum = 0 i = 4 First iteration: sum += i --> sum = 0 + 4 --> sum = 4 i++ --> i = 4 + 1 --> i = 5 Second iteration: sum += i --> sum = 4 + 5 --> sum = 9 i++ --> i = 5 + 1 --> i = 6 Third iteration: i == 6 is true, so continue is triggered, skipping the rest of the interation, so i is not added to the sum i++ --> i = 6 + 1 --> i = 7 Fourth iteration: sum += i --> sum = 9 + 7 --> sum = 16 i++ --> i = 7 + 1 --> i = 8 Loop ends: i<8 is false, so the loop ends sum = 16
25th Jan 2019, 9:28 AM
James
James - avatar