Beginner 8 min

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 + 5 gives 15, 10 - 5 gives 5
  • Remember: These have the lowest precedence among arithmetic operators

Multiplication (*) and Division (/)

  • What they do: Multiply and divide numbers
  • Integer division: 10 / 3 gives 3, not 3.33 (decimal part truncated)
  • Double division: 10.0 / 3.0 gives 3.33333
  • Remember: Multiplication and division happen BEFORE addition and subtraction

Modulo (%) - The Remainder Operator

  • What it does: Returns the remainder after division
  • Example: 17 % 5 gives 2 (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 * 2 equals 16, not 26 (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:

1 #include <iostream>
2
3 int main() {
4 int a{7};
5 int b{4};
6 double average = a + b / 2;
7 std::cout << "Average: " << average << "\n";
8 return 0;
9 }

Quick Quiz

  1. What is the result of 17 / 5 in C++?
3
3.4
4
  1. What does 23 % 4 evaluate to?
3
5
4
  1. What is the value of x after this statement?
int x = 10 + 3 * 2;
16
26
13

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.

Lesson Progress

  • Fix This Code
  • Quick Quiz
  • Practice Playground - run once