Skip to main content
C++ Fundamentals for Beginners to Advanced
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.
OperatorNameExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52
%Modulus (Remainder)10 % 31

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.
OperatorDescriptionExample
++Increases value by 1x++ or ++x
--Decreases value by 1x-- or --x

Prefix vs. Postfix:

cpp
12345
int a = 5;
int b = a++; // Postfix: assigns 'a' to 'b' THEN increments 'a'. (b=5, a=6)

int x = 5;
int y = ++x; // Prefix: increments 'x' THEN assigns to 'y'. (y=6, x=6)

5. Relational (Comparison) Operators

Used to compare two values. They return true (1) or false (0).
OperatorMeaningExampleResult
==Equal to5 == 3false
!=Not equal5 != 3true
>Greater than5 > 3true
<Less than5 < 3false
>=Greater or equal5 >= 5true
<=Less or equal5 <= 3false

6. Logical Operators

Used to combine multiple relational conditions.
OperatorNameDescriptionExample
&&Logical ANDTrue only if BOTH are true(5 > 3) && (2 < 4) is true
||Logical ORTrue if AT LEAST ONE is true(5 < 3) || (2 < 4) is true
!Logical NOTReverses the result!(5 == 5) is false

7. Assignment Operators

Used to assign values to variables.
OperatorExampleEquivalent to
=x = 5Assigns 5 to x
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3

8. The Ternary Operator (?:)

A shorthand for a simple if-else statement. Syntax: condition ? valueiftrue : valueiffalse;
cpp
123
int age = 20;
// If age >= 18, assign true, else assign false.
bool is_adult = (age >= 18) ? true : false; 

9. Operator Precedence

Just like PEMDAS in math, C++ evaluates operators in a specific order.
  1. 1. () Parentheses
  1. 2. ++, --, ! (Unary)
  1. 3. *, /, % (Multiplication/Division/Modulus)
  1. 4. +, - (Addition/Subtraction)
  1. 5. <, >, <=, >= (Relational)
  1. 6. ==, != (Equality)
  1. 7. && (Logical AND)
  1. 8. || (Logical OR)
  1. 9. =, +=, -= (Assignment)

Best Practice: When in doubt, use parentheses () to force the order you want.

10. Memory-Level Explanation

When you run c = a + b:
  1. 1. The CPU fetches the value of a from RAM into a CPU register.
  1. 2. The CPU fetches the value of b into another register.
  1. 3. The Arithmetic Logic Unit (ALU) inside the CPU adds them.
  1. 4. The result is pushed back into the RAM address of c.

11. Common Mistakes

  • Confusing = and ==: x = 5 assigns 5 to x. x == 5 checks if x is 5. Using = in an if statement is a massive source of logical bugs.
  • Modulus on floats: The % operator ONLY works with integers.
  • Integer Division: Forgetting that 5 / 2 yields 2, not 2.5.

12. Exercises

  1. 1. Write a program that calculates the area and perimeter of a rectangle.
  1. 2. Demonstrate the difference between x++ and ++x using cout.
  1. 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?

Q6. Can the modulus operator % 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 using cin and getline().

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·