Need help with loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need help with loops

Code1 for(x=0; x<6; x++){ document.write(x) } output => 012345 Code2 for(x=0; x<6; x++){ } document.write(x) output =>6 Why is the output 6 but not 5 in the second code?

10th Jan 2021, 4:27 PM
★_★_★
★_★_★ - avatar
2 Answers
+ 4
★_★_★ Because in second loop after checking 5 < 6, x will be increment by 1 for next checking. So now x is 6 but 6 < 6 is false so x will not be increment by 1 so finally x will be 6
10th Jan 2021, 4:34 PM
A͢J
A͢J - avatar
+ 1
Maybe bcuz in the second code u wrote the document.write element outside the {} with the for element
11th Jan 2021, 6:50 AM
JustSteve