0
Please help me to understand about prefix, postfix of increment and decrement operators with simple example..?
3 Respuestas
+ 4
Examples:
int x = 0;
cout << ++x * 5 << endl;
cout << x;
//Output:
//5
//1
int y = 0;
cout << y++ * 5 << endl;
cout << y;
//Output:
//0
//1
+ 2
Increment operator does 2 things:
~Increments variable value by 1
~Returns variable value for other operations to use
Difference between prefix and postfix is that which one of those 2 tasks is done first.
++x:
Firstly increments x's value by 1,
Secondly it returns x's (new) value for other operations to use.
x++:
Firstly returns x's (original) value for other operations to use.
Secondly it increments x's value by 1.
Decrement operator works very similarly to increment operator, but instead of adding 1 it subtracts 1.
An advantage of increment and decrement operators is that you can increment or decrement variable value while you use the variable in calculations.
+ 1
My doubt cleared Seb TheS
Thank U