Skip to content
Ameen Abozaid
BEG-012

Conditions

  • 6 Lessons
  • 65 min of reading
  • 4 h practice (estimate)
On this page 8 topics

Overview

Topics

  • if
  • else
  • else if
  • switch
  • switch expressions
  • nested conditions

Learn first

65 min of reading · 4 h of practice (estimate)

Learning outcomes

  1. Write if/else if/else chains that cover every case, including boundaries.
  2. Choose between an if chain, a switch statement, and a switch expression.
  3. Simplify nested conditions using logical operators and early exits.
  4. Identify unreachable or overlapping branches.

Lessons

  1. 01Making decisions with if10 min read
  2. 02else if chains and boundary values13 min read
  3. 03Classic switch and fall-through10 min read
  4. 04Switch expressions (modern Java)10 min read
  5. 05Taming nested conditions11 min read
  6. 06Demo: shipping cost rules and their boundaries11 min read

Lab and exercises

Lab

BEG-012-LAB1 · Shipping cost calculator

Outcomes: 1–4 · Time: ~2 h

  1. Write the rules on paper: cost by weight band, extra cost by zone, discount for members.
  2. Code the calculator with hard-coded inputs first, then read them with Scanner.
  3. Flatten any nesting deeper than two levels.
  4. Build a test table and work out every expected result by hand before running the code.

Acceptance: a table of at least 10 test inputs with expected and actual results, including every weight boundary.

Exercises (assessment: six exercises)

IDOutcomeTask
BEG-012-EX011Print "negative", "zero", or "positive" for an int.
BEG-012-EX021Map a score to grades A-F with an else if chain; test 90, 89, 60, 59.
BEG-012-EX032Convert a month number (1-12) to days using a classic switch with grouped cases.
BEG-012-EX042Rewrite EX03 as a switch expression. Which version do you prefer, and why?
BEG-012-EX053Flatten if (a) { if (b) { if (c) { run(); } } } once with && and once with early exits.
BEG-012-EX064Find the problem in if (x > 5) {...} else if (x > 10) {...} and in a chain that tests x >= 0 then x > 100.
Show solutions
  • EX01 if (n < 0) ... else if (n == 0) ... else ....
  • EX02 Order from highest: >= 90, >= 80, and so on. 90 gives A, 89 gives B, 60 gives D, 59 gives F.
  • EX03 case 4: case 6: case 9: case 11: days = 30; break; and so on; February is 28 (or 29), default for the rest.
  • EX04 int days = switch (m) { case 4, 6, 9, 11 -> 30; case 2 -> 28; default -> 31; }; Preference is open; the arrow form is shorter and has no fall-through.
  • EX05 if (a && b && c) run(); or if (!a) return; if (!b) return; if (!c) return; run();.
  • EX06 x > 10 can never be reached: any x above 10 is already caught by x > 5 (unreachable branch). In the second chain every x above 100 also satisfies x >= 0, so the second test is never reached for the values it was meant for.
  • Outcome 4 is also covered by quiz Q09 and Q10 and by the lab test table.

Quiz

Outcomes: O1 if chains · O2 choosing if/switch · O3 simplifying · O4 unreachable/overlap.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1The condition inside if (...) must be:O1
  2. 2In an else if chain, Java runs:O1
  3. 3For if (n >= 10), which values are the boundary to test?O1
  4. 4Which fits a switch best?O2
  5. 5A missing break in a classic switch causes:O2
  6. 6In case 6, 7 -> "Weekend" the arrow form:O2
  7. 7if (a) { if (b) { run(); } } equals:O3
  8. 8An early exit helps because it:O3
  9. 9In if (x > 5) {...} else if (x > 10) {...} the second branch is:O4
  10. 10Two branches that can both be true for the same value are:O4

Practical part

Six exercises BEG-012-EX01 to EX06 plus the shipping lab with its test table. Pass: at least five exercises correct and the test table covering all boundaries.

Common mistakes

Missing break in classic switch; gaps or overlaps at boundaries (< vs <=); deep nesting.

Assessment

6 exercises.

Career relevance

Business rules in every service; switch expressions return with sealed types in JNR-001.

Type to search levels, phases, modules and lessons.