Java if, else and switch: Conditions Explained Simply
Updated on October 22, 2026
With conditions, a Java program makes decisions with if, else and switch: depending on the input, one part or another is run. Without them it runs stubbornly from top to bottom, like the first examples of input and output in Java.
if, else if, else
After if comes a condition in parentheses. If it's true, the block in curly braces runs. Otherwise Java moves on to else if (the next condition) or else (all remaining cases). Java checks from top to bottom and takes only the first matching branch.
To compare, there's == (equal), != (not equal), <, <=, > and >=.
import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int temperature = scanner.nextInt(); if (temperature < 0) { System.out.println("Coat and scarf!"); } else if (temperature < 15) { System.out.println("Take a jacket."); } else { System.out.println("T-shirt weather."); } }}
Input12
With the input 12, temperature < 0 is false and temperature < 15 is true, so the program prints Take a jacket. With 15, both conditions would be false, and the program would end up in else.
Truth values and combining them
A condition is a value of type boolean: true or false. You can store it in a variable and combine it:
a && b: both must be true (and)a || b: at least one must be true (or)!a: flips the value (not)
&& binds more tightly than ||. When in doubt, add parentheses, which also makes it clear to read.
Compare texts with equals, not with ==: command.equals("stop"). The article on strings in Java explains more.
public class Main { public static void main(String[] args) { int hour = 14; boolean weekend = false; boolean holiday = true;
boolean openingHours = hour >= 9 && hour < 18; boolean open = openingHours && !(weekend || holiday); System.out.println("Opening hours: " + openingHours); System.out.println("Library open: " + open);
String command = "stop"; System.out.println(command.equals("stop")); }}switch
If you check one variable against several fixed values, switch is clearer than a long else if chain. Each case goes after case, and after the arrow -> comes what should happen. Separate several values with commas. default covers all remaining values. switch works with whole numbers, with texts and with enums in Java.
A switch can also produce a value (a switch expression). Then it stands to the right of = or after return, ends with a semicolon and needs a case for every possible value, usually via default.
import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String day = scanner.nextLine(); switch (day) { case "Saturday", "Sunday" -> System.out.println("Weekend!"); case "Friday" -> System.out.println("Almost weekend."); default -> System.out.println("Lectures."); }
int month = 4; String season = switch (month) { case 12, 1, 2 -> "winter"; case 3, 4, 5 -> "spring"; case 6, 7, 8 -> "summer"; default -> "autumn"; }; System.out.println("Month " + month + " is in " + season + "."); }}
InputFriday
Common mistakes
= instead of ==
=assigns a value,==compares. The compiler rejectsif (age = 18)withincompatible types: int cannot be converted to boolean.Comparing texts with ==
For texts,
==checks whether it's the same object, not whether the letters are the same. With texts you read in, that's oftenfalseeven though they look the same. Always useequals:if (answer.equals("yes")) { ... }Semicolon after if
if (x > 10);is a condition with an empty statement. The block after it then always runs. The curly brace belongs directly after the closing parenthesis.Wrong order
With
else if, the first matching branch wins. Ifif (points >= 50)comes beforeelse if (points >= 90), the second branch is never reached. Check the narrower condition first.Missing default in a switch expression
If a
switchproduces a value, every possible value must be covered. Withoutdefault, the compiler reportsthe switch expression does not cover all possible input values.
Tasks on this topic
Practise what you learned right in your browser. Tests check your solution instantly.
Related articles
- LoopsJava loops explained simply: for, while, do-while, break and continue with examples. Plus the common mistakes that cost points in exams.Read article
- Input and outputJava input and output explained simply: print values with println and printf, read numbers with Scanner. With examples and common mistakes.Read article
- StringsJava strings explained simply: substring, charAt, split, equals and StringBuilder with examples. Plus common mistakes like comparing with ==.Read article