skip to content

What is the if statement in Java and how does the optional else clause work?

level: juniorimportance: must knowfreq 70%

answer

  1. condition must be boolean — no truthy ints
  2. else is optional; exactly one branch runs
  3. branch = one statement; use a block { } for many
  4. always use braces to avoid the silent-extra-line bug

basics

~10 s

An if statement runs a block of code only when its condition is true. An optional else block runs when the condition is false. The condition must be a boolean.

solid answer

~40 s

The if statement is Java's primary conditional: `if (condition) statement` runs the statement only when the boolean condition evaluates to true. Adding `else statement2` provides an alternative branch that runs when the condition is false; exactly one of the two branches executes. The condition must be of type boolean (or Boolean, which is auto-unboxed) — unlike C, Java does not treat integers as truthy, so `if (x)` where x is an int does not compile. Each branch can be a single statement or a block in braces `{ ... }`. Best practice is to always use braces even for single statements, because it prevents bugs when code is later added and makes the scope of each branch unambiguous.

code

java · 7 lines
java
int score = 72;
if (score >= 60) {
    System.out.println("Pass");
} else {
    System.out.println("Fail");
}
// Prints: Pass

go deeper

for a junior

Can write a correct if/else, knows the condition must be boolean, and can read simple branching code.

for a middle

Habitually uses braces, knows the auto-unboxing NPE risk, and avoids the = vs == trap; understands exactly one branch runs.

for a senior

Articulates why Java forbids truthy values as a deliberate safety choice and enforces brace style in reviews to prevent the dangling-statement bug.

for a principal

Frames conditionals within readability/maintainability standards, sets team conventions (braces always, guard clauses), and weighs branching against polymorphism for complex decision logic.

## What a conditional statement is Programs often need to do different things depending on data. A **conditional statement** is a control-flow construct that chooses whether (or which) code to run based on a test. **Control flow** simply means the order in which statements execute; normally it is top-to-bottom, and conditionals let you skip or pick branches. ## The `if` statement The basic form is: ```java if (condition) statement; ``` - **condition** is an expression that must evaluate to a `boolean` (the primitive type with exactly two values: `true` and `false`). A `Boolean` object also works because Java *auto-unboxes* it (automatically converts the wrapper object to the primitive). If the `Boolean` is `null`, unboxing throws `NullPointerException` at runtime. - **statement** is the *then-branch* — it runs only when the condition is `true`. If the condition is `false`, the statement is skipped and execution continues after the `if`. Unlike C or JavaScript, Java has **no concept of "truthy"/"falsy"** values. You cannot write `if (someInt)` or `if (someObject)`; the condition must literally be boolean. `if (x = 5)` (assignment, not comparison) is also a compile error unless x is boolean — a common bug that Java's type system catches for you. ## The `else` clause You can attach an alternative branch: ```java if (condition) statement1; // runs when condition is true else statement2; // runs when condition is false ``` Exactly **one** of the two statements executes. The `else` is optional; without it, a false condition simply does nothing. ## Single statements vs. blocks A branch is *one* statement. To run several statements, wrap them in a **block** — curly braces grouping statements into one compound statement: ```java if (condition) { doA(); doB(); } ``` Without braces, only the single next statement belongs to the `if`. This causes the classic bug: ```java if (loggedIn) grantAccess(); auditLog(); // NOT part of the if — always runs! ``` The indentation lies; `auditLog()` runs unconditionally. **Always using braces** prevents this entire class of mistakes, which is why most style guides (and Java's own conventions) mandate them. ## How a junior derives the answer Think: "if condition then do X, otherwise do Y." The condition is a yes/no (boolean) question; the `then` runs on yes, the `else` on no, and braces let each side do more than one thing.

  • Why does Java require a boolean condition instead of allowing any value like C?
    To eliminate a whole class of bugs: `if (x = 5)` or `if (count)` are usually mistakes. Forcing a boolean means the compiler rejects accidental assignments and unintended truthiness, making intent explicit.
  • What happens if the condition is a Boolean wrapper that is null?
    Java auto-unboxes it to a primitive boolean, and unboxing null throws a NullPointerException at runtime.

saying these in an interview costs you the question

  • Claiming `if (x)` works for an int or object reference (it does not compile in Java)
  • Assuming the second indented line belongs to the if without braces
  • Confusing `=` (assignment) with `==` (comparison) in the condition

context