Previous chapter: Chapter 2: Variables, Data Types, and Basic I/O in C++
In Chapter 2, you learned how to store values in variables. This chapter introduces operators, the symbols C++ uses to manipulate those values, perform calculations, and make comparisons. Combining operators and variables forms an expression, which is a unit of code that evaluates to a single result.
1. Arithmetic Operators
These are the fundamental operators used for mathematical calculations.
| Operator | Name | Example | Result |
|---|
| + | Addition | 5 + 3 | $8$ |
| - | Subtraction | 10 - 4 | $6$ |
| * | Multiplication | 2 * 6 | $12$ |
| / | Division | 10 / 3 | $3$ (Integer division) |
| % | Modulus (Remainder) | 10 % 3 | $1$ |
Important Note on Division: When both operands of the division operator (/) are integers, C++ performs integer division, which truncates the decimal part. To get a floating-point result, at least one operand must be a floating-point number (e.g., 10.0 / 3).
2. Relational and Logical Operators
These operators are essential for making decisions in your code (the topic of the next chapter).
Relational Operators
Used to compare two values, they always return a bool result (true or false).
| Operator | Description | Example | Result (if $x = 5$) |
|---|
| == | Equal to | x == 5 | true |
| != | Not equal to | x != 10 | true |
| > | Greater than | x > 6 | false |
| < | Less than | x < 10 | true |
| >= | Greater than or equal to | x >= 5 | true |
| <= | Less than or equal to | x <= 4 | false |
WARNING: A common mistake is using the assignment operator (=) when you mean the equality operator (==). if (x = 5) assigns the value $5$ to $x$, while if (x == 5) checks if $x$ is equal to $5$.
Logical Operators
Used to combine or negate boolean expressions.
| Operator | Description | Example |
|---|
| && | AND (both must be true) | (age > 18) && (is_citizen == true) |
| ` | | ` |
| ! | NOT (inverts the boolean value) | !is_ready |
3. Assignment and Compound Operators
The simplest assignment operator is =, which assigns the value of the right operand to the variable on the left.
Compound Assignment Operators combine an arithmetic operation with assignment, offering a concise syntax:
| Operator | Equivalent Expression | Example |
|---|
| += | a = a + b | a += 5; |
| -= | a = a - b | a -= 2; |
| *= | a = a * b | a *= 3; |
| /= | a = a / b | a /= 4; |
| %= | a = a % b | a %= 2; |
4. Increment and Decrement Operators
These are shortcuts used to increase or decrease a variable's value by exactly one.
| Operator | Name | Description |
|---|
| ++ | Increment | Adds $1$ to the variable's value. |
| -- | Decrement | Subtracts $1$ from the variable's value. |
These operators have two forms, with different behaviors when used within a larger expression:
int x = 10;
int y = 5;
// Prefix: y becomes (11 * 2) = 22. x becomes 11.
y = (++x) * 2;
// Postfix: y becomes (5 * 2) = 10. z then becomes 6.
int z = (y++) * 2;
5. Operator Precedence
Precedence defines the order in which operators in an expression are evaluated. For example, multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-). You can always override the default precedence using parentheses ().
$$ \frac{5 + 3}{4 \times 2} $$
int result_A = 5 + 3 * 4; // Evaluates as 5 + (3 * 4) = 17
int result_B = (5 + 3) * 4; // Evaluates as 8 * 4 = 32