The document discusses C++ increment and decrement operators. It explains that these unary operators work only on integers, and can be used in both prefix (++a) and postfix (a++) forms. In prefix form, the increment/decrement occurs before the expression is evaluated. In postfix form, it occurs after. The key difference is that prefix returns the new value, while postfix returns the original value. Several examples are provided to illustrate this behavior. Finally, some practice questions are included to help test understanding of these fundamental operators.
2. Objectives
At the end of this lesson students should be able to:
Identify the
increment/
decrement
operators
1
Understand the
working of the
operators
2
Distinguish between
the prefix and
postfix forms
3
Evaluate different
expressions
containing these
operators
4
4. Increment(++) / Decrement(--) Operators
The increment(++) and decrement(--) are unary operators that
work only on integer variables.
The syntax is : Examples:
The increment / decrement operators do not work on constants.
Thus :
variable ++;
or
++ variable;
variable --;
or
-- variable;
a++;
++ x;
p=--q;
t--;
6++; //gives error as 6 is a constant
x=--9; // gives error as 9 is a constant
5. Increment(++) / Decrement(--) Operators
The increment operator increases the value of a variable by 1
The decrement operator decreases the value of a variable by 1.
Thus and
a++;
is the same as
a=a+1;
b--;
is the same as
b=b-1;
6. Both the increment and decrement operators can be prefixed or postfixed.
i.e.
In the above statements both prefix and postfix forms do not make any
difference in the output as these are stand alone statements.
But when the pre/post increment or decrement operators are part of an
expression the prefix and postfix notation does matter
Increment(++) / Decrement(--) Operators
++a; (prefix)
is the same as
a++; (postfix)
7. Increment(++) / Decrement(--) Operators
Pre Increment
int a,b;
a=3;
b=++a;
cout<<“a= ”<<a<<“tb= ”<<b;
In the prefix form the increment or decrement operation is carried
out before the rest of the expression.
Consider the following code snippet:
a b
a= 4 b= 4
1
2
output
Working
code
Explanation
8. Increment(++) / Decrement(--) Operators
Post Increment
In the postfix form the increment or decrement operation is carried
out after the rest of the expression.
Consider the following code snippet:
a b
a= 4 b= 3
1
2
output
Working
Explanation
code
int a, b ;
a=3;
b=a++;
cout<<“a= “<<a<<“tb= “<<b;
9. Thumb rule
• Prefix: change and use
CHANGE the Value of the variable
USE the new value
• Postfix : use and change
• USE the original value of the variable
• CHANGE the Value of the variable
1
1
2
2
10. Program to illustrate Increment, Decrement
operators
#include<iostream.h>
void main()
{
int a,b,c,d;
a = 0;
b = 2;
c = 4;
d = a -- + b++ + ++c;
cout<<“A= ”<<a<<“tB= ” <<b<<“tC= ”<<c“tD = "<<d;
}
A= -1 B= 3 C= 5 D= 7
0 2 4
a b c
d
d= a -- + b++ + ++c;
17. Question 4
Consider the following code and answer the question that follows :
#include <iostream.h>
void main()
{
int x = 4, y = 7, z;
x = ++x; y = --y;
z = x++ + y--;
cout << z;
}