Chapter 3 - Operators in C++

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

Operators in C++

In programming, an operator is a symbol that operates on


a value or a variable.

Operators are symbols that perform operations on


variables and values. For example, + is an operator used
for addition, while - is an operator used for subtraction.

2
Operators in C++ can be classified into 6 types:

1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
6. Other Operators

3
1. C++ Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and data. For
example,

Here, the + operator is used to add two variables a and b. Similarly there are various other
arithmetic operators in C++.

4
Output:

a+b=9
a–b=5
a + b =14
Here, the operators
+, - and * compute
a/b=3
addition,subtraction, a%b=1
and multiplication
respectively as we
might have
expected.
5
/ Division Operator
Note the operation (a / b) in our program. The / operator is the division operator.

As we can see from the above example, if an integer is divided by another


integer, we will get the quotient. However, if either divisor or dividend is a
floating-point number, we will get the result in decimals.

% Modulo Operator

The modulo operator % computes the remainder. When a = 9 is divided by b = 4, the


remainder is 1.
Note: The % operator can only be used with integers.

6
Increment and Decrement Operators
C++ also provides increment and decrement operators: ++ and -- respectively.

++ increases the value of the operand by 1


-- decreases it by 1

For example,

Here, the code ++num; increases the value of num by 1.

7
Example 2: Increment and Decrement Operators
Output

In the above program, we


have used the ++ and --
operators as prefixes (++a
and --b). However, we can
also use these operators as
postfix (a++ and b--).

8
2. C++ Assignment Operators
In C++, assignment operators are used to assign values to variables. For example,

9
Example 3: Assignment Operators

Output:

10
3. C++ Relational Operators
A relational operator is used to check the relationship between two operands. For example,

11
Example 4: Relational Operators

Output

Note: Relational operators are used


in decision-making and loops.

12
4. C++ Logical Operators
Logical operators are used to check whether an expression is true or false. If the expression
is true, it returns 1 whereas if the expression is false, it returns 0.

In C++, logical operators are commonly used in decision making. To further


understand the logical operators, let's see the following examples,

13
14
Example 5: Logical Operators

Output

15
Explanation of logical operator program

• (3 != 5) && (3 < 5) evaluates to 1 because both operands (3 != 5) and (3


< 5) are 1 (true).
• (3 == 5) && (3 < 5) evaluates to 0 because the operand (3 == 5) is 0
(false).
• (3 == 5) && (3 > 5) evaluates to 0 because both operands (3 == 5) and (3
> 5) are 0 (false).
• (3 != 5) || (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 <
5) are 1 (true).
• (3 != 5) || (3 > 5) evaluates to 1 because the operand (3 != 5) is 1 (true).
• (3 == 5) || (3 > 5) evaluates to 0 because both operands (3 == 5) and (3 >
5) are 0 (false).
• !(5 == 2) evaluates to 1 because the operand (5 == 2) is 0 (false).
• !(5 == 5) evaluates to 0 because the operand (5 == 5) is 1 (true).
16
5. C++ Bitwise Operators
In C++, bitwise operators are used to perform operations on individual bits. They can only
be used alongside char and int data types.

17
6. Other C++ Operators
Here's a list of some other common operators available in C++.

18
Source:
https://www.programiz.com/cpp-programming/operators

19
20

You might also like