Lesson 10 / 38

Conditionals

Branch with if / else if / else, the ternary operator, and modern switch expressions that return a value and require no break.

if / else if / else

Conditions must be boolean — Java will not treat 0 or null as false. The ternary cond ? a : b is an expression that yields a value.

int score = 72;
String grade = score >= 60 ? "pass" : "fail";
if (score >= 90) System.out.println("A");
else if (score >= 75) System.out.println("B");
else System.out.println("C");

switch expressions

Modern switch (Java 14+) uses -> arms, needs no break, and can return a value. It also requires you to cover every case (or add default).

String kind = switch (day) {
    case SATURDAY, SUNDAY -> "weekend";
    case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> "weekday";
};