C, C++, MFC  

Chapter 3: Operators and Expressions in C++

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.

OperatorNameExampleResult
+Addition5 + 3$8$
-Subtraction10 - 4$6$
*Multiplication2 * 6$12$
/Division10 / 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).

OperatorDescriptionExampleResult (if $x = 5$)
==Equal tox == 5true
!=Not equal tox != 10true
>Greater thanx > 6false
<Less thanx < 10true
>=Greater than or equal tox >= 5true
<=Less than or equal tox <= 4false

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.

OperatorDescriptionExample
&&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:

OperatorEquivalent ExpressionExample
+=a = a + ba += 5;
-=a = a - ba -= 2;
*=a = a * ba *= 3;
/=a = a / ba /= 4;
%=a = a % ba %= 2;

4. Increment and Decrement Operators

These are shortcuts used to increase or decrease a variable's value by exactly one.

OperatorNameDescription
++IncrementAdds $1$ to the variable's value.
--DecrementSubtracts $1$ from the variable's value.

These operators have two forms, with different behaviors when used within a larger expression:

  • Prefix Form (++x): Increments the value of $x$ before the expression is evaluated.

  • Postfix Form (x++): Increments the value of $x$ after the expression is evaluated.

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