⚡0 XP🔥0

Conditionals - if, else and switch

Lesson 4 of 15 ⏱ 4 min read ✎ 2 exercises ❓ 4-question quiz ▶ Runs Java

Decision making with if, if-else, else-if ladders, nested conditions, the classic switch statement and modern switch expressions with arrows.

if and if-else#

if (condition) {
    // runs when condition is true
} else {
    // runs otherwise
}

In Java the condition must be a boolean. if (count) or if (x = 5) will not compile - which prevents a classic C bug.

▶ Try it YourselfJava
Ctrl + Enter to run
Output
You can vote in 2 year(s).

else-if ladder#

▶ Try it YourselfJava
Ctrl + Enter to run
Output
95 -> O (Outstanding)
82 -> A
67 -> B
48 -> C
30 -> F (Fail)

Only the first matching branch runs, so order the conditions carefully (here from highest to lowest).

Nested if and combining conditions#

▶ Try it YourselfJava
Ctrl + Enter to run
Output
Eligible for some companies
हिंदी में समझें

if में condition हमेशा true या false (boolean) होनी चाहिए। कई conditions जोड़ने के लिए && (और) और || (या) का उपयोग करें। Nested if यानी if के अंदर if - जब पहली शर्त पूरी होने के बाद ही दूसरी जाँचनी हो।

The classic switch statement#

switch compares one value against constant case labels. Each case usually ends with break; without it, execution falls through to the next case:

▶ Try it YourselfJava
Ctrl + Enter to run
Output
SAT: lab sessions only

Modern switch expressions (Java 14+)#

The arrow form case X -> ... never falls through, can list several values in one case, and the whole switch can produce a value:

▶ Try it YourselfJava
Ctrl + Enter to run
Output
Result: 144.0
Very good
Tip

Prefer the arrow form in new code. It is shorter, cannot fall through by accident, and the compiler checks that a switch expression covers every case (that is why it needs default for ints and Strings).

The ternary operator#

For a simple two-way choice that produces a value:

String status = (balance >= 0) ? "OK" : "Overdrawn";
int max = (a > b) ? a : b;

Summary#

  • if conditions must be booleans; else-if ladders run only the first true branch.
  • Classic switch works with int, char, String and enum; remember break.
  • Arrow switch expressions (case a, b -> value) never fall through and can return values with yield.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Electricity bill slabs

Read units consumed. Rates: first 100 units Rs 4.5/unit, next 100 Rs 6/unit, above 200 Rs 8/unit. Add a fixed charge of Rs 50. Print Bill: Rs 1360.00 style (2 decimals). Test input 285.

Test input:
285
Expected output:
Bill: Rs 1780.00
2

Month to season with switch expression

Read a month number and print the Indian season using a switch expression with arrows: 3-5 Summer, 6-9 Monsoon, 10-11 Post-monsoon, 12, 1, 2 Winter, anything else Invalid. Print Month 7: Monsoon. Test input 11.

Test input:
11
Expected output:
Month 11: Post-monsoon

Quick Quiz

Q1.

Which types can a classic switch use?

Q2.

What does a switch expression with -> NOT need?

Q3.

if (x = 5) in Java...

Q4.

Which keyword returns a value from a block inside a switch expression?