Operators recap

Excellent work! You've learned about operators and expressions in C++. Let's review the key concepts from this section.

Operator Precedence

Always use parentheses to clarify operator precedence when there's any potential for confusion.

Arithmetic Operators

The arithmetic operators work just like standard mathematics. The remainder operator (%) returns what's left over after integer division.

Increment and Decrement Operators

The increment and decrement operators provide an easy way to add or subtract 1 from numbers. Prefer the prefix versions whenever possible—they're more efficient and less likely to cause problems.

Side Effects

Be cautious of side effects, especially regarding the order in which function parameters are evaluated. Don't use a variable that has a side effect applied more than once in a single statement.

Comma Operator

The comma operator can combine multiple statements into one. However, writing statements separately is usually clearer.

Conditional Operator

The conditional operator (?:) (also called the arithmetic if operator) is a ternary operator taking three operands. Given a conditional operation condition ? a : b, if condition evaluates to true, then a is evaluated; otherwise, b is evaluated. Parenthesize the conditional operator as follows:

  • Parenthesize the entire conditional operator when used in compound expressions
  • For readability, parenthesize the condition if it contains operators (excluding function call operators)

Relational Operators

Relational operators let you compare floating-point numbers. Be very careful using equality and inequality on floating-point values.

Logical Operators

Logical operators let us create compound conditional statements.

Key Terminology

  • Operator precedence: Rules that determine the order in which operators are evaluated
  • Arithmetic operators: Operators for basic mathematical operations (+, -, *, /, %)
  • Remainder operator: Operator (%) that returns what's left over after integer division
  • Increment operator: Operator (++) that adds 1 to a variable
  • Decrement operator: Operator (--) that subtracts 1 from a variable
  • Prefix version: Increment/decrement operator placed before the variable (++x, --x)
  • Postfix version: Increment/decrement operator placed after the variable (x++, x--)
  • Side effect: When an expression modifies state beyond its evaluation
  • Comma operator: Operator (,) that evaluates multiple expressions in sequence
  • Conditional operator: Ternary operator (?:) that selects between two values based on a condition
  • Arithmetic if operator: Another name for the conditional operator
  • Relational operators: Operators that compare two values (<, >, <=, >=, ==, !=)
  • Logical operators: Operators that combine boolean expressions (&&, ||, !)

Best Practices

Use parentheses for clarity

When operator precedence might be unclear, use parentheses to make your intent explicit.

Prefer prefix increment/decrement

The prefix versions (++x, --x) are more efficient and less error-prone than postfix versions.

Avoid side effects in complex expressions

Don't use variables with side effects multiple times in a single statement—it leads to undefined behavior.

Write clear statements

While the comma operator allows combining statements, writing them separately is usually more readable.

Parenthesize conditional operators

Follow the parenthesization guidelines to make conditional operator usage clear and unambiguous.

Be careful with floating-point comparisons

Never use equality operators (== or !=) to compare floating-point values due to rounding errors.

Looking Forward

Operators are fundamental tools in C++. The principles you've learned—proper precedence handling, avoiding side effects, careful floating-point comparisons—will serve you throughout your C++ journey. Practice applying these concepts to write clear, correct, and efficient code.