Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
C++ Increment /
Decrement Operators
Computer Science
Class XI CBSE
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
How do they look like?
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
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;
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)
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
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;
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
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;
A video explaining the increment and
decrement operators
TEST YOURSELF
Question 1
Question 2
Question 3
int x, y;
x=0;
y=x++;
y--; --x;
Consider the following code snippet and answer the question that follows :
Question 3
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;
}
Question 4
Question 5

More Related Content

Increment and Decrement operators in C++

  • 1. C++ Increment / Decrement Operators Computer Science Class XI CBSE
  • 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
  • 3. How do they look like?
  • 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;
  • 11. A video explaining the increment and decrement operators
  • 15. Question 3 int x, y; x=0; y=x++; y--; --x; Consider the following code snippet and answer the question that follows :
  • 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; }