+ 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?
8 Antworten
+ 5
thanks for answers.
+ 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".
+ 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.
+ 3
https://www.sololearn.com/discuss/489143/?ref=app
+ 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
+ 2
thank you for your answers
0
hallo i am newbie