Increment and Decrement operators in C++

NeeruMittal4 2,882 views 19 slides Jul 16, 2018
Slide 1
Slide 1 of 19
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19

About This Presentation

increment and decrement operators in C++ explained with the help of animation


Slide Content

C++ Increment / Decrement Operators Computer Science Class XI CBSE

Objectives At the end of this lesson students should be able to:

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 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