W3docs

Java if, else, and else if

Branch program flow in Java with if, else if, and else statements, and learn how to nest conditions cleanly.

A program that always runs the same sequence of statements isn't very useful. if is how Java asks a question and chooses what to do next. Together with else if and else, it forms the backbone of every decision your code will ever make.

The basic if

if (condition) {
  // runs only when condition is true
}

The condition must be a boolean expression — no truthy/falsy values like in JavaScript or Python. if (1) does not compile; if (count > 0) does.

int age = 20;

if (age >= 18) {
  System.out.println("You can vote.");
}

Adding an else

else provides the alternative branch — what runs when the condition is false:

int age = 16;

if (age >= 18) {
  System.out.println("You can vote.");
} else {
  System.out.println("Too young to vote.");
}

Exactly one of the two branches runs. There is no fall-through.

Chaining with else if

For more than two outcomes, chain else if. Java checks each condition top to bottom and runs the first branch whose condition is true; the rest are skipped:

int score = 72;

if (score >= 90) {
  System.out.println("A");
} else if (score >= 80) {
  System.out.println("B");
} else if (score >= 70) {
  System.out.println("C");
} else {
  System.out.println("F");
}

Order matters. If you put the loosest condition first, it swallows every later case. The example above prints C because score >= 70 is the first match — even though score >= 60 would also be true if it were checked.

Braces are optional — but use them

Java lets you drop the braces when a branch is a single statement:

if (loggedIn)
  showDashboard();
else
  showLogin();

Don't. Always use braces. The two lines below look the same but behave differently:

if (loggedIn)
  showDashboard();
  logActivity();        // ALWAYS runs — not part of the if!

if (loggedIn) {
  showDashboard();
  logActivity();        // both inside the if
}

The cost of {} is two characters; the cost of the bug is your weekend.

Boolean expressions inside if

The condition can be anything that evaluates to boolean:

if (x > 0 && x < 100) { ... }
if (name.equals("admin")) { ... }
if (!list.isEmpty()) { ... }
if (user != null && user.isActive()) { ... }

Note that if is a statement, not an expression: it performs an action but does not itself produce a value, so you cannot write int x = if (...). For the value-producing version, reach for the Ternary Operator.

That last pattern — null check first, then a method call — uses short-circuit evaluation. If user is null, user.isActive() is never invoked. See Logical Operators for &&, ||, and !, and Comparison Operators for >, ==, and friends.

Nesting if statements

You can put an if inside another if. Sometimes it's the clearest way to express a condition:

if (user != null) {
  if (user.isAdmin()) {
    System.out.println("Welcome, admin.");
  } else {
    System.out.println("Welcome, user.");
  }
} else {
  System.out.println("Please log in.");
}

But deep nesting reads badly. Early returns usually clean it up:

if (user == null) {
  System.out.println("Please log in.");
  return;
}
if (user.isAdmin()) {
  System.out.println("Welcome, admin.");
  return;
}
System.out.println("Welcome, user.");

Less indentation, easier to follow.

Common mistake: = instead of ==

Beginners coming from other languages sometimes write:

if (x = 5) { ... }   // compile error

= is assignment, == is comparison. Unlike C, Java rejects this at compile time unless the variable is a boolean — because the result of int x = 5 is an int, not a boolean. That's a small mercy Java's type system gives you for free.

A worked example

The else if chain below converts numeric scores into letter grades. Run it and confirm the output:

java— editable, runs on the server

It prints:

95 -> A
82 -> B
71 -> C
68 -> D
45 -> F

When to use switch instead

A long else if chain that tests one variable against a list of fixed values is often clearer as a switch statement. Use if/else if when branches test different expressions or use ranges (like the grade example above); use switch when you compare a single value to several constants.

What's next

For one-line conditional assignments, the Ternary Operator is often more readable than a full if/else.

Practice

Practice
What is printed by this code when score is 85?
What is printed by this code when score is 85?
Was this page helpful?