Skip to main content
๐Ÿ“œ 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:

  1. Decide the boundaries before coding (who owns 90?).
  2. Order the chain from most-specific to catch-all.
  3. End with else only 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.