Lesson 46 min read

Conditionals

Teaching your code to make decisions

If/Else — The Fork in the Road

Imagine you're walking and the path splits. You check: "Is it raining?" If yes, you take the covered path. If no, you take the scenic route. That's exactly what if/else does in Java.

The condition inside the parentheses must be a boolean expression — something that evaluates to true or false. Branching like this is at the heart of binary search and many other algorithms. Java checks it, picks one path, and ignores the other.

You can also chain decisions with else if when you have more than two options — like choosing between walking, biking, or taking the bus based on the distance.

If / Else If / Else

public class Main {
public static void main(String[] args) {
int temperature = 35;
if (temperature >= 40) {
System.out.println("Stay inside! It's dangerously hot.");
} else if (temperature >= 30) {
System.out.println("It's hot — grab some sunscreen.");
} else if (temperature >= 20) {
System.out.println("Nice weather! Perfect for a walk.");
} else if (temperature >= 10) {
System.out.println("A bit chilly. Wear a jacket.");
} else {
System.out.println("Brrr! Bundle up.");
}
// Nested if
int age = 15;
boolean hasTicket = true;
if (age >= 12) {
if (hasTicket) {
System.out.println("Welcome to the movie!");
} else {
System.out.println("You need a ticket first.");
}
} else {
System.out.println("Sorry, you must be 12 or older.");
}
}
}
Output
It's hot — grab some sunscreen.
Welcome to the movie!

Switch — The Vending Machine

A switch statement is like a vending machine. You put in a value, and it checks which button matches. It's cleaner than writing a dozen else if blocks when you're comparing one variable against many exact values.

Important rules:

  • Each case needs a break; at the end, or Java will "fall through" and run the next case too.
  • The default case runs when nothing else matches — like the "invalid selection" message on a vending machine.
  • Switch works with int, char, String, and enums (but not double or boolean).

Switch Statement

public class Main {
public static void main(String[] args) {
String day = "Wednesday";
switch (day) {
case "Monday":
System.out.println("Start of the work week.");
break;
case "Wednesday":
System.out.println("Halfway there! Hump day.");
break;
case "Friday":
System.out.println("TGIF! Weekend is near.");
break;
case "Saturday":
case "Sunday":
System.out.println("Weekend vibes!");
break;
default:
System.out.println("Just another day.");
break;
}
// Ternary — the one-liner if/else
int score = 72;
String grade = score >= 90 ? "A" :
score >= 80 ? "B" :
score >= 70 ? "C" : "F";
System.out.println("Grade: " + grade);
}
}
Output
Halfway there! Hump day.
Grade: C

Comparing Strings in Conditions

public class Main {
public static void main(String[] args) {
String password = "secret123";
String input = new String("secret123");
// WRONG way — might not work!
if (input == password) {
System.out.println("== says: Match!");
} else {
System.out.println("== says: No match!");
}
// RIGHT way — always works
if (input.equals(password)) {
System.out.println(".equals() says: Match!");
}
// Case-insensitive comparison
String userInput = "ADMIN";
if (userInput.equalsIgnoreCase("admin")) {
System.out.println("Welcome, admin!");
}
}
}
Output
== says: No match!
.equals() says: Match!
Welcome, admin!
Note: Forgot a break in your switch? Java will "fall through" and execute every case below the matching one. This is occasionally useful on purpose (like grouping Saturday and Sunday), but usually it's a bug. When in doubt, always add break.

Quick check

What happens if you forget 'break' in a switch case?
StringsLoops