C++ chapters

Chapter 6 of 17

Operators

Try this in our C++ Compiler →

Arithmetic, comparison and logical

Operators are short symbols that act on one or more values. C++ groups them into arithmetic (+ - * / %), comparison (== != < > <= >=) and logical (AND OR NOT). Combine them to build the conditions that drive your program.

int a = 10, b = 3;
std::cout << a + b << " " << a % b << "\n";
std::cout << (a > b && b < 5) << "\n";

Precedence

Just like in maths, * binds tighter than +, so 2 + 3 * 4 is 14, not 20. When in doubt add parentheses — they cost nothing at runtime and make intent clear to anyone reading the code later.

Equality vs assignment

One of the most common beginner bugs is writing = (assignment) when you meant == (comparison). Read your conditions out loud — "if total equals one hundred" — to catch this kind of typo.

Try it yourself

C++ Compiler
Output
Code runs on the Play with Coding execution engine — your code is saved locally for next time.