cadence_learn
the_c++_track / level 05 of 15 ▶ 16 min

the order changes the answer.

Five operators, one you have never met, and a pair of expressions with the same numbers that give 0 and 1. Step through the order C++ actually works in.

after this level you'll be able to
  • Use % to test even, odd and exactly divisible
  • Work out which operator C++ evaluates first, and why
  • Spot an integer division that will quietly ruin an answer
the operators

five symbols, and one you have never met

Four of these you have known since primary school. The fifth is new, and it turns out to be one of the most useful things in the language.

7 + 2 // 9 add 7 - 2 // 5 subtract 7 * 2 // 14 multiply — a star, never an x 7 / 2 // 3 divide — WHOLE numbers, so no half 7 % 2 // 1 the REMAINDER left over

% is called modulus, and it answers “what is left over after dividing?” Seven divided by two is three, with one left over. That is the 1.

It sounds like a curiosity. It is not — it is how you ask is this number even? (n % 2 == 0), is it exactly divisible?, is this every third one?, and how you pull the last digit off a number. It will appear in your assignments constantly.

◈ ask an ai about this

“What does the % operator do in C++, and what is it actually useful for? Give me everyday examples.”

chatgpt ↗ claude ↗

try it yourself

the remainder, drawn

Drag the two sliders. The green squares are the remainder — the ones that would not fit into a complete row.

▤ lab 05a · what % actually means

Rows of the second number. Whatever is left over is the answer.

7
2
Try k = 2 and slide n. The remainder flips between 0 and 1 — that is exactly how you test for even and odd.
order matters

which operator goes first

C++ does not read left to right the way you read a sentence. It has an order, it is the one you learned in maths, and getting it wrong is quiet rather than loud.

the order, and why it changes the answer
1 · FIRST ( ) — brackets, innermost first 2 · THEN * / % — multiply, divide, remainder 3 · LAST + − — add and subtract AND WITHIN ONE LEVEL — LEFT TO RIGHT 1 / 2 * 2.0 this one goes first → 0 * 2.0 → 0 2.0 * 1 / 2 this one goes first → 2.0 / 2 → 1 Same numbers. Same three operators. 0 and 1 — because the first one starts with two whole numbers and loses the half immediately. Nothing warns you. Both compile. Both run. Only one is what you meant.
Order decides the answer, and not because the maths changed. Once whole-number division has thrown the remainder away, no amount of decimals further along the line can get it back.

Step through these and watch which piece gets worked out at each stage. The chip on the right tells you what kind of number the result is at that moment — and that is where the trouble hides.

▤ lab 05b · the expression stepper

The last two are the same numbers in a different order. Do those two back to back.

Pick an expression and press step.
◈ ask an ai about this

“In C++, why does 1 / 2 * 2.0 give 0 but 2.0 * 1 / 2 give 1? Walk me through it step by step.”

chatgpt ↗ claude ↗

two shorthands

the ones you will see everywhere

Adding to a variable is so common that C++ has a shorter way to write it. These two lines do exactly the same thing:

total = total + 5; // the level 9 way — clear, and always fine total += 5; // the same thing, said shorter

There is one for each operator: -=, *=, /=, %=. And for the most common case of all — adding exactly one — there is a shorthand for the shorthand:

count++; // add 1. you will see this in every loop you ever write count--; // take 1 away
the difference nobody explains

i++ and ++i both add one, and they differ in what the expression hands back. Verified on a real compiler:

int i = 5; cout << i++; prints 5, and i is then 6
int j = 5; cout << ++j; prints 6, and j is then 6

i++ means “give me the old value, then add one”. ++i means “add one, then give me the new value”. On a line of its own they are identical — and on a line of its own is where you should keep them. Mixing i++ into a bigger expression is how you write something nobody can read, including you.

the negative surprise

what happens below zero

Both / and % do something specific with negative numbers, and it is worth seeing once rather than discovering it in an assignment. Real output:

output
-7 / 2  = -3    // chopped toward zero, not down to -4
-7 % 2  = -1    // the remainder takes the sign of the LEFT number

So n % 2 == 1 is not a reliable test for odd — it is false for −7, which is odd. n % 2 != 0 works for every number. A small thing, and exactly the kind of edge case level 11 told you to go looking for.

the other way to write it

static_cast, which your professor may insist on

To force a real division you have to make one side a decimal. There are two ways to write that, and they do exactly the same thing here:

double avg = (double)a / b; // 3.5 — the short way, inherited from C double avg = static_cast<double>(a) / b; // 3.5 — the C++ way

Both were verified and both give 3.5. static_cast is longer and it is what most C++ courses ask for, for a reason worth knowing: it is deliberately hard to miss when reading. The short form (double) is easy to overlook in a dense line, and it will also perform conversions that are genuinely dangerous without saying so. static_cast is narrower and shouts.

which one to use

Use whichever your professor uses. If the lecture slides say static_cast, write static_cast — marks have been lost over less. If nobody has said, either is correct, and static_cast is the habit worth building.

do this together

predict, then check

☶ two people · 12 minutes · paper first, laptop second

Write expressions for each other. The rule: no compiling until both of you have committed to an answer.

person a

Write a line mixing whole numbers and decimals: 5 / 2 + 0.5, 3 * 7 % 4, 1 + 2 / 4 * 8.

person b

Write down your answer. A said theirs already. Then compile it and see who was right — often neither.

The disagreements are the whole value. Nearly all of them come from one of two things: the order the operators fire, or an integer division that quietly threw away a half early on. Once you have argued about six of these you will never write a / b again without checking what kind of numbers a and b are.

what to keep

three things worth remembering

01

% is more useful than it looks

Even or odd, exactly divisible, every nth one, the last digit. It shows up constantly.

02

Brackets cost nothing

If you had to think about the order for more than a second, write the brackets. Nobody has ever been marked down for them.

03

Lost halves never come back

Once whole-number division has thrown the remainder away, decimals later in the line cannot recover it.

check yourself

5 questions before you move on

Not recall — these are the shapes an exam actually uses. Every answer below was produced by compiling and running the code, so if you disagree with one, the compiler is the one to believe.

▢ check yourself5 questions

Have a real go before revealing. Being wrong here is worth more than being right in three weeks.

01

What does this print?

cout << 2 + 3 * 4;
02

And this?

cout << 7 % 3;
03

This is the one worth getting wrong now rather than in an exam.

cout << 1 / 2 * 2.0;
04

What are the two numbers printed?

int i = 5; cout << i++ << " " << i;
05

Careful with this one.

cout << -7 % 2;
answered: 0 of 5right first time: 0
Stuck? Peter reads these personally and replies to your email.
Ask Peter →