Whats the diference between x++ and ++x | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Whats the diference between x++ and ++x

14th Nov 2016, 4:39 PM
Manai Chaouki
Manai Chaouki - avatar
9 Answers
+ 13
Even if it's answered a few times i will explain it to you: The difference lies in the execution order. When you type x++ it means that the variable is being incremented AFTER it is used. int x=0; System.out.println(x++);//the output will be still 0, because it is used first, then incremented Now again System.out.println(x); // this will be now 1 _________________ int x=0; System.out.println(++x);//the output will now be 1 because it is incremented first, then used Now again System.out.println(x); // this will be now 1
14th Nov 2016, 5:11 PM
Arsal Ali
Arsal Ali - avatar
+ 5
x++ returns value of x and then increments x y=x++ means y=x and x=x+1 ++x increments x and then returns value of x y=++x means x=x+1 and y=x
16th Nov 2016, 3:46 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 4
++a is prefix which increases the variables value and the uses the new value in expression. a++ is posfix which takes the variable value first in the expression and then increase the variable. E.g. prefix: int x = 4; int y = ++x; // y = 5 prosfix: int x = 4; int y = x++;// y = 4
14th Nov 2016, 10:26 PM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar
+ 2
is that means ++ is the incrementing
14th Nov 2016, 5:52 PM
Manai Chaouki
Manai Chaouki - avatar
+ 2
++x means pre increment x++ means post increment example x=2; ++x; //x=3 x++; //x=3
15th Nov 2016, 4:14 PM
ANAND ALABAL
ANAND ALABAL - avatar
+ 1
Exactly the same thing you can do with - -
14th Nov 2016, 6:15 PM
Arsal Ali
Arsal Ali - avatar
+ 1
++x means pre increment i. e. you used the value of incremented x before store it and x++ is post increment u used incremented value after store it. if x=5; ex- x=x++ + ++x; 5 + 6 =11;
18th Nov 2016, 10:25 AM
Sandeep Kumar Pandey
Sandeep Kumar Pandey - avatar
0
The former is postfix and the latter is prefix .In the former,first the value of x is displayed and then it is increased by one while the case is opposite in the latter.
12th Dec 2016, 7:12 AM
Anand Chourasiya
Anand Chourasiya - avatar
0
X++ will increment the value after a usage and ++X will first increment the value and then use it , for ex , if x=5 and say y=++x; Print y Print x Then output will be : 6 6 But if y=x++ then Print y Print x Will give output as: 5 6 So, in ++x it uses modified value instantly and in x++ it uses old value of x and then increment it for further use.
25th Aug 2020, 4:17 PM
Techspert
Techspert - avatar