Page 29 - Modular_V1.1_Flipbook
P. 29
For example:
int a = 10, b = 0;
b = ++a; // This is prefix increment operator
b = a++; // This is postfix increment operator
b = --a; // This is prefix decrement operator
b = a--; // This is postfix decrement operator
The difference between prefix and postfix (++ and – –) operators is that, in case of prefix operators
the value of the variable is incremented/decremented first and then the expression is evaluated.
In case of postfix operators, the expression is evaluated first and then the value of the variable is
incremented/decremented. Consider the preceding example:
int a = 10, b = 0;
b = ++a; // The value is incremented first and then assigned
b = a++; // The value is assigned first and then incremented
b = --a; // The value is decremented first and then assigned
b = a--; // The value is assigned first and then decremented
Program 6: To use prefix increment/decrement operators.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a = 10, b = 0, c = 0;
cout<<”The old value of a is: “ << a << “\n”;
b = ++a; //Value is incremented first and then assigned
cout<<”The value of a is: “ << a << “\n”;
cout <<”The value of b is: “ << b << “\n”;
c = --a; //Value is decremented first and then assigned
cout<<”The value of c is: “ << a << “\n”;
getch();
}
Program 7: To use postfix increment/decrement operators.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a = 10, b = 0, c = 0;
cout <<”The old value of a is: “ << a << “\n”;
b = a++; //Value is assigned first and then incremented
Operators in C++ 27

