Core Java · Chapter 5 of 48

Conditionals and switch

By the end of this chapter you can make code choose: with if, with && and ||, with the ternary operator, and with a switch expression. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: First steps
  • 6 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Core Java are free. This one comes with the all labs plan.

Why you need this

Meera has new rules for the counters. Big bills get a discount, and loyalty members get a little more. Staff and VIP tickets jump the queue. The price of each item must come from one place, and the platform screen must say something useful when a train arrives, leaves or is late.

Every method so far runs the same lines for every input. A discount that depends on the bill, a ticket that may be missing, a message that depends on a code: each needs the program to pick one path, without crashing on an input nobody expected.

What you build: 6 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Rules.java: the discount for a bill

    In Rules.java, make discountPercent(total, member) return the discount in percent, as a whole number. A bill of 500 or more gets 15, member or not. A bill from 200 to 499 gets 10 for a member and 5 for anyone else. A bill under 200 gets 5 for a member and 0 for anyone else.

    For example, ₹600 not a member returns 15, ₹500 a member returns 15, ₹250 a member returns 10, ₹200 not a member returns 5, ₹199 a member returns 5 and ₹40 not a member returns 0.

  2. Task 2

    Rules.java: spot a priority ticket safely

    In Rules.java, make isPriority(ticket) return true when the ticket starts with STAFF or with VIP, in capitals, and false otherwise. An empty ticket is not priority. A missing ticket, null, is not priority either, and must not throw a NullPointerException.

    For example, isPriority("VIP-0007") and isPriority("STAFF-12") return true; isPriority("SBC-2026-0042"), isPriority("") and isPriority(null) return false.

  3. Task 3

    Rules.java: one cup or many cups

    In Rules.java, make cupLabel(cups) return the number, a space, and the word cup when there is exactly 1, or cups for any other number, 0 included. Use the ternary operator for the choice.

    For example, cupLabel(1) returns 1 cup, cupLabel(3) returns 3 cups and cupLabel(0) returns 0 cups.

  4. Task 4

    Rules.java: look up a price with a switch

    In Rules.java, make price(item) return the price in rupees with a switch expression: masala chai and adrak chai 40 (one arm with both labels), cutting chai 25, bun maska 50, Parle-G 10. The names match exactly as written here, capitals included. Any other item throws an IllegalArgumentException.

    For example, price("adrak chai") returns 40 and price("Parle-G") returns 10; price("filter coffee") throws an IllegalArgumentException.

  5. Task 5

    Rules.java: turn a platform code into a message

    In Rules.java, make trainAlert(code, minutes) return the platform message. ARR gives Train arriving in N min. DEP gives Train leaving in N min. DLY under 60 minutes gives Train delayed by N min, and from 60 minutes gives Train delayed by H h, where H is the whole hours, rounded down. Any other code gives Check the board. N is the minutes it was given.

    For example, ("ARR", 5) gives Train arriving in 5 min, ("DLY", 45) gives Train delayed by 45 min, ("DLY", 60) gives Train delayed by 1 h, ("DLY", 150) gives Train delayed by 2 h and ("XYZ", 0) gives Check the board.

  6. Task 6 · interview question

    Interview: Rules.java, predict what an old switch prints

    In Rules.java, make predictFallThrough() return the lines the code in its comment prints, one string per line, in order, written exactly as they appear in the code.

    The check runs the same code and compares your list with what Java really printed.

What you will learn

  • if, else if and else: the first true condition wins.
  • &&, || and !: combining conditions, and how short-circuiting keeps you safe from null.
  • The ternary operator: a choice between two values, in one expression.
  • Switch expressions: a value picked by matching, with shared labels, default and yield; and the old switch that falls through.

More in First steps

Build "Conditionals and switch" yourself

Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Core Java are free. This one comes with the all labs plan.