Lesson 14 of 60 – Operators in C++
23%

Operators in C++

Operators are special symbols or keywords used to perform operations on values and variables. C++ provides operators for arithmetic, comparison, logical operations, assignment, bit manipulation, and more.

Note: An operator works with one or more operands. For example, in a + b, + is the operator and a and b are operands.

1. What is an Operator?

An operator is a symbol or keyword that tells C++ to perform a specific operation.

int sum = 10 + 20;

Here, + is the addition operator.

2. What is an Operand?

An operand is a value or expression on which an operator works.

int result = 10 + 20;

Here, 10 and 20 are operands, while + is the operator.

3. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

Operator Name Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus a % b

4. Addition Operator +

The + operator adds two values.

int a = 10;
int b = 20;

int result = a + b;

std::cout << result;

The result is 30.

5. Subtraction Operator -

The - operator subtracts one value from another.

int a = 30;
int b = 10;

int result = a - b;

The result is 20.

6. Multiplication Operator *

The * operator multiplies two values.

int price = 100;
int quantity = 5;

int total = price * quantity;

The value of total is 500.

7. Division Operator /

The / operator performs division.

int a = 20;
int b = 5;

int result = a / b;

The result is 4.

8. Modulus Operator %

The % operator gives the remainder of integer division.

int result = 17 % 5;

The result is 2.

9. Relational Operators

Relational operators compare two values. The result is a Boolean value.

Operator Meaning
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

10. Equal To Operator ==

The == operator checks whether two values are equal.

int a = 10;
int b = 10;

bool result = (a == b);

Here, result is true.

11. Not Equal Operator !=

The != operator checks whether two values are different.

int a = 10;
int b = 20;

bool result = (a != b);

The result is true.

12. Greater Than Operator >

The > operator checks whether the left value is greater than the right value.

int age = 25;

bool result = (age > 18);

The result is true.

13. Less Than Operator <

The < operator checks whether the left value is less than the right value.

int age = 15;

bool result = (age < 18);

The result is true.

14. Greater Than or Equal To >=

The >= operator checks whether a value is greater than or equal to another value.

int marks = 40;

bool passed = (marks >= 40);

The result is true.

15. Less Than or Equal To <=

The <= operator checks whether a value is less than or equal to another value.

int age = 18;

bool result = (age <= 18);

The result is true.

16. Logical Operators

Logical operators are used to combine or modify Boolean expressions.

Operator Name
&& Logical AND
|| Logical OR
! Logical NOT

17. Logical AND &&

The && operator returns true when both conditions are true.

int age = 25;
bool hasID = true;

bool allowed = (age >= 18 && hasID);

Both conditions must be true for allowed to be true.

18. Logical OR ||

The || operator returns true when at least one condition is true.

bool isStudent = true;
bool isTeacher = false;

bool allowed = isStudent || isTeacher;

The result is true because one condition is true.

19. Logical NOT !

The ! operator reverses a Boolean value.

bool passed = true;

bool result = !passed;

The value of result becomes false.

20. Assignment Operator =

The = operator assigns a value to a variable.

int age = 20;

age = 25;

After the second statement, age contains 25.

21. Compound Assignment Operators

Compound assignment operators combine an operation with assignment.

Operator Equivalent
+= a = a + b
-= a = a - b
*= a = a * b
/= a = a / b
%= a = a % b

22. Increment and Decrement Operators

The increment operator ++ increases a value by one. The decrement operator -- decreases a value by one.

int count = 5;

count++;
count--;

23. Conditional Operator

The conditional operator ?: provides a compact way to choose between two expressions based on a condition.

int age = 20;

std::string result =
    (age >= 18) ? "Adult" : "Minor";

If the condition is true, the first expression is selected; otherwise, the second expression is selected.

24. Bitwise Operators

Bitwise operators work with the individual bits of integral values.

Operator Name
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Left shift
>> Right shift

25. Operator Categories

Category Examples
Arithmetic + - * / %
Relational == != > < >= <=
Logical && || !
Assignment = += -= *= /= %=
Increment/Decrement ++ --
Conditional ?:
Bitwise & | ^ ~ << >>

26. Unary and Binary Operators

A unary operator works with one operand.

int a = 10;

a++;

The ++ operator works with one operand.

A binary operator works with two operands.

int result = 10 + 20;

The + operator works with two operands here.

27. Operators in a Practical Example

#include <iostream>

int main() {

    int price = 100;
    int quantity = 5;

    int total = price * quantity;

    if (total >= 500) {
        std::cout << "Large order";
    } else {
        std::cout << "Normal order";
    }

    return 0;
}

This example uses arithmetic, relational, assignment, and conditional control logic together.

28. Complete Operators Example

#include <iostream>

int main() {

    int a = 20;
    int b = 6;

    std::cout << "Addition: " << a + b << std::endl;
    std::cout << "Subtraction: " << a - b << std::endl;
    std::cout << "Multiplication: " << a * b << std::endl;
    std::cout << "Division: " << a / b << std::endl;
    std::cout << "Remainder: " << a % b << std::endl;

    std::cout << "a == b: " << (a == b) << std::endl;
    std::cout << "a > b: " << (a > b) << std::endl;

    return 0;
}

29. Common Operator Mistakes

  • Using = when comparison with == is intended.
  • Forgetting that integer division discards the fractional part.
  • Using % with floating-point operands.
  • Forgetting operator precedence in complex expressions.
  • Using bitwise operators when logical operators are intended.
  • Changing a variable unexpectedly with increment or decrement operators.

30. Operators – Final Summary

Operators are essential for performing calculations, comparisons, assignments, logical operations, and other tasks in C++.

Category Main Operators Purpose
Arithmetic + - * / % Mathematical calculations
Relational == != > < >= <= Compare values
Logical && || ! Combine or modify conditions
Assignment = += -= *= /= %= Assign and update values
Increment/Decrement ++ -- Increase or decrease by one
Conditional ?: Select between two expressions
Bitwise & | ^ ~ << >> Operate on individual bits

📌 Key Points

  • Operators perform operations on one or more operands.
  • Arithmetic operators are used for mathematical calculations.
  • Relational operators compare two values and produce a Boolean result.
  • Logical operators work with Boolean conditions.
  • Assignment operators assign or update variable values.
  • ++ increments a value by one and -- decrements it by one.
  • The conditional operator ?: provides a compact conditional expression.
  • Bitwise operators work on individual bits of integral values.
  • = means assignment, while == means comparison for equality.
  • Operator precedence determines the order in which parts of an expression are evaluated.

🧠 Quick Quiz

Question: Which operator is used to compare two values for equality in C++?