Arithmetic Operators
Master C++ arithmetic operations and understand operator precedence for accurate calculations
Master the fundamental arithmetic operators in C++ and understand how operator precedence affects your calculations.
A Simple Example
#include <iostream>
int main() {
int a{10};
int b{3};
// Basic arithmetic
std::cout << "Addition: " << a + b << "\n"; // 13
std::cout << "Subtraction: " << a - b << "\n"; // 7
std::cout << "Multiplication: " << a * b << "\n"; // 30
std::cout << "Division: " << a / b << "\n"; // 3 (integer division!)
std::cout << "Modulo: " << a % b << "\n"; // 1 (remainder)
// Division with doubles
double x{10.0};
double y{3.0};
std::cout << "Double division: " << x / y << "\n"; // 3.33333
return 0;
}
Breaking It Down
Addition (+) and Subtraction (-)
- What they do: Basic mathematical addition and subtraction
- Works with: All numeric types (int, double, float, etc.)
-
Example:
10 + 5gives15,10 - 5gives5 - Remember: These have the lowest precedence among arithmetic operators
Multiplication (*) and Division (/)
- What they do: Multiply and divide numbers
-
Integer division:
10 / 3gives3, not3.33(decimal part truncated) -
Double division:
10.0 / 3.0gives3.33333 - Remember: Multiplication and division happen BEFORE addition and subtraction
Modulo (%) - The Remainder Operator
- What it does: Returns the remainder after division
-
Example:
17 % 5gives2(17 = 5*3 + 2) -
Use cases: Check even/odd (
n % 2 == 0), cycle through ranges, extract digits - Remember: Only works with integers, not floating-point numbers
Operator Precedence
- Order: Multiplication, Division, and Modulo happen FIRST
- Then: Addition and Subtraction happen AFTER
-
Example:
10 + 3 * 2equals16, not26(multiply first!) -
Remember: Use parentheses
()to override precedence when needed
Why This Matters
- Every program deals with numbers - from calculating game character damage to processing financial transactions.
- Understanding operators and their precedence is the difference between correct results and subtle bugs that take hours to find.
Critical Insight
The modulo operator (%) is your best friend for cyclical problems! Need to wrap around after reaching a limit? Check if a number is even or odd? Extract individual digits? Modulo has you covered.
Think of it like a clock: 15 o'clock % 12 = 3 o'clock. The number "wraps around" after hitting the divisor.
// Check if even or odd
if (number % 2 == 0) {
std::cout << "Even" << "\n";
}
// Cycle through 0-9
int digit = largeNumber % 10;
// Wrap around array indices
int index = (currentIndex + 1) % arraySize;
Best Practices
Use parentheses for clarity: Even when not needed, (a + b) * c is clearer than a + b * c.
Be aware of integer division: When you need decimal results, use at least one double: a / 2.0 instead of a / 2.
Use modulo for cycling: The % operator is perfect for wrapping indices or checking even/odd numbers.
Remember operator precedence: * / % happen before + -. Use parentheses when in doubt.
Common Mistakes
Integer Division Surprise: Writing 5/2 and expecting 2.5 but getting 2. Use 5.0/2.0 or 5/2.0 for decimal results.
Operator Precedence Confusion: Writing int result = 10 + 5 * 2; and thinking it's 30 instead of 20. Multiplication happens first!
Modulo with Negatives: The behavior of % with negative numbers can be surprising. -7 % 3 might not give what you expect.
Dividing by zero: Division or modulo by zero causes undefined behavior or crashes. Always validate divisors.
Debug Challenge
This program tries to calculate the average of two integers. Click the highlighted line to fix the integer division bug:
Quick Quiz
- What is the result of
17 / 5in C++?
- What does
23 % 4evaluate to?
- What is the value of
xafter this statement?
int x = 10 + 3 * 2;
Practice Playground
Time to try out what you just learned! Play with the example code below, experiment by making changes and running the code to deepen your understanding.
Output:
Error:
Lesson Progress
- Fix This Code
- Quick Quiz
- Practice Playground - run once