CHAPTER 05
Beginner
Operators in C++
Updated: May 17, 2026
5 min read
# CHAPTER 5
Operators in C++
1. Introduction
Operators are the engines of a program. They take variables and values, perform operations on them, and produce results. Just as a calculator uses+ and -, C++ provides a rich set of operators for mathematics, logic, and memory manipulation.
2. Learning Objectives
By the end of this chapter, you will be able to:- Perform calculations using Arithmetic Operators.
- Compare values using Relational Operators.
- Combine conditions using Logical Operators.
- Use Assignment and Increment/Decrement operators.
- Understand basic Bitwise operators.
3. Arithmetic Operators
Used for mathematical calculations.| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 5 | 2 |
% | Modulus (Remainder) | 10 % 3 | 1 |
Important Note on Division: If you divide two integers, C++ truncates the decimal. 5 / 2 results in 2. To get 2.5, at least one number must be a float: 5.0 / 2.
4. Increment and Decrement Operators
Used to add or subtract 1 from a variable. Highly used in loops.| Operator | Description | Example |
|---|---|---|
++ | Increases value by 1 | x++ or ++x |
-- | Decreases value by 1 | x-- or --x |
Prefix vs. Postfix:
cpp
5. Relational (Comparison) Operators
Used to compare two values. They returntrue (1) or false (0).
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 3 | false |
!= | Not equal | 5 != 3 | true |
> | Greater than | 5 > 3 | true |
< | Less than | 5 < 3 | false |
>= | Greater or equal | 5 >= 5 | true |
<= | Less or equal | 5 <= 3 | false |
6. Logical Operators
Used to combine multiple relational conditions.| Operator | Name | Description | Example |
|---|---|---|---|
&& | Logical AND | True only if BOTH are true | (5 > 3) && (2 < 4) is true |
|| | Logical OR | True if AT LEAST ONE is true | (5 < 3) || (2 < 4) is true |
! | Logical NOT | Reverses the result | !(5 == 5) is false |
7. Assignment Operators
Used to assign values to variables.| Operator | Example | Equivalent to |
|---|---|---|
= | x = 5 | Assigns 5 to x |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
8. The Ternary Operator (?:)
A shorthand for a simple if-else statement.
Syntax: condition ? valueiftrue : valueiffalse;
cpp
9. Operator Precedence
Just like PEMDAS in math, C++ evaluates operators in a specific order.-
1.
()Parentheses
-
2.
++,--,!(Unary)
-
3.
*,/,%(Multiplication/Division/Modulus)
-
4.
+,-(Addition/Subtraction)
-
5.
<,>,<=,>=(Relational)
-
6.
==,!=(Equality)
-
7.
&&(Logical AND)
-
8.
||(Logical OR)
-
9.
=,+=,-=(Assignment)
Best Practice: When in doubt, use parentheses () to force the order you want.
10. Memory-Level Explanation
When you runc = a + b:
-
1.
The CPU fetches the value of
afrom RAM into a CPU register.
-
2.
The CPU fetches the value of
binto another register.
- 3. The Arithmetic Logic Unit (ALU) inside the CPU adds them.
-
4.
The result is pushed back into the RAM address of
c.
11. Common Mistakes
-
Confusing
=and==:x = 5assigns 5 to x.x == 5checks if x is 5. Using=in anifstatement is a massive source of logical bugs.
-
Modulus on floats: The
%operator ONLY works with integers.
-
Integer Division: Forgetting that
5 / 2yields2, not2.5.
12. Exercises
- 1. Write a program that calculates the area and perimeter of a rectangle.
-
2.
Demonstrate the difference between
x++and++xusingcout.
-
3.
Create a program that determines if a number is even or odd using the modulus operator (
%).
13. MCQ Quiz with Answers
Question 1
What is the result of 10 % 3?
Question 2
Which operator is used to compare equality?
Question 3
What does x += 5 mean?
Question 4
If a = true and b = false, what is a && b?
Question 5
Which operator has the highest precedence?
% be used with float variables in C++?
a) Yes b) No
Answer: b) No
Question 7
What is the output: int x = 5; cout << x++;?
Question 8
What is the output of !true?
Question 9
Which operator is Logical OR?
Question 10
How does C++ handle double x = 5 / 2;?
14. Interview Questions
- Q: Explain short-circuit evaluation in logical operators.
- Q: Without using a temporary variable, how can you swap two numbers? (Hint: arithmetic or XOR).
- Q: Why does C++ truncate integer division, and how do you prevent it?
15. Summary
Operators allow you to perform calculations and logic. Arithmetic operators handle math, relational operators compare values, and logical operators combine conditions. Operator precedence determines the order of evaluation, but parentheses should be used for clarity.16. Next Chapter Recommendation
In Chapter 6: User Input and Output, we will make our programs interactive by learning how to ask the user for data usingcin and getline().