Lesson 3 • Beginner

    Operators

    Master arithmetic, comparison, logical, and assignment operators to perform calculations and make decisions in your code.

    What You'll Learn

    • Arithmetic operators (+, -, *, /, %)
    • Comparison and logical operators
    • Increment, decrement, and compound assignment
    • Practical calculations and the ternary operator

    Arithmetic Operators

    Arithmetic operators perform mathematical calculations. Think of them as a calculator built into the language.

    OperatorNameExampleResult
    +Addition10 + 313
    -Subtraction10 - 37
    *Multiplication10 * 330
    /Division10 / 33 (int!)
    %Modulus10 % 31

    ⚠️ Important: When both operands are integers, / performs integer division (truncates decimals). Use 10.0 / 3 for decimal results.

    Arithmetic Operators

    Practice math operations including a time converter

    Try it Yourself »
    C++
    #include <iostream>
    using namespace std;
    
    int main() {
        int a = 17, b = 5;
        
        // Arithmetic operators
        cout << "=== Arithmetic Operators ===" << endl;
        cout << a << " + " << b << " = " << (a + b) << endl;   // 22
        cout << a << " - " << b << " = " << (a - b) << endl;   // 12
        cout << a << " * " << b << " = " << (a * b) << endl;   // 85
        cout << a << " / " << b << " = " << (a / b) << endl;   // 3 (integer division!)
        cout << a << " % " << b << " = " << (a % b) << endl;  
    ...

    Comparison & Logical Operators

    Comparison operators compare two values and return true (1) or false (0). Logical operators combine multiple conditions.

    Comparison

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

    Logical

    &&  AND (both true)
    ||  OR  (one true)
    !   NOT (invert)

    Comparison & Logical Operators

    Combine conditions with AND, OR, and NOT operators

    Try it Yourself »
    C++
    #include <iostream>
    using namespace std;
    
    int main() {
        int age = 20;
        double gpa = 3.7;
        bool hasPermission = true;
        
        // Comparison operators (return true/false)
        cout << "=== Comparison Operators ===" << endl;
        cout << "age == 20: " << (age == 20) << endl;    // 1 (true)
        cout << "age != 18: " << (age != 18) << endl;    // 1 (true)
        cout << "age > 21: " << (age > 21) << endl;      // 0 (false)
        cout << "age <= 20: " << (age <= 20) << endl;     // 1 (true)
        cou
    ...

    Increment & Compound Assignment

    Learn pre/post increment and shorthand assignment operators

    Try it Yourself »
    C++
    #include <iostream>
    using namespace std;
    
    int main() {
        // Increment and Decrement
        cout << "=== Increment & Decrement ===" << endl;
        int x = 5;
        
        cout << "x = " << x << endl;       // 5
        cout << "x++ = " << x++ << endl;   // 5 (uses THEN increments)
        cout << "x now = " << x << endl;   // 6
        cout << "++x = " << ++x << endl;   // 7 (increments THEN uses)
        
        int y = 10;
        cout << "\ny-- = " << y-- << endl;   // 10
        cout << "y now = " << y << endl;     // 9
        c
    ...

    Practical Examples

    Build a temperature converter, BMI calculator, and discount calculator

    Try it Yourself »
    C++
    #include <iostream>
    using namespace std;
    
    int main() {
        // === Temperature Converter ===
        double celsius = 37.5;
        double fahrenheit = (celsius * 9.0 / 5.0) + 32;
        cout << celsius << "°C = " << fahrenheit << "°F" << endl;
        
        // === BMI Calculator ===
        double weight = 75.0;  // kg
        double height = 1.80;  // meters
        double bmi = weight / (height * height);
        cout << "\nBMI: " << bmi << endl;
        
        bool isHealthy = (bmi >= 18.5) && (bmi <= 24.9);
        cout << "Healthy 
    ...

    Common Mistakes

    ⚠️ = vs ==: = assigns a value, == compares. Writing if (x = 5) assigns 5 to x instead of comparing!

    ⚠️ Integer division: 7 / 2 gives 3, not 3.5. Cast to double for decimals.

    ⚠️ Pre vs post increment: ++x increments before use, x++ increments after. This matters in expressions!

    ⚠️ Division by zero: Dividing by zero causes undefined behavior in C++. Always check the divisor.

    Pro Tips

    💡 Use parentheses: When in doubt about operator precedence, add parentheses: (a + b) * c is clearer than a + b * c.

    💡 Ternary shorthand: result = (condition) ? valueIfTrue : valueIfFalse; replaces simple if-else.

    💡 Modulus for patterns: Use % to check even/odd, cycle through values, or wrap indices.

    📋 Quick Reference

    CategoryOperators
    Arithmetic+ - * / %
    Comparison== != > < >= <=
    Logical&& || !
    Assignment= += -= *= /= %=
    Increment++ --
    Ternarycondition ? a : b

    Lesson Complete!

    You can now perform calculations, compare values, and combine conditions. Next up: Control Flow — use if statements and switch cases to make your programs smart.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service