Programming Fundamentals 05

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

COMPUTER

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 pre-increment operator is used to increment the value of a variable before using it in an


expression. In the Pre-Increment, value is first incremented and then used inside the
expression.
• Syntax:
a = ++x;
• Here, if the value of ‘x’ is 10 then the value of ‘a’ will be 11 because the value of ‘x’ gets
modified before using it in the expression.
EXAMPLE
POSTFIX-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++

• Decrement: It is used to decrement the value of the variable by 1. --


• The decrement can be done in two ways:
• Pre-Decrement Operator
• Post-Decrement Operator
PREFIX-DECREMENT OPERATOR

• 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

You might also like