Level 10’s conditions with C++ punctuation. Drag a score down a grading ladder, then reorder four correct lines and watch every A in the class quietly become a C.
=, missing braces, missing breakYou already know what a condition is and how AND, OR and NOT combine — that was level 10. Here it is with C++ punctuation on it.
Read down the ladder. The first condition that is true runs, and everything below it is not even tested. That is not an optimisation detail — it is the thing that makes the order you write them in part of the logic.
score >= 70 at the top and it catches 85 as well — every A and B in the class quietly becomes a C, and nothing anywhere reports a problem.Amber means tested and false. Green means it fired. Faded means never looked at.
In the backwards order, every single score above 70 gets a C. Nothing crashes. Nothing warns. The program is confidently, silently wrong for most of the class — which is level 11’s third column again, and it is caused purely by the order of four correct-looking lines.
You met the first in the error decoder. It is worth saying once more because it is the most expensive typo in the language:
The second is quieter. C++ lets you leave the braces off when the body is one line — and then the indentation lies to you:
Only the first line belongs to the if. The second is just the next statement, and it runs every time regardless. Verified — and with -Wall the compiler does say something:
warning: misleading indentation; statement is not part of the previous 'if'
Always use braces, even for one line. It costs two characters and it removes the entire class of bug. Professional codebases mandate it, and there is no counter-argument worth hearing.
When you are checking one variable against a list of exact values, switch reads more cleanly than a long ladder. It has one famous trap:
Leave a break out and execution falls through into the next case and keeps going. Verified: with day = 2 and no breaks, it prints Tue Wed — two answers to a question that had one.
It is occasionally deliberate. It is far more often a missing line.
Write four rungs for a real rule — grades, postage bands, ticket prices by age. Do not touch the conditions themselves.
Rearrange the rungs and find an input that now gets the wrong answer. Then find the input that is wrong in every order — there is usually a boundary that nobody wrote a rung for.
The second half is the interesting one. Ladders are usually correct in the middle of each band and wrong at the joins — exactly 80, exactly 90 — which is level 11’s boundary trio arriving in real code.
The first true rung wins and the rest are never tested. Narrowest test at the top.
Two characters, and a whole family of bugs stops being possible.
A ladder is nearly always right in the middle of a band and wrong at exactly the boundary.
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.
A grading ladder tests score >= 70 first, then >= 80, then >= 90. What grade does a score of 95 get?
What does if (age = 18) do?
A switch on day = 2 has cases 1, 2 and 3, and nobody wrote any break statements. What happens?
Why should you always use braces on an if, even for one line?
← back to the whole C++ track · stuck on anything? ask peter.