- 1

JavaScript loops

What is the difference between the two codes? for(i = 0;i < 5;i++){       i += i;       Document.write(i); }  And for(i = 0;i < 5;i++){       Document.write(i);       i += i; } and i don't understand it ( i+=i) and outputs

16th Apr 2018, 9:58 PM
Chess
Chess - avatar
4 Answers
+ 2
i += i means i = i + i. Basically when you have, in more general terms: x += y it means x = x + y Now, I have explained what is going on with the two loops in the comments in this code. Go to the JS tab. You can also run it and see the outputs in the console Chess Check this out. https://code.sololearn.com/WN88XktetWc7/?ref=app
16th Apr 2018, 10:20 PM
cyk
cyk - avatar
+ 2
i+=i means i is incrementing with i i=5 i+=i means i+=5 so i=10 and for this two codes the second justs outputs i's value before the incrementation so different outputs
16th Apr 2018, 10:05 PM
᠌᠌Code X
᠌᠌Code X - avatar
0
i+=i means i is incremented by i. That means i = i + i In the 1st case, first i is incremented by i i.e. becomes 2i and then print the modified i. 2nd case, first i is printed and then modified i.e. increasing by i.
16th Apr 2018, 10:07 PM
Souvik
Souvik - avatar
0
thank you for your answers but i don't understand what is the output and why ?
16th Apr 2018, 10:19 PM
Chess
Chess - avatar