+ 1
why it's result is 3??
var count = 0; for (x = 0; x <5; x++){ count++; x++; } document.write(count);
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
+ 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
+ 5
because you do x++ in the for and in the loop of for which means twice each loop
+ 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
+ 1
thanks to all.. specially to @Pink Lemonade!!
+ 1
I see.. thank you so much!! @Remmae
+ 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.
0
still not clear..!! anybody please mention in easy line!!
0
Do you understand how loop for works?
0
Yeah.. I know what is loop? but here is count ++ for printing but x++ for what?? @suzie
0
Suzie thanks for the best ans!!
0
потому что х увеличивается в цикле дополнительно на единицу и тем самым приближает финал.