DO NOT MISS

Ads

Tuesday, 21 June 2016

Increment & Decrement operators


'C' has two very useful operators not generally found in other languages. They are increment and decrement operators ++ and --.
The operator ‘++’ adds 1 to the variable, while ‘--‘subtracts 1. Both the operators are unary operators & the fallowing form.

Increment & Decrement operators
a++ (post increment)
a-- (post decrement)
++a (pre increment)
--a (pre decrement)


++a or a++
++a --> a=a+1
--a or a--
--a --> a=a-1

While ++a & a-- mean something when form statements independently.

Example:-
Let a=5, b=a++
(post increment)
Let a = 5, b = ++a
(pre increment)
b=5, a=a+1 --> 5+1=6
b=++a --> 5+1=6
b=5, a=6
a=6, b=6


They behave differently when they are used in expressions from the right hand side of assignment statements. 
In the 1st case, a post fix operators 1st assigns the value to the variable on left & the increments the operator. In the 2nd case a prefix operator 1st adds 1 to the operator & then the result is assigned to the variable on left.


Increments operators
a=10, b=a++
b=10
a=11
a=10, b=++a
b=11
a=11
a=10, b=a++ + a++
b=20
a=12
a=10, b=a++ + ++a
b=22
a=12
a=10, b=++a + a++
b=22
a=12
a=10, b=++a + ++a
b=24
a=12
a=10, b=a++ + a++ + a++
b=33
a=13
a=10, b=a++ + a++ + ++a
b=34
a=13
a=10, b=a++ + ++a + ++a
b=35
a=13
a=10, b=++a + a++ + a++
b=34
a=13
a=10, b=++a + a++ + ++a
b=35
a=13
a=10, b=++a + ++a + a++
b=35
a=13
a=10, b=++a + ++a + ++a
b=36
a=13

Decrements operators
a=10, b=a--
b=10
a=9
a=10, b=--a
b=9
a=9
a=10, b=a-- + a--
b=19
a=8
a=10, b=a-- + --a
b=18
a=8
a=10, b=--a + a--
b=18
a=8
a=10, b=--a + --a
b=17
a=8
a=10, b=a-- + a-- + a--
b=27
a=7
a=10, b=a-- + a-- + --a
b=27
a=7
a=10, b=a-- + --a + a--
b=27
a=8
a=10, b=a-- + --a + --a
b=25
a=7
a=10, b=--a + a-- + a--
b=27
a=7
a=10, b=--a + a-- + --a
b=24
a=7
a=10, b=--a + --a + a--
b=25
a=7
a=10, b=--a + --a + --a
b=24
a=7
a=10, b=a++ - a++ - a++
b=-10
a=-13
a=10, b=a++ - ++a a++
b=-11
a=-13
a=10, b=a++ - --a + a++
b=10
a=11

Greataims

 
Copyright © 2016 Computersadda. Designed by @ Computersadda Team - Published By Greataims