+ 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);

9th Oct 2016, 3:26 PM
Namita
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).
10th Oct 2016, 3:36 AM
Rizza Camille Cañete
Rizza Camille Cañete - avatar
+ 8
x=0 z=2 ,x=1 z=4 , x=2 z=6 , x=3 z= 8
10th Oct 2016, 12:20 AM
Jaekyoung Jeon
Jaekyoung Jeon - avatar
+ 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
9th Oct 2016, 4:11 PM
AndyPandy
AndyPandy - avatar
+ 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
4th Dec 2016, 1:14 PM
Didi Georgel Danaila
Didi Georgel Danaila - avatar
+ 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.
10th Oct 2016, 7:43 PM
Vladimir Popkov
Vladimir Popkov - avatar
+ 2
The loop runs for x=0,1,2,3 and each time adds 2 to z. 4*2=8
15th Nov 2016, 8:48 PM
payam
payam - avatar
+ 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...
18th Nov 2016, 4:24 PM
Amirali Farbooodi
Amirali Farbooodi - avatar
+ 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
5th Jan 2017, 4:30 AM
Ryan Hull
Ryan Hull - avatar
+ 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?
9th Oct 2016, 5:47 PM
Namita
+ 1
yes it stops at 3 and then it add 1 to 3
9th Oct 2016, 6:06 PM
Long Vũ Trần
Long Vũ Trần - avatar
+ 1
your loop starts at 0, so 4 loops is 0 1 2 3.
11th Oct 2016, 4:28 PM
dvanfoss
+ 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..
18th Oct 2016, 3:44 AM
Dhanya Sunil
Dhanya Sunil - avatar
+ 1
thank they
24th Oct 2016, 8:02 PM
Excellent
Excellent - avatar
+ 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
6th Nov 2016, 12:16 PM
Atul Ranjan
Atul Ranjan - avatar
+ 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
6th Nov 2016, 5:39 PM
Naresh Mitta
Naresh Mitta - avatar
+ 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.
29th Nov 2016, 1:12 AM
Syed NomanulHasan
Syed NomanulHasan - avatar
+ 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
22nd Dec 2016, 5:13 PM
G mailer
G mailer - avatar
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.
8th Jan 2017, 8:03 AM
Jimmy Rawson
Jimmy Rawson - avatar
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.
8th Jan 2017, 8:04 AM
Jimmy Rawson
Jimmy Rawson - avatar
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?
25th Jan 2017, 8:52 PM
CMPLX 16
CMPLX 16 - avatar