Please explain what is the difference between ++x and x++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 72

Please explain what is the difference between ++x and x++?

Please explain what is the difference between ++x and x++?

26th Oct 2016, 12:27 PM
Remmae
Remmae - avatar
105 Answers
+ 277
There is no difference when used alone, the difference is when you use them in an expression. a++ evaluates a, then increments it (post-incrementation). ++a increments a, then evaluates it (pre-incrementation). Example: int a = 1; int b = a++; //b = 1, a = 2 System.out.println(b); //prints 1 a = 1; b = ++a; //b = 2, a = 2 System.out.println(b); //prints 2
26th Oct 2016, 12:31 PM
Lara
Lara - avatar
+ 38
If int x=5; and and y=5; System.out.println(x++); //5 System.out.println(x); //6 System.out.println(++y); //6 hope it helps
26th Oct 2016, 12:32 PM
FreakManMega
+ 13
I'm probably going to say the same thing Lara said.. ++x just increments x before working with it and, x++ increments x after working with it.. there isnt much difference until used in a function..
28th Oct 2016, 8:39 AM
Adam Salehe
Adam Salehe - avatar
+ 10
++x is prefix. x++ is postfix. In the case of y =++x , y will become (x+1), x will become (x+1). Which means that prefix adds the value before processing the data/command. However, in the case of y = x++ , y will become x and x will become (x+1). Which also means that postfix process the data/command first before it adds the value
25th Nov 2016, 7:14 PM
Wen Qin
Wen Qin - avatar
+ 3
Now x++ means Increment x after this line and ++x means Increment x before this line. With i++, it's called post increment, and the value is used in whatever context then incremented; ++i is pre-increment increments the value first and then uses it in context for example: #include <iostream> using namespace std; main() { int a = 21; int c ; // Value of a will not be increased before assignment. c = a++; cout << "Line 1 - Value of a++ is :" << c << endl ; // After expression value of a is increased cout << "Line 2 - Value of a is :" << a << endl ; // Value of a will be increased before assignment. c = ++a; cout << "Line 3 - Value of ++a is :" << c << endl ; return 0; } answer for this is: Line 1 - Value of a++ is :21 Line 2 - Value of a is :22 Line 3 - Value of ++a is :23 There isn't much difference until used in a function. I hope it helps!!! Thank You
30th Nov 2016, 12:14 PM
Jaydeep Khatri
Jaydeep Khatri - avatar
+ 2
++ is a increment operator. there is two types of increment operators postfix (var++) & prefix (++var). as the name suggests PREFIX OPERATORS EVALUATE BEFORE. & POSTFIX OPERATORS EVALUATE AFTER. let see some basic example like int a=10; int b=a++ /*old value of a will be assigned to b , the value of a increased by 1 means a=11 & b=10 after this statement.*/ int a=10; int b=++a; /*new value of a will be assigned to b after increasing value of a by 1 means a=11 & b=10*/ NOTE: now here one more thing you should know about prefix & post fix is IN JAVA POSTFIX HAVE HIGHEST PRECEDENCE. yes I have not make any mistake in typing, postfix will evaluate before any other operators. let see one interesting example... int a=10; a=a++; think what should be the value of a... let see the code carefully. first of all there is assignment operator (=) is there. so it will evaluate first as any equation in Java evaluated lest to right. but in the case of '=' the right side of operator evaluated first so a++ IS EVALUATED FIRST. now at the time of evaluation the VALUE OF A WILL BE INCREASED & STORE IN MEMORY OF VARIABLE A. BUT THE OLD VALUE OF A WILL BE USED TO EVALUATE EXPRESSION. so when assignment operator executed the value of a actually incremented in memory but the old value is in buffer or temporary memory which is used to evaluate expression so final 10 is assigned to a. do not make mistake in understanding of postfix & prefix. in old gcc compiler for c language the evaluation of the postfix done differently. for example you can get different value of c in different c compiler in following example... int a=10; int b=13; int c; c= a++ + ++b + --a + --b + a; in old gcc compiler you may get c = 53 & in new gcc compiler you will get c = 57. so do not make mistake. enjoy programming.
25th Nov 2016, 11:29 AM
Neel Patel
Neel Patel - avatar
+ 1
Hello, PHP explains so: $a=2; $b=$a++ // $a=3, $b=2 $a=2; $b=++$a // $a=3, $b=3
1st Dec 2016, 4:19 AM
Anibal Roman
Anibal Roman - avatar
+ 1
for example y=4 z=++y so both y and z = 5 but y=4 z=y++ y=5 and z=4
30th Jul 2017, 4:30 AM
sohail khan
sohail khan - avatar
+ 1
please solve my problem int x =5; int y = x++; //out put is 5. why? please give me answer with explain
27th Jul 2019, 3:31 PM
Vikram Singh
Vikram Singh - avatar
+ 1
Since x++ is post increment so it first assign value of x to y the. Increment value of x by 1.
27th Mar 2021, 11:19 AM
Arpit Dubey
Arpit Dubey - avatar
0
for example y=4 z=++y so both y and z = 5 but y=4 z=y++ y=5 and z=4
2nd Nov 2016, 9:14 PM
Mark Bassem
Mark Bassem - avatar
0
prefix inc {cout<<++a;} equal {a=a+1;cout<<a;} postfix inc {cout<<a++;} equal {cout<<a;a=a+1;}
25th Nov 2016, 8:40 PM
SUPER_S
SUPER_S - avatar
0
a++ use the value of 'a' first and then increment it. But,++a increment the value by 1 and use the value of 'a'.
26th Nov 2016, 10:36 AM
Sreerag V S
Sreerag V S - avatar
0
processing
30th Nov 2016, 5:07 PM
Abdikasim Ali Mohamed
Abdikasim Ali Mohamed - avatar
0
++ is a increment operator. there is two types of increment operators postfix (var++) & prefix (++var). as the name suggests PREFIX OPERATORS EVALUATE BEFORE. & POSTFIX OPERATORS EVALUATE AFTER.
30th Nov 2016, 5:48 PM
Abdikasim Ali Mohamed
Abdikasim Ali Mohamed - avatar
0
++x is prefix so during execution the value of x increased by one during execution of line whereas the x++ is a post fix so it results after the execution of the line.
8th Jul 2017, 1:51 AM
Arpit Dubey
Arpit Dubey - avatar
0
this is a year old why are yall still commenting lmao, theres already 100 other answers
30th Jul 2017, 8:24 AM
Blood
Blood - avatar
0
x++ is post-increament and ++x is pre-increament let see int a=0; System.out.println(++a);//output will be 1 but a=0; System.out.println(a++);//output will be 0 in this case but value of a is equal to 1 now.
20th Mar 2018, 11:36 AM
MUHAMMAD WAQAS KHAN
MUHAMMAD WAQAS KHAN - avatar
- 1
x=1; y = x++; post increment ----------------------------------------------------- first: y = x = 1 second x++ : x = 2 x=1; y = ++x; pre increment; ----------------------------------------------------- first ++x: x = 2 second: y = 2;
1st Nov 2016, 8:24 PM
Dusan Neagic
Dusan Neagic - avatar
- 1
you will know the diffrent when seeing this example: for example we have a=1 and i=3 a=i++ that mean a=3 and i=4 but for a=++i we have a=4 and i=4
2nd Nov 2016, 5:57 PM
Marouane Mehdi
Marouane Mehdi - avatar