Chapter 4: Operators in C++
Operators are special symbols that perform operations on variables and values. They are used to calculate results, compare values, assign data, and perform logical or bitwise operations.
For example:
1int a = 10; 2int b = 5; 3 4cout << a + b;
Output
115
In the example above, the + operator adds two numbers.
Types of Operators in C++
C++ provides several categories of operators:
- Arithmetic Operators
- Assignment Operators
- Comparison (Relational) Operators
- Logical Operators
- Bitwise Operators
1. Arithmetic Operators
Arithmetic operators perform mathematical calculations.
| Operator | Description | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (Remainder) | a % b |
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 int a = 10; 8 int b = 3; 9 10 cout << "Addition: " << a + b << endl; 11 cout << "Subtraction: " << a - b << endl; 12 cout << "Multiplication: " << a * b << endl; 13 cout << "Division: " << a / b << endl; 14 cout << "Remainder: " << a % b << endl; 15 16 return 0; 17}
Output
1Addition: 13 2Subtraction: 7 3Multiplication: 30 4Division: 3 5Remainder: 1
2. Assignment Operators
Assignment operators assign or update values stored in variables.
| Operator | Description | Example |
|---|---|---|
= | Assign | x = 10 |
+= | Add and assign | x += 5 |
-= | Subtract and assign | x -= 5 |
*= | Multiply and assign | x *= 2 |
/= | Divide and assign | x /= 2 |
%= | Modulus and assign | x %= 2 |
Example
1int x = 10; 2 3x += 5; 4 5cout << x;
Output
115
3. Comparison (Relational) Operators
Comparison operators compare two values and return true or false.
| Operator | Description |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Example
1int a = 10; 2int b = 20; 3 4cout << (a < b);
Output
11
1representstrue, while0representsfalse.
4. Logical Operators
Logical operators combine or reverse Boolean expressions.
| Operator | Description | ||
|---|---|---|---|
&& | Logical AND | ||
| ` | ` | Logical OR | |
! | Logical NOT |
Example
1int age = 22; 2 3cout << (age >= 18 && age <= 60);
Output
11
5. Bitwise Operators
Bitwise operators work directly with the binary representation of integers.
| Operator | Description | |
|---|---|---|
& | Bitwise AND | |
| ` | ` | Bitwise OR |
^ | Bitwise XOR | |
~ | Bitwise NOT | |
<< | Left Shift | |
>> | Right Shift |
Example
1int a = 5; 2int b = 3; 3 4cout << (a & b);
Output
11
Operator Precedence
When multiple operators appear in an expression, C++ follows operator precedence.
Example:
1int result = 5 + 3 * 2;
Output
111
Multiplication is performed before addition.
Use parentheses to control evaluation order.
1int result = (5 + 3) * 2;
Output
116
Best Practices
- Use parentheses to improve readability.
- Choose meaningful variable names.
- Avoid overly complex expressions.
- Be careful with integer division.
- Use logical operators for conditions.
- Use bitwise operators only when required.
Common Mistakes
Integer Division
1cout << 5 / 2;
Output
12
To get a decimal result:
1cout << 5.0 / 2;
Output
12.5
Using = Instead of ==
Incorrect:
1if (a = 10)
Correct:
1if (a == 10)
Practice Exercises
- Write a program to perform all arithmetic operations on two numbers.
- Compare two numbers using relational operators.
- Check whether a person is eligible to vote using logical operators.
- Demonstrate the use of assignment operators.
- Perform left and right bitwise shift operations on an integer.
Summary
In this chapter, you learned:
- What operators are in C++.
- How arithmetic operators perform mathematical calculations.
- How assignment operators update variable values.
- How comparison operators compare values.
- How logical operators evaluate conditions.
- How bitwise operators manipulate binary data.
- The importance of operator precedence and parentheses.
In the next module, you'll learn Decision Making in C++, including if, if...else, nested if, switch, and the conditional (ternary) operator to make your programs respond to different conditions.