What is an expression?

An expression is a combination of literals, variables, operators, and function calls that can be evaluated to produce a result. Every expression has a value and a type.

#include <iostream>

int main() {
    // These are all expressions:
    5                   // literal expression, value: 5
    3 + 4              // arithmetic expression, value: 7
    2 * 6 - 1          // complex arithmetic expression, value: 11

    return 0;
}

Simple expressions

Literal expressions

The simplest expressions are just literals:

5           // integer expression with value 5
3.14        // floating-point expression with value 3.14
"Hello"     // string expression with value "Hello"

Variable expressions

Variables by themselves are expressions that can be used in statements:

#include <iostream>

int main() {
    int x = 10;
    
    // Variable expressions used in different contexts:
    std::cout << x << std::endl;    // x is a variable expression (evaluates to 10)
    int y = x;                      // x is a variable expression (copied to y)
    int z = x + 5;                  // x is a variable expression (used in arithmetic)

    return 0;
}

Arithmetic expressions

Arithmetic expressions use operators to combine values:

#include <iostream>

int main() {
    // Simple arithmetic expressions
    std::cout << 3 + 5 << std::endl;        // 8
    std::cout << 10 - 4 << std::endl;       // 6
    std::cout << 7 * 2 << std::endl;        // 14
    std::cout << 15 / 3 << std::endl;       // 5

    return 0;
}

Complex expressions

You can combine multiple operators and values:

#include <iostream>

int main() {
    std::cout << 2 + 3 * 4 << std::endl;           // 14 (not 20!)
    std::cout << (2 + 3) * 4 << std::endl;         // 20 (parentheses change order)
    std::cout << 10 - 3 + 2 << std::endl;          // 9
    std::cout << 8 / 2 * 3 << std::endl;           // 12

    return 0;
}

Expressions with variables

Variables can be part of expressions:

#include <iostream>

int main() {
    int a = 5;
    int b = 3;

    std::cout << a + b << std::endl;            // 8
    std::cout << a * 2 << std::endl;            // 10
    std::cout << a + b - 1 << std::endl;        // 7
    std::cout << a * b + 10 << std::endl;       // 25

    return 0;
}

Expressions in assignments

The right side of an assignment is always an expression:

#include <iostream>

int main() {
    int x = 5;                  // 5 is an expression
    int y = x + 3;              // x + 3 is an expression (evaluates to 8)
    int z = x * y - 10;         // x * y - 10 is an expression (evaluates to 30)

    std::cout << "x: " << x << std::endl;    // x: 5
    std::cout << "y: " << y << std::endl;    // y: 8
    std::cout << "z: " << z << std::endl;    // z: 30

    return 0;
}

Order of operations (operator precedence)

C++ follows mathematical rules for order of operations:

  1. Parentheses first: ()
  2. Multiplication and Division next: * / (left to right)
  3. Addition and Subtraction last: + - (left to right)
#include <iostream>

int main() {
    std::cout << 2 + 3 * 4 << std::endl;           // 14 (3*4=12, then 2+12=14)
    std::cout << (2 + 3) * 4 << std::endl;         // 20 (2+3=5, then 5*4=20)
    std::cout << 10 - 6 / 2 << std::endl;          // 7 (6/2=3, then 10-3=7)
    std::cout << (10 - 6) / 2 << std::endl;        // 2 (10-6=4, then 4/2=2)

    return 0;
}

Expressions produce values

Every expression "evaluates to" or "produces" a value:

#include <iostream>

int main() {
    // Each expression produces a value that gets assigned
    int result1 = 5 + 3;        // 5 + 3 evaluates to 8
    int result2 = 10 * 2;       // 10 * 2 evaluates to 20
    int result3 = result1 + result2;  // 8 + 20 evaluates to 28

    std::cout << result1 << std::endl;    // 8
    std::cout << result2 << std::endl;    // 20
    std::cout << result3 << std::endl;    // 28

    return 0;
}

Using parentheses for clarity

Even when not required, parentheses can make expressions clearer:

#include <iostream>

int main() {
    int a = 10, b = 5, c = 2;

    // These are equivalent, but second is clearer:
    int result1 = a + b * c;            // 20
    int result2 = a + (b * c);          // 20 (clearer intention)

    // Complex expression made clearer with parentheses:
    int complex = (a + b) * (c + 1);    // (10+5) * (2+1) = 15 * 3 = 45

    std::cout << result1 << std::endl;   // 20
    std::cout << result2 << std::endl;   // 20
    std::cout << complex << std::endl;   // 45

    return 0;
}

Common mistakes with expressions

Forgetting order of operations

// What you might expect: (2 + 3) * 4 = 20
// What actually happens: 2 + (3 * 4) = 14
int wrong = 2 + 3 * 4;

// Use parentheses to be explicit:
int right = (2 + 3) * 4;    // 20

Integer division

#include <iostream>

int main() {
    // Integer division truncates (cuts off) decimal part
    std::cout << 7 / 2 << std::endl;        // 3 (not 3.5!)
    std::cout << 9 / 4 << std::endl;        // 2 (not 2.25!)

    return 0;
}

Expression Statements

Expressions by themselves cannot be compiled - they need to be part of statements. When you add a semicolon to an expression, it becomes an expression statement. This is how expressions actually do work in your program:

#include <iostream>

int main() {
    int x = 5;
    int y = 3;

    // These are expression statements:
    x + y;                  // Valid expression statement (but result is discarded)
    std::cout << x + y;     // Expression statement that outputs the result
    x = x + 1;             // Assignment expression statement
    std::cout << "Hello";   // Output expression statement

    return 0;
}

Important: You cannot write just x + y in a C++ program and compile it. The expression needs to be part of a statement (with a semicolon) or used in other contexts like assignments or function calls.

Useful vs Useless Expression Statements

int main() {
    int a = 10;
    int b = 5;

    // Useless expression statements (results thrown away):
    a + b;              // Calculates 15 but does nothing with it
    a * 2;              // Calculates 20 but discards it

    // Useful expression statements (results are used):
    int sum = a + b;    // Stores the result in sum
    std::cout << a + b; // Outputs the result
    a = a + 1;          // Updates the value of a

    return 0;
}

Expression Statements vs Other Statements

int main() {
    // Declaration statement (not an expression statement)
    int x;

    // Expression statements:
    x = 10;                     // Assignment
    std::cout << x;             // Function call
    x + 5;                      // Arithmetic (result discarded)

    // Control flow statements (not expression statements)
    if (x > 5) {                // if statement
        std::cout << "Big!";    // This IS an expression statement inside the if
    }

    return 0;
}

Key Point: Most lines of code you write that end with a semicolon are expression statements!

Summary

  • Expressions combine literals, variables, and operators to produce values
  • Every expression has a value and a type
  • Order of operations matters: parentheses, then *//, then +/-
  • Expressions can be simple (like 5) or complex (like (a + b) * (c - d))
  • Use parentheses to make intentions clear and control order

Understanding expressions is crucial because most of C++ programming involves creating and evaluating expressions to solve problems.