Mastering Module 3

Operators & Expressions

Video Overview

Unlock the math and logic behind C++. You’ll master arithmetic operators, the tricky Integer Division, correct Order of Operations, and Logical Operators.

Goal: Mastering Math & Logic.

Notes

1. Arithmetic Operators & Expressions

C++ provides five basic arithmetic operators. These are binary operators because they require two operands.

Arithmetic.cpp

int a = 7 / 2; // Result: 3

double b = 7.0 / 2; // Result: 3.5

int r = 17 % 3; // Result: 2

  • Integral Division (/): Integers drop remainder (e.g., 7 / 2 is 3).
  • Floating-point Division: If one operand is float/double, result is decimal (e.g., 7.0 / 2 is 3.5).
  • Modulus (%): Returns remainder. Integers only.
⚠️ Caution on Divide Rules:
5 / 2 = 2 (Decimals lost).
5.0 / 2 = 2.5 (Double operand makes result double).
Interactive Test Run

See the output of Arithmetic Operators

#include <iostream> using namespace std; int main() { int a = 10, b = 5; cout << "|a + b = " << (a + b) << "| "; cout << "|a - b = " << (a - b) << "| "; cout << "|a * b = " << (a * b) << "| "; cout << "|a / b = " << (a / b) << "| "; return 0; }

2. Increment & Decrement Operators

These are unary operators that add or subtract 1 from a single variable.

Inc_Dec.cpp

int x = 10;

++x; // x is now 11 (Pre)

x--; // x is back to 10 (Post)

  • Pre-increment (++x): Updates the value immediately before use.
  • Post-increment (x++): Uses the current value first, then updates it afterward.
Interactive Test Run

Visualizing Pre vs Post Increment

#include <iostream> using namespace std; int main() { int x = 10; cout << "x++ = " << x++ << " | "; cout << "x = " << x << " | "; cout << "++x = " << ++x << endl; return 0; }

3. Precedence & Relational

Input numbers below to see how C++ evaluates the Order of Precedence and Relational logic.

Order of Precedence

C++ follows a specific Hierarchy of Operations. Operators with higher precedence are evaluated first. If equal, they use associativity (usually left to right).

  1. ( ): Highest priority.
  2. *, /, %: Equal priority, evaluated L to R.
  3. +, -: Lowest priority, evaluated L to R.
result = 5 + 2 * 4;
// 1. 2 * 4 = 8
// 2. 5 + 8 = 13
Relational Operators

Compare two values. Returns a Boolean value:

  • 1 (True): Condition met. (Non-zero is true).
  • 0 (False): Condition not met.
a == b // Equal to
a != b // Not equal to
a <= b // Less/Equal
Order of Precedence Expression: result = A + B * C
A B C
// Result here...
Relational Operators Compare X and Y:
X Y
// Result here...

4. Logical Operators & Compound Assignment

Interact with the cards below to see how these operators work in real-time.

Logical Operators

Combine multiple relational expressions into a single condition.

&& (AND): Both true || (OR): At least one true ! (NOT): Reverse
Current State: A = true, B = false

// Click Run to see evaluations...

Compound Assignment

Concise way to update variables. Arithmetic + Assignment (=).

+= (Add & Assign) -= (Sub & Assign) *= (Mult & Assign)
Enter Number (x)

// Enter a number and click Run...

Module 3 Knowledge Check

Answer the following multiple-choice questions to test your understanding.

1. What is the result of the expression 5 / 2?

Explanation: In C++, when you divide two integers, the fractional part is discarded (truncated). Although the mathematical result is 2.5, the integer result is 2.

2. Which operator is used for remainder in C++?

Explanation:The modulus operator % returns the remainder of an integer division. For example, 5 % 2 would result in 1.

3. Which operator has the highest precedence?

Explanation: In the order of operations (PEMDAS), multiplication * is performed before addition + , subtraction - , or assignment = .

4. What is the output of the following code?
int x = 5; x++; cout << x;

Explanation: The ++ is the increment operator, which increases the value of a variable by 1. Since x started at 5 , x++ makes it 6 .

5. What is the result of this expression?
10 + 6 / 2

Explanation: Because of operator precedence, division is performed before addition. First, 6 / 2 = 3, then 10 + 3 = 13.