Java Tutorial: Operators & Control Flow
Comparison operators, logical operators, if/else, switch expressions, and the ternary operator.
Make decisions
Every program needs to choose. `if/else` is the classic. Java's modern `switch` expressions (Java 14+) bring a cleaner syntax. And `instanceof` with pattern matching makes type checks elegant.
What you'll learn in this Java operators & control flow tutorial
This interactive Java tutorial has 7 hands-on exercises. Estimated time: 18 minutes.
- if / else if / else — Java's `if/else` should feel familiar. One rule: use braces `{}` always — missing braces are a historic source of critic…
- Logical operators: &&, ||, ! — Combine boolean conditions:
- Ternary — one-line if/else — The ternary operator returns a value based on a condition:
- switch — classic and modern — Classic switch matches exact values. Modern `switch` expression (Java 14+) uses `->` arrows and returns values — no `bre…
- switch with yield — For multi-statement switch cases, use `yield` to return a value:
- instanceof and pattern matching — Classic `instanceof` checks type. Java 16+ adds pattern matching — it assigns AND checks in one step:
- Build a calculator — Final challenge — no starter code.
Java Operators & Control Flow concepts covered
- Make decisions