๐ WAYPOINT LESSON
Classification, validation, menus
โญ beginnerโณ 10 min read๐ Lesson 15 of 256
The three if/else patterns the exam reuses, plus trace strategy.
Classification problems map values to categories โ letter grades, shipping tiers, tax brackets. The recipe:
- Decide the boundaries before coding (who owns 90?).
- Order the chain from most-specific to catch-all.
- End with
elseonly when a default category truly exists.
Validation problems check inputs and reject bad ones early:
if (quantity < 1) {
return "invalid";
}
if (quantity > 100) {
return "too many";
}
return "ok";
Menu logic selects behavior by a choice code:
if (choice == 1) {
...
} else if (choice == 2) {
...
} else {
...
}
The exam's favorite trick: give you code with a subtle flaw (an off-by-one,
an unreachable branch, a missing else) and four candidate outputs. The
defense is the same trace discipline: walk each test input statement by
statement, and check every branch condition it passes on the way.