What is the real difference between X++ and ++X? Similarly for X-- and --X. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

What is the real difference between X++ and ++X? Similarly for X-- and --X.

When exactly does the value of X increment or decrement according to the situation?

20th Apr 2017, 12:50 PM
Jérémie Agossouvi
Jérémie Agossouvi - avatar
8 Answers
+ 5
thanks for answers.
20th Apr 2017, 5:29 PM
Jérémie Agossouvi
Jérémie Agossouvi - avatar
+ 4
I see people have already answered your questions but I would like to give advice as how to remember this because I sometimes forget what does what. Example 1 a = 1; cout << a++; // Outputs 1 cout << a; // Outputs 2 Example 2 a = 1; cout << ++a; // Outputs 2 cout << a; // Outputs 2 Defining variables a = say the value of a ++ = increment a I tend to look at statements as if they were a sentence. If I said say a++. The first thing you would say is "say the value of a" then "increment a". If I said say ++a you would say "increment a" then "say the value of a".
20th Apr 2017, 8:48 PM
Ty Ler
Ty Ler - avatar
+ 3
++x or --x is prefix notation x++ or x-- is postfix notation In simple words ++x means increment value then use Eg: int x=5; cout<<++x; //here value of x is first incremented by 1 then the new value that is 6 is printed x++ means use value then increment Eg: int x=5; cout<<x++; // here x is used first (that is printed first as 5) then it is incremented to 6 If we try to print x again it would print 6.
20th Apr 2017, 1:09 PM
Jilson Joseph
20th Apr 2017, 1:12 PM
Benneth Yankey
Benneth Yankey - avatar
+ 3
https://www.sololearn.com/discuss/489143/?ref=app
25th Jun 2017, 10:10 AM
Siddharth Saraf
+ 2
++ or -- beforce variable(like ++x): baisicly value is increased first, and then everything else is done after that. x++ or x-- is opposite of that
20th Apr 2017, 1:01 PM
Luka Kldiashvili
Luka Kldiashvili - avatar
+ 2
thank you for your answers
26th Apr 2017, 10:06 AM
Praveena Aruchunan
Praveena Aruchunan - avatar
0
hallo i am newbie
29th Apr 2017, 5:15 PM
otomotif TV
otomotif TV - avatar