C++ course menu
C++ Operators: Arithmetic, Comparison, Logical, Assignment and Increment
Learn the C++ operators: + - * / %, integer division, compound assignment, ++ and --, comparison and logical operators, short-circuit evaluation, the ternary operator, operator precedence, and common pitfalls.
On this page
Operators are the symbols that do work on values: adding, comparing, combining conditions. Most look like their maths equivalents, but a few behave in ways that surprise beginners.
Arithmetic operators#
#include <iostream>
int main() {
std::cout << 7 / 2 << ' ' << 7.0 / 2 << '\n';
std::cout << 7 % 2 << ' ' << 20 % 6 << '\n';
std::cout << -7 / 2 << ' ' << -7 % 2 << '\n';
}3 3.5
1 2
-3 -1- Dividing two integers gives an integer; the fraction is dropped (towards zero, so
-7 / 2is-3). %works only on integers. Its result has the sign of the left operand.- Dividing an integer by zero is undefined behaviour and usually crashes the program. Check the divisor first.
Useful % patterns#
#include <iostream>
int main() {
int n = 2026;
std::cout << (n % 2 == 0) << '\n'; // even?
std::cout << n % 10 << '\n'; // last digit
std::cout << n / 10 << '\n'; // remove last digit
int total_minutes = 135;
std::cout << total_minutes / 60 << "h " << total_minutes % 60 << "m\n";
}1
6
202
2h 15mC++ has no power operator: ^ means bitwise XOR, not "to the power of". Use std::pow(2, 10) from <cmath>, which returns a double.
Assignment and compound assignment#
= stores a value. Compound operators update a variable in place:
#include <iostream>
int main() {
int balance = 1000;
balance += 250; // balance = balance + 250
balance -= 100;
balance *= 2;
balance /= 3;
balance %= 7;
std::cout << balance << '\n';
}3Step by step, the value goes 1000 → 1250 → 1150 → 2300 → 766 → 3. Note that /= 3 is integer division (2300 / 3 is 766), and 766 % 7 is 3.
Increment and decrement#
++ adds 1 and -- subtracts 1. There are two forms:
- Prefix
++iincreasesi, then gives the new value. - Postfix
i++gives the old value, then increasesi.
#include <iostream>
int main() {
int a = 5;
int b = ++a; // a becomes 6, b gets 6
int c = 5;
int d = c++; // d gets 5, then c becomes 6
std::cout << a << ' ' << b << '\n';
std::cout << c << ' ' << d << '\n';
}6 6
6 5On its own line (i++; or ++i;) both forms do the same thing. Do not use ++ on the same variable twice in one expression, such as i++ + i++; the result is unspecified or undefined.
Comparison operators#
Comparisons produce a bool:
#include <iostream>
int main() {
int age = 20;
std::cout << std::boolalpha;
std::cout << (age == 20) << ' ' << (age != 20) << ' ' << (age >= 18) << '\n';
}true false trueWrap comparisons in parentheses when printing them, because << has higher precedence than ==.
Logical operators#
#include <iostream>
int main() {
int age = 25;
bool has_ticket = true;
std::cout << std::boolalpha;
std::cout << (age >= 18 && has_ticket) << '\n';
std::cout << (age < 12 || age > 60) << '\n';
std::cout << !has_ticket << '\n';
}true
false
falseShort-circuit evaluation#
&& stops as soon as the left side is false, and || stops as soon as the left side is true. This lets you guard a risky check:
#include <iostream>
int main() {
int count = 0;
int total = 50;
if (count != 0 && total / count > 10) {
std::cout << "High average\n";
} else {
std::cout << "No data or low average\n";
}
}No data or low averageBecause count != 0 is false, total / count is never evaluated, so there is no division by zero.
The ternary operator#
condition ? a : b picks one of two values. It is a compact if/else for simple choices:
#include <iostream>
#include <string>
int main() {
int marks = 72;
std::string result = marks >= 40 ? "pass" : "fail";
std::cout << result << '\n';
}passKeep ternaries short; nested ternaries are hard to read.
Operator precedence#
Like in maths, *, / and % run before + and -. A simplified order, from highest to lowest:
++--(postfix), function calls,[]!, unary-,++--(prefix)*/%+-<<>><<=>>===!=&&||?:and assignment (=,+=, …)
#include <iostream>
int main() {
std::cout << 2 + 3 * 4 << ' ' << (2 + 3) * 4 << '\n';
}14 20When in doubt, add parentheses. They cost nothing and make your intent clear.
Common mistakes#
Practice#
Practice exercise
Rewrite the compound-assignment example so it prints balance after every step, starting from 1000 and applying += 250, -= 100, *= 2, /= 3 and %= 7 in order. Predict each value before you run it.
Show solution
#include <iostream>
int main() {
int balance = 1000;
balance += 250;
std::cout << balance << '\n';
balance -= 100;
std::cout << balance << '\n';
balance *= 2;
std::cout << balance << '\n';
balance /= 3;
std::cout << balance << '\n';
balance %= 7;
std::cout << balance << '\n';
}1250
1150
2300
766
3Printing intermediate values is the fastest way to check your reasoning when a calculation gives an unexpected result.
Practice exercise
Given int seconds = 4000;, print it as hours, minutes and seconds in the form 1h 6m 40s.
Show hint
There are 3600 seconds in an hour. Use / for whole units and % for what is left.
Show solution
#include <iostream>
int main() {
int seconds = 4000;
int hours = seconds / 3600;
int minutes = seconds % 3600 / 60;
int secs = seconds % 60;
std::cout << hours << "h " << minutes << "m " << secs << "s\n";
}1h 6m 40sKey takeaway#
Finished this lesson?