Skip to content
Laviyx Learn

Try

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.

Beginner12 min readUpdated
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#

OperatorMeaningExampleResult
+Addition7 + 29
-Subtraction7 - 25
*Multiplication7 * 214
/Division7 / 23
%Remainder (modulo)7 % 21
C++
#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';
}
Output
3 3.5
1 2
-3 -1
  • Dividing two integers gives an integer; the fraction is dropped (towards zero, so -7 / 2 is -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#

C++
#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";
}
Output
1
6
202
2h 15m

C++ 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:

C++
#include <iostream>

int main() {
    int balance = 1000;
    balance += 250;   // balance = balance + 250
    balance -= 100;
    balance *= 2;
    balance /= 3;
    balance %= 7;
    std::cout << balance << '\n';
}
Output
3

Step 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 ++i increases i, then gives the new value.
  • Postfix i++ gives the old value, then increases i.
C++
#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';
}
Output
6 6
6 5

On 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:

OperatorMeaning
==Equal to
!=Not equal to
< >Less than, greater than
<= >=Less or equal, greater or equal
C++
#include <iostream>

int main() {
    int age = 20;
    std::cout << std::boolalpha;
    std::cout << (age == 20) << ' ' << (age != 20) << ' ' << (age >= 18) << '\n';
}
Output
true false true

Wrap comparisons in parentheses when printing them, because << has higher precedence than ==.

Logical operators#

OperatorMeaningTrue when
&&ANDboth sides are true
||ORat least one side is true
!NOTthe value is false
C++
#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';
}
Output
true
false
false

Short-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:

C++
#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";
    }
}
Output
No data or low average

Because 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:

C++
#include <iostream>
#include <string>

int main() {
    int marks = 72;
    std::string result = marks >= 40 ? "pass" : "fail";
    std::cout << result << '\n';
}
Output
pass

Keep ternaries short; nested ternaries are hard to read.

Operator precedence#

Like in maths, *, / and % run before + and -. A simplified order, from highest to lowest:

  1. ++ -- (postfix), function calls, []
  2. !, unary -, ++ -- (prefix)
  3. * / %
  4. + -
  5. << >>
  6. < <= > >=
  7. == !=
  8. &&
  9. ||
  10. ?: and assignment (=, +=, …)
C++
#include <iostream>

int main() {
    std::cout << 2 + 3 * 4 << ' ' << (2 + 3) * 4 << '\n';
}
Output
14 20

When 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
C++
#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';
}
Output
1250
1150
2300
766
3

Printing 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
C++
#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";
}
Output
1h 6m 40s

Key takeaway#

Finished this lesson?