+ 2
Why does this code have an output of 8? var z=0; for (var x=0;x<4;x++) { z+=2; } document.write(z);
22 Réponses
+ 17
0 + 2 = 2 (First loop) / 2* + 2 = 4 (2nd loop) / 4* + 2 = 6 (3rd loop) / 6* + 2 = 8 (4th loop) . The first value in the expression is carried from the earlier loop (the ones with asterisk).
+ 8
x=0 z=2 ,x=1 z=4 , x=2 z=6 , x=3 z= 8
+ 4
x starts at 0 and increments by 1 until it reaches 4 each time 2 is added to z thats 4 times which is 8
+ 4
x=0 <4 true 》 z=z+2=0+2=2 》x=x+1=0+1=1 》
x=1 <4 true 》 z=z+2=2+2=4 》×=x+1=1+1=2 》
x=2 <4 true 》 z=z+2=4+2=6 》x=×+1=2+1=3 》
x=3 <4 true 》 z=z+2=6+2=8 》x=x+1=3+1=4 》
x=4 <4 false 》stop loop 》z=8
+ 3
Answer to Namita question about z = 6:
You are right, last loop will execute with a value of x = 3, then x will increase (3 + 1 = 4) and loop will stop. But loop will start with a value of x = 0 (1st loop), then with x = 1 (2nd loop), then with x = 2 (3rd loop) and final loop with x = 3 (4th loop). And z = 8 in result.
+ 2
The loop runs for x=0,1,2,3 and each time adds 2 to z. 4*2=8
+ 2
we can give each loop a certain name...the first one, namely, zero loop, and so on...you 'll see we can only reach to forth one, namely, third loop...so we have to add 2 to the zero four times...
+ 2
the statement "x++" means that x will be in incremented at the end of the statement the code is executing. In this case that statement is the entire loop, so x is not equal to 4 untill after Z is set to 8
+ 1
That makes more sense now! I still have a question... if the condition is "x<4" how will the code continue for when x is equal to 4? Wouldn't it stop at x = 3.... making z=6?
+ 1
yes it stops at 3 and then it add 1 to 3
+ 1
your loop starts at 0, so 4 loops is 0 1 2 3.
+ 1
yes final loop finishes at x=3..but then, to finsh the rest of the code, the (z+=2) gets executed once more thus making z=8. then it comes out of for loop..
+ 1
thank they
+ 1
x=0,z=0+2=2;x=1,z=2+2=4;x=2,z=4+2=6;x=3,z=6+2=8;x=4 condition false cntrol come out of loop
+ 1
when x=0,z=2 and when x=1,z=4, and when x=2,z=6 and finally when x=3,z=8
+ 1
Because you have used for loop for x till it gets value as 4 and z += 2 makes z double of x in the count.
+ 1
It's simply because it's looping four times and each time it loops z is added to its self plus two. the last time it loops it is 6 + 2
0
Skip to main content
Menu

Menu
Search View topic
Enter your search term
Skip to main content
Menu

Menu
Search View topic
Enter your search term
Clear
Search results
Please enter a search term in the box above.
ClearSkip to main content
Menu

Menu
Search View topic
Enter your search term
Clear
Search results
Please enter a search term in the box above.
Skip to main content
Menu

Menu
Search View topic
Skip to main content
Menu

Menu
Search View topic
Enter your search term
Clear
Search results
Please enter a search term in the box above.
Enter your search term
Clear
Search results
Please enter a search term in the box above.
Search results
Please enter a search term in the box above.
0
Skip to main content
Menu

Menu
Search View topic
Enter your search term
Clear
Search results
Please enter a search term in the box above.
0
Hey guys Everyone explained the code great but uhh, what is the x++ doing in the entire loop? it's basically z=0 then it's put through the (for loop) where the first var x=0 is saying, the loop has.. looped 0 timezone and then the x <4; is saying you want the loop to repeat 4 times. and the z+=2 is saying you want (var z) to get an addition 2 every time your code loops. so 4*2=8. But.. what's the x++ for?? and also why is it z+=2 and not z=+2?