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.
% to test even, odd and exactly divisibleFour 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.
% 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.
Drag the two sliders. The green squares are the remainder — the ones that would not fit into a complete row.
Rows of the second number. Whatever is left over is the answer.
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.
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.
The last two are the same numbers in a different order. Do those two back to back.
Adding to a variable is so common that C++ has a shorter way to write it. These two lines do exactly the same thing:
There is one for each operator: -=, *=, /=, %=. And for the most common case of all — adding exactly one — there is a shorthand for the shorthand:
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.
Both / and % do something specific with negative numbers, and it is worth seeing once rather than discovering it in an assignment. Real 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.
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:
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.
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.
Write a line mixing whole numbers and decimals: 5 / 2 + 0.5, 3 * 7 % 4, 1 + 2 / 4 * 8.
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.
Even or odd, exactly divisible, every nth one, the last digit. It shows up constantly.
If you had to think about the order for more than a second, write the brackets. Nobody has ever been marked down for them.
Once whole-number division has thrown the remainder away, decimals later in the line cannot recover it.
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.
Have a real go before revealing. Being wrong here is worth more than being right in three weeks.
What does this print?
And this?
This is the one worth getting wrong now rather than in an exam.
What are the two numbers printed?
Careful with this one.
← back to the whole C++ track · stuck on anything? ask peter.