C++ Operators
Operators are symbols that perform operations on values (called operands). You already used = to assign and + to add. C++ has many more, grouped into a few families.
Arithmetic operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 7 + 2 |
9 |
- |
Subtraction | 7 - 2 |
5 |
* |
Multiplication | 7 * 2 |
14 |
/ |
Division | 7 / 2 |
3 (integers!) |
% |
Modulus (remainder) | 7 % 2 |
1 |
++ |
Increment | x++ |
adds 1 |
-- |
Decrement | x-- |
subtracts 1 |
#include <iostream>
using namespace std;
int main() {
int a = 17, b = 5;
cout << a + b << " " << a - b << " " << a * b << "\n";
cout << a / b << " remainder " << a % b << "\n";
cout << 17.0 / 5 << "\n";
}22 12 85
3 remainder 2
3.4
The % operator only works on integers. It is perfect for checking divisibility: n % 2 == 0 means n is even.
Increment and decrement
++x (prefix) increases x and then gives the new value. x++ (postfix) gives the old value and then increases x:
#include <iostream>
using namespace std;
int main() {
int x = 5;
int a = x++; // a gets 5, then x becomes 6
int b = ++x; // x becomes 7, then b gets 7
cout << "a=" << a << " b=" << b << " x=" << x << "\n";
}a=5 b=7 x=7
Assignment operators
Compound assignment combines an operation with =:
#include <iostream>
using namespace std;
int main() {
int coins = 10;
coins += 5; cout << coins << "\n"; // 15
coins -= 3; cout << coins << "\n"; // 12
coins *= 2; cout << coins << "\n"; // 24
coins /= 5; cout << coins << "\n"; // 4
coins %= 3; cout << coins << "\n"; // 1
}15
12
24
4
1
Comparison operators
Comparisons produce a bool: true (printed as 1) or false (printed as 0).
| Operator | Meaning |
|---|---|
== |
equal to |
!= |
not equal to |
> / < |
greater / less than |
>= / <= |
greater / less than or equal |
Warning:
=assigns,==compares. Writingif (x = 5)assigns 5 toxand is always true — a classic bug. Compile with-Walland the compiler will warn you.
Logical operators
| Operator | Name | True when |
|---|---|---|
&& |
AND | both sides are true |
|| |
OR | at least one side is true |
! |
NOT | the operand is false |
#include <iostream>
using namespace std;
int main() {
int age = 20;
bool hasTicket = true;
cout << boolalpha; // print true/false instead of 1/0
cout << (age >= 18 && hasTicket) << "\n";
cout << (age < 12 || age > 65) << "\n";
cout << !hasTicket << "\n";
cout << (10 == 10.0) << "\n";
}true
false
false
true
&& and || short-circuit: if the left side already decides the result, the right side is not evaluated at all. That makes if (p != nullptr && p->ok()) safe.
Precedence
Like in maths, *, / and % bind tighter than + and -; comparisons come next, then &&, then ||, and assignment last. When in doubt, add parentheses — they cost nothing and make intent clear: (a + b) * c.
Bitwise operators (preview)
&, |, ^, ~, << and >> work on the individual bits of integers. For example 5 & 3 is 1 and 1 << 4 is 16. They are used in low-level code, flags and graphics.
Test yourself
What is 17 % 5?
- 3
- 2
- 3.4
- 12
After int x = 3; int y = x++;, what are x and y?
- x=4, y=4
- x=4, y=3
- x=3, y=4
- x=3, y=3
What does true || f() do with f()?
- Calls it first
- Calls it last
- Does not call it
- Compile error