+ 2

Can someone help me understand the ++x, x++, - - x, and x-- thing I still have some problems with it \\fell free to upvote,comnt

23rd Jun 2017, 5:29 PM
jones Edward
jones Edward - avatar
8 Respostas
+ 2
++x increase x and return increased value. x++ increase and return old value. -- do the same, but with desrease
23rd Jun 2017, 5:38 PM
alex
alex - avatar
+ 1
example1 int x = 0; x++; (now x = 1) example2 int x = 0; int y = x++; (now x = 1; but y=0;) why y = 0? because with x++ it first evaluates the expression then it increases the value example3 int x = 0; int y = ++x; (now x = 1; y=1;) why y = 1? because with ++x it first increases the value then it evaluates the expression; Similarly when --x or x--. Try it for yourself: example4: int x = 100; int y = 1+x--; y - ? example5: int x = 1; int y = 500*--x; y - ?
23rd Jun 2017, 8:12 PM
Ренат Мухаметшин
Ренат Мухаметшин - avatar
+ 1
Postfix notation(e.g. a++, a--) is like a postpaid card increment or decrement will happens after performing the operations like assign or print. Prefix notation(e.g. ++a, --a) is like a postpaid card increment or decrement will happens first then performing the operations like assign or print. Ex. Program with output: #include <iostream> using namespace std; int main() { int a=10; cout<<"\nA="<<a--; cout<<"\nA="<<--a; cout<<"\nA="<<a++; cout<<"\nA="<<++a; return 0; } A=10 A=8 A=8 A=10 Note: First output prints the value(A=10) and decremented the value of A by 1. Value of A will be 9 in the memory. Second output decrements the value of A by 1 and prints the value(A=8). Value of A will be 8 in the memory. Third output prints the value(A=8) and increment the value of A by 1. Value of A will be 9 in the memory. Fourth output increments the value of A by 1 and prints the value(A=10). Value of A will be 10 in the memory.
6th Jul 2017, 10:45 AM
balachandar karnan
balachandar karnan - avatar
0
This means when I use x++ the value of x stays the same?
23rd Jun 2017, 5:40 PM
jones Edward
jones Edward - avatar
0
no, the value doesn't stay the same. it is altered in memory but in an expression, the value would be the initial x, the increasing taking place after evaluation. with ++x it first increases the value then it evaluates the expression
23rd Jun 2017, 5:55 PM
Vraciu Stefan
Vraciu Stefan - avatar
0
--x and ++x are called prefixes while x-- and x++ are postfixes. Prefixes increment or decrement the variable before it's used, postfixes change the value after it was used.
23rd Jun 2017, 8:19 PM
Jonas Schröter
Jonas Schröter - avatar
0
So, basically ++ or -- before a variable (++x or --x) is prefix. So, it performs the function before the variable is used. ++ or -- after a variable(x++ or x--) is postfix. So, it performs the function after the next use of variable in the statements.
26th Jun 2017, 5:55 PM
Vidhi Tiwari
Vidhi Tiwari - avatar
0
After execution of x++ the value x will increase after I.e x=1,x++ output will be 2 After execution of ++x the value x will increase before i.e x=1,++x output will be 2 but before execution of x
14th Jul 2017, 3:35 AM
Bansi Patel