Could you explain? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Could you explain?

var count=0; for(var i=0; i<=6; i++){ if(i==3){ i=5; continue; } count++; } document.write(count); //output is 4 //could you explain how did we get that output?

27th Aug 2019, 5:49 PM
Safaa Alnabhan
Safaa Alnabhan - avatar
3 Answers
+ 4
After 1. iteration: i=1; count = 1 After 2. iteration: i=2; count = 2 After 3. iteration: i=3; count = 3 After 4. iteration: i=6; count = 4 After the i=3 iteration the if clause will be used. So i=5 and after the continue the for loop will increase i to 6. After that the for loop condition isn't true anymore. :)
27th Aug 2019, 5:59 PM
Tom Hammerbacher
Tom Hammerbacher - avatar
+ 2
i=0 --> count++ i=1 --> count++ i=2 --> count++ i=3 --> i=5 i=6 --> count++
27th Aug 2019, 5:56 PM
daniel
daniel - avatar
+ 1
3 was skipped which means counter incremented 4 times
27th Aug 2019, 10:32 PM
Ukoha Obinna