Making Decisions with if
Programs need to react to data: pass or fail, in stock or sold out, valid or not. The if statement is how Java chooses which code to run based on a condition.
An if takes a boolean condition in parentheses and runs the block in braces only when that condition is true:
The condition score >= 60 is true, so the message prints. The line after the closing brace always runs - it is outside the if. Change score to 40 and the "passed" line is skipped entirely.
Unlike C, Java will not let you put a number where a boolean belongs. if (score) is a compile error - the condition must be a real boolean expression built from comparisons (>, >=, ==, !=) or boolean logic (&&, ||, !).
Adding an else
else gives you the "otherwise" path - code that runs only when the condition is false:
Exactly one of the two blocks runs - never both, never neither. else has no condition of its own; it simply catches everything the if did not.
Chaining with else if
When there are more than two outcomes, chain conditions with else if. Java checks them top to bottom and runs the first block whose condition is true, then skips the rest:
Order matters. Because 84 is checked against >= 90 first (false), then >= 80 (true), it stops at B and never even tests the lower bounds. That is also why you do not need to write score >= 80 && score < 90 - reaching the second branch already guarantees score was below 90. Put your narrowest or highest-priority condition first.
The trailing else is your catch-all. Leaving it off here would be a compile error, since grade might be unassigned by the time you print it.
Comparing Values: == vs equals()
The most common if-statement bug in Java is comparing strings with ==. For primitives (int, double, char, boolean) == is correct. For objects, == asks "is this the same object in memory?" while .equals() asks "do these have the same contents?" - and you almost always want the latter:
Only the second message prints. The new String("yes") is a different object than the literal "yes", so == is false, but the text is identical, so .equals() is true. Rule of thumb: primitives use ==, objects use .equals().
One safe trick to avoid a NullPointerException when the variable might be null: put the literal first - if ("yes".equals(input)) - since a literal is never null.
Combining and Nesting Conditions
You can combine conditions with && (and), || (or), and ! (not), or nest one if inside another when a check only makes sense after a prior one passed:
&& and || short-circuit: in a && b, if a is false Java never evaluates b. That is handy for guarding against errors - if (s != null && s.length() > 0) is safe because the length check is skipped when s is null. Prefer combining with && over deep nesting when you can; flat reads more clearly than a pyramid of braces.
A classic gotcha: a single = is assignment, == is comparison. Writing if (x = 5) is a compile error for int (good), but if (flag = true) on a boolean compiles and silently assigns - watch for it.
The Ternary Operator
When all you want is to pick between two values, the ternary operator ?: is a compact one-liner. Read condition ? a : b as "if condition then a else b":
The ternary produces a value you can assign or pass directly. Keep it for simple selection - if either branch needs multiple statements or real logic, a full if-else stays readable where a nested ternary quickly becomes a puzzle.
A Gotcha: the Optional Braces Trap
Java lets you skip the braces when a branch has a single statement, but this invites a subtle bug. The indentation lies:
if (loggedIn)
System.out.println("Welcome");
System.out.println("Loading dashboard"); // NOT part of the if!
Only the first line is controlled by the if; the second always runs regardless, despite the indentation suggesting otherwise. Always use braces - even for one line - and this whole class of mistake disappears.
Next: switch
if-else if chains are perfect when each branch tests a different condition. But when you are checking one value against many fixed possibilities - a day name, a menu choice, an enum - a long else if ladder gets repetitive. The switch statement is built for exactly that, and that is the next page.
Frequently Asked Questions
How do you write an if-else statement in Java?
Put a boolean condition in parentheses after if, then the code to run in braces, and an optional else block for when the condition is false: if (score >= 60) { System.out.println("Pass"); } else { System.out.println("Fail"); }. The condition must evaluate to a boolean - Java will not accept an int like C does.
What is the difference between == and equals() in a Java if statement?
== compares whether two references point to the same object, while .equals() compares the contents. For primitives like int use ==. For objects - especially String - use .equals(): if (name.equals("Sam")), not if (name == "Sam"). Using == on strings is the single most common if-statement bug in Java.
What is the ternary operator in Java?
The ternary operator condition ? valueIfTrue : valueIfFalse is a compact if-else that produces a value. For example String label = age >= 18 ? "adult" : "minor"; assigns one of two values based on the condition. Use it for simple value selection; reach for a full if-else when there is logic to run in each branch.