x++ will increase a value sometimes. Please help me learn all of the times when it won't change x. This is my understanding so far: x=4; for { y=x++; } document.write(y) //returns 4 x=4; for { y=x++; y+=2; return y; } //returns 7 x++/2==? ++x/2=2.5 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

x++ will increase a value sometimes. Please help me learn all of the times when it won't change x. This is my understanding so far: x=4; for { y=x++; } document.write(y) //returns 4 x=4; for { y=x++; y+=2; return y; } //returns 7 x++/2==? ++x/2=2.5

I was told X++ only increments if it is followed by code in a block. But what about in a math problem? or any other instance?

9th Oct 2016, 5:31 PM
ixnayOntheAmScray
6 Answers
+ 3
In second code block a value of y will be 6. And in third code block (if x = 4): x++/2 // 2 is result ++x/2 // 2,5 is result Because: x++ will increase a value of x every time, but if you use it in expression it will return a value at first, and then increase a value. There are a two types of increment: postfix (i++) and prefix (++i). Postfix return a value at first, and then increase a value. Prefix increase a value at first, and then return a value. Here is an examples: //#1 var x = 1; var y = x++ + 3; // y=4, because postfix increment return a value in expression at first (x = 1). And then x will be increased (x = 2) //#2 var x = 1; var y = ++x + 3; // y=5, because prefix increment increase a value at first (x = 2), and then x returns this value in expression ( x = 2) //#3 var x = 1; var y = x++; // y = 1; x = 2; y = ++x; // y = 3, x = 3
10th Oct 2016, 7:07 PM
Vladimir Popkov
Vladimir Popkov - avatar
0
postfix x++ first use the value and then increase the value of x. prefix ++x first increase the Value and then use value of x. for example: x=0; y=++x; output: x=1,y=1 And if x=0; y=x++; output: x=1,y=0
1st Feb 2017, 2:34 PM
mansi trivedi
mansi trivedi - avatar
- 1
++name adds first then tag it to the place mentioned, but name++ first tags it then adds it by one so the value is changed afterward...
18th Nov 2016, 4:28 PM
Amirali Farbooodi
Amirali Farbooodi - avatar
- 2
Simply put x++ will use the value of x, then increment x ++x will increment x, then use the new value of x examples x=0 y=x++ y now has value of 0 x now has value of 1 x=0 y=++x y now has value of 1 x now has value of 1
11th Jan 2017, 5:39 AM
Simon
- 2
how do I promote apps like in funadict com
30th Jan 2017, 11:35 PM
Nirmal Chamoli
Nirmal Chamoli - avatar
- 2
when you use b=1 a=b++ // a=1 and b=2 a=b // a=2 and b=2 a=++b // a=3 and b=3 c=5 d=c++ // d=5 c=6
31st Jan 2017, 12:29 AM
quomoow
quomoow - avatar