+ 1
I often confused with preincrement and postincrement .i need a clear answer
2 ответов
+ 6
pre increment :
increments the value first and then proceeds with the code.
post increment:
proceeds with the code by holding the same value and then increments the value
eg. int x=1,y=1,z=1;
int a=++x + ++y; // 2+2=4
int b= a++ + z; // 4+1=5
cout<<a; // 5 but in previous line execution a=4
+ 1
Its's simple to understand if you add dry run part to understand it.
The post-incremental operator (i++) uses the value as it is and after that particular execution or use of the variable the value is officially increased.
The pre-incremental operator (++i) changes the value as it is executed or used.
if:
x=0;
x++;//same value being used and changed after use i.e. 0 and now changed to 1
x;//value was changed earlier to 1
++x;//change to value to 2 now
Basically, x++ is Use then Change and ++x is Change then Use.
I hope it helps if you need anymore help like though more example please post any code and I will tell you the output and how it works.