cadence_learn
the_c++_track / level 07 of 15 ▶ 15 min

the first true rung wins.

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.

after this level you'll be able to
  • Predict which branch of a ladder runs for any input
  • Explain why the order of conditions changes the answer
  • Avoid the three classic traps: =, missing braces, missing break
level 10, in c++

the ladder, and the one branch that runs

You 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.

if (score >= 90) { cout << "A"; } else if (score >= 80) { cout << "B"; } else { cout << "needs work"; }

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.

first true rung wins
if (score >= 90) tested first — false, so skip else if (score >= 80) true — THIS one runs score is 85 else if (score >= 70) never even tested else never even reached EXACTLY ONE branch of a ladder ever runs. The moment one is true, the rest are not even looked at — which is why the order you write them in is part of the logic.
Write the narrowest test first. Put 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.
◈ ask an ai about this

“How does an if / else if / else chain work in C++, and why does the order I write the conditions in matter?”

chatgpt ↗ claude ↗

try it yourself

drag the score and watch which rung fires

▤ lab 06a · the ladder

Amber means tested and false. Green means it fired. Faded means never looked at.

85
Slide the score. Then press “backwards order” and slide it again.
the bug you just made

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.

the two traps

one equals sign, and no braces

You met the first in the error decoder. It is worth saying once more because it is the most expensive typo in the language:

if (age = 18) // sets age to 18, then treats 18 as true. ALWAYS runs. if (age == 18) // asks whether age is 18. what you meant.

The second is quieter. C++ lets you leave the braces off when the body is one line — and then the indentation lies to you:

if (a > 10) cout << "big"; cout << "this runs no matter what"; // NOT part of the if

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:

terminal
warning: misleading indentation; statement is not part of the previous 'if'
the rule that ends this forever

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.

◈ ask an ai about this

“What is the difference between = and == in C++, and why should I always use braces with an if?”

chatgpt ↗ claude ↗

one more shape

switch, and the missing break

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:

switch (day) { case 1: cout << "Mon"; break; case 2: cout << "Tue"; break; default: cout << "something else"; }

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.

do this together

find the order that breaks it

☶ two people · 12 minutes

Write a grading ladder. Then try to break it by reordering only.

person a

Write four rungs for a real rule — grades, postage bands, ticket prices by age. Do not touch the conditions themselves.

person b

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.

what to keep

three things worth remembering

01

Order is logic

The first true rung wins and the rest are never tested. Narrowest test at the top.

02

Braces, always

Two characters, and a whole family of bugs stops being possible.

03

Test the joins

A ladder is nearly always right in the middle of a band and wrong at exactly the boundary.

check yourself

4 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 yourself4 questions

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

01

A grading ladder tests score >= 70 first, then >= 80, then >= 90. What grade does a score of 95 get?

02

What does if (age = 18) do?

03

A switch on day = 2 has cases 1, 2 and 3, and nobody wrote any break statements. What happens?

04

Why should you always use braces on an if, even for one line?

answered: 0 of 4right first time: 0
Stuck? Peter reads these personally and replies to your email.
Ask Peter →