Skip to content

Operators

6.1 — Operator Precedence and Associativity

When multiple operators appear in a single expression, precedence and associativity determine the order of evaluation.

  • Precedence: Determines which operator is evaluated first (e.g., multiplication has higher precedence than addition).

  • Associativity: If two operators have the same precedence, associativity determines whether they are evaluated from left-to-right or right-to-left.

  • Best Practice: When in doubt, use parentheses () to make your intent explicit. It makes code more readable and prevents subtle bugs.


6.2 — Arithmetic Operators

C++ includes standard mathematical operators:

  • Binary: + (addition), - (subtraction), * (multiplication), / (division).

  • Unary: + (positive), - (negative).

  • Integer Division: When dividing two integers (e.g., 7 / 2), C++ drops the remainder and returns an integer (3). To get a decimal, at least one operand must be a floating-point number (7.0 / 2).


6.3 — Remainder and Exponentiation

  • Remainder (Modulus) %: Returns the remainder after integer division (e.g., 7 % 2 is 1). This only works with integers.

  • Exponentiation: C++ does not have a built-in operator for exponents (like ^). You must use the std::pow(base, exp) function from the <cmath> header.


6.4 — Increment/Decrement and Side Effects

These operators increase or decrease a variable by 1.

  • Prefix (++x, --x): Increments/decrements the variable and then returns the value.

  • Postfix (x++, x--): Returns the current value and then increments/decrements the variable.

  • Side Effects: A side effect is a change to the program's state (like changing a variable's value). Avoid using the same variable more than once in an expression where it also has a side effect (e.g., x = x++), as this often leads to undefined behavior.


6.5 — The Comma Operator

The comma operator allows you to evaluate multiple expressions where only one is expected.

  • It evaluates the left operand, discards the result, and then evaluates the right operand, returning that result.

  • Use Case: Almost exclusively used inside for loops. In almost all other cases, it makes code confusing and should be avoided.


6.6 — The Conditional Operator (Ternary)

The only operator in C++ that takes three operands. It serves as a shortcut for a simple if-else statement.

  • Syntax: condition ? expression1 : expression2;

  • If the condition is true, expression1 is evaluated; otherwise, expression2 is evaluated.

  • Constraint: Both expressions must evaluate to the same (or compatible) type.


6.7 — Relational Operators and Floating Point Comparisons

Used to compare two values.

  • Operators: == (equal to), != (not equal), >, <, >=, <=.

  • Floating Point Warning: Because of tiny rounding errors, you should never compare floating-point numbers using ==.

    • Solution: Check if the difference between the two numbers is smaller than a tiny threshold called an "epsilon."

6.8 — Logical Operators

Used to combine multiple boolean conditions.

  • Logical NOT (!): Inverts a boolean (true becomes false).

  • Logical AND (&&): True only if both operands are true.

  • Logical OR (||): True if at least one operand is true.

  • Short-circuit Evaluation: C++ is efficient. In A && B, if A is false, B is never evaluated because the whole expression cannot be true. Similarly, in A || B, if A is true, B is skipped.