Programming Fundamentals 05
Programming Fundamentals 05
PROGRAMMING
LECTURE #05
UNARY OPERATORS/ PRE-POST INCREMENT
ARITHMETIC OPERATOR
INCREMENT OPERATOR IN C++
• Increment operators are used to increase the value of the variable by one.
• ++
• It is used to increment the value of the variable by 1. The increment can be done in two
ways:
• Pre-Increment Operator
• Post-Increment Operator
PREFIX-INCREMENT OPERATOR
• A post-increment operator is used to increment the value of the variable after executing
the expression completely in which post-increment is used. In the Post-Increment, value
is first used in an expression and then incremented.
• Syntax:
a = x++;
• Here, suppose the value of ‘x’ is 10 then the value of variable ‘a’ will be 10 because the
old value of ‘x’ is used.
EXAMPLE
WRITE OUTPUT OF THE FOLLOWING CODE:
DECREMENT OPERATOR IN C++
• prefix decrement: In this method, the operator precedes the operand (e.g., – -a). The value
of the operand will be altered before it is used.
int a = 1;
int b = --a; // b = 0
EXAMPLE
POSTFIX-DECREMENT OPERATOR
• postfix : In this method, the operator follows the operand (e.g., a- -). The value of the
operand will be altered after it is used.
int a = 1;
int b = a--; // b = 1
int c = a; // c = 0
EXAMPLE
WRITE OUTPUT OF THE FOLLOWING CODE:
SUMMARY