C++20 sections · 1024 units
Open in Course

Order of Conditions

Why sequence matters

The order you write conditions changes your program's behavior. I'll show you why cpp if (x > 5) else if (x > 10) is different from cpp if (x > 10) else if (x > 5) You put more specific conditions first.

Testing grade >= 90 before grade >= 80 catches A students before the B check. Reverse them and all A students become B students. If you write if (x > 0) { } else if (x > 100) { }, the second condition never runs.

The first condition catches all positive numbers, including those over 100.