https://ide.codingblocks.com/s/41907
it prints 7 but https://ide.codingblocks.com/s/41908 this prints 6 why
Unable to understand pre and post increment
Pre : value first changed (increased or dec ) then used
Post : value first used then changed (inc or dec)
int main() {
int i=6;
i++;
cout<<i;
}
value first changed ( i.e 7 ) then printed so 7
here :
#include
using namespace std;
int main() {
int i=6;
int x=i++;
cout<<x;
}
value first used (i.e x gets 6 then changed (i.e i=7 ) hence x is 6
I hope this clears your doubt!
Hit like if you it it
Cheers!
but why in int main(){
int i=6;
i++;
cout<<i;
but here first i should be print then it should be changed means 6 should be printed then i should become 7
you are getting confused with using and changing.
using means in the current place where āiā variable is mentioned it is older value is used and after that at memory location increment is performed.
in
int main(){
int i=6;
i++; stat
cout<<i;
i++ // after this value in memory referenced by becomes 7 so in next the statement value is printed as 7.
while in other code
x=i++;
older value of ā i ā is used then the value is incremented. (is you do a change cout<<x to cout<<x<<" "<<i then it will print 6 7.
Hit like if you it it
Cheers!