+ 1

why it's result is 3??

var count = 0; for (x = 0; x <5; x++){ count++; x++; } document.write(count);

19th Oct 2016, 11:34 AM
Infoore
Infoore - avatar
12 Respuestas
+ 7
Lets try and break it down: var count = 0; //Count is set to 0 for (x = 0; //X is set to 0. x <5; //for loop will run so long as X is LESS THAN 5. x++) // X is incremented by 1 at the end of the loop { count++; // when your loop runs, add's 1 to "count" x++; // X is incremented by 1 again } Your loop is adding 2 to "X" on each loop. SO your first run will assign value as follows count = 1 x = 2 2nd run > count = 2 x = 4 3rd run > count = 3 x = 6 ^-Your loop stops as "X" is now GREATER than 5. Thus printing the value of "Count" it will display 3
20th Oct 2016, 3:29 PM
Pink Lemonade
+ 6
First time it runs the for loop with x=0, adds 1 to count,and 1 to x; Second time the for loop runs with x=2; then x=4(adding 1 to count every time) So the count will be 3
21st Oct 2016, 4:59 AM
Remmae
Remmae - avatar
+ 5
because you do x++ in the for and in the loop of for which means twice each loop
19th Oct 2016, 12:00 PM
‫שי קמחי‬‎
‫שי קמחי‬‎ - avatar
+ 3
in your loop the last number to which you can add 1 and get number smaller than 5 is 3. x++ means x=x+1
19th Oct 2016, 3:23 PM
Suzie
Suzie - avatar
+ 1
thanks to all.. specially to @Pink Lemonade!!
22nd Oct 2016, 3:28 AM
Infoore
Infoore - avatar
+ 1
I see.. thank you so much!! @Remmae
22nd Oct 2016, 3:30 AM
Infoore
Infoore - avatar
+ 1
It is so because x is being increased by 2 every time. x=0 count 1 X=2 count 2 X=4 count 3 Now x=6>5 so loop exits.
22nd Oct 2016, 4:27 PM
Abhishek Rajput
Abhishek Rajput - avatar
0
still not clear..!! anybody please mention in easy line!!
20th Oct 2016, 2:43 PM
Infoore
Infoore - avatar
0
Do you understand how loop for works?
20th Oct 2016, 2:46 PM
Suzie
Suzie - avatar
0
Yeah.. I know what is loop? but here is count ++ for printing but x++ for what?? @suzie
25th Oct 2016, 6:01 PM
Infoore
Infoore - avatar
0
Suzie thanks for the best ans!!
10th Nov 2016, 7:50 PM
Infoore
Infoore - avatar
0
потому что х увеличивается в цикле дополнительно на единицу и тем самым приближает финал.
15th Jan 2017, 1:52 PM
Володимир Нарижний
Володимир Нарижний - avatar