Conditionals & Switch in Java

From the Java cheat sheet · Control Flow · verified Jul 2026

Conditionals & Switch

if/else and switch expressions with pattern matching.

java
// Quick Reference
if (x > 0) { ... } else if (x < 0) { ... } else { ... }

String r = switch (day) {
    case 6, 7 -> "weekend";
    default   -> "weekday";
};
💡 Arrow-form switch has no fall-through and needs no break statements.
⚡ switch can be an expression that returns a value - use yield inside a block.
📌 Pattern matching (case Type t) plus when guards replaces long if-else chains.
🟢 A pattern switch can handle null directly with a case null branch.
conditionalsswitch

More Java tasks

Back to the full Java cheat sheet