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.
Notes
1. Arithmetic Operators & Expressions
C++ provides five basic arithmetic operators. These are binary operators because they require two operands.
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.
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.
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).
- ( ): Highest priority.
- *, /, %: Equal priority, evaluated L to R.
- +, -: 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
// Result here...// Result here...4. Logical Operators & Compound Assignment
Interact with the cards below to see how these operators work in real-time.
Combine multiple relational expressions into a single condition.
// Click Run to see evaluations...
Concise way to update variables. Arithmetic + Assignment (=).
// Enter a number and click Run...
Module 3 Knowledge Check
Answer the following multiple-choice questions to test your understanding.