Operators

Lesson 2 of 20

Arithmetic, comparison and logic: see why 17 / 5 is 3 in C++, what % keeps, and how && decides a condition.

The big picture

The verb set of C++

Operators are the verbs of the language: they add, compare, flip and combine the values you store.

Arithmetic

+, -, * and / do the math you expect; % keeps only the remainder — 17 % 5 is 2.

Integer division

When both sides are ints, / throws the fraction away: 17 / 5 is 3, not 3.4.

Compound shortcuts

a += 3 grows a in place, b-- shrinks b by one — same math, less typing.

Comparisons

==, !=, <, <=, > and >= ask a question and answer 1 (true) or 0 (false).

Logic glue

&& demands both sides true, || accepts one, ! flips a condition upside down.