---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 13 Edition"
java_version: 13
chapter: "Chapter 14. Blocks and Statements"
section: "14.9"
heading: "14.9. The if Statement"
parent: "Chapter 14. Blocks and Statements"
source: https://docs.oracle.com/javase/specs/jls/se13/html/jls-14.html#jls-14.9
id: "14.9"
url: https://jvm.obfuscated.software/jls/se13/14.9.html
raw: https://jvm.obfuscated.software/jls/se13/14.9.md
---

# 14.9. The `if` Statement

The `if` statement allows conditional execution of a statement or a conditional choice of two statements, executing one or the other but not both.

```
IfThenStatement:
  if ( Expression ) Statement

IfThenElseStatement:
  if ( Expression ) StatementNoShortIf else Statement

IfThenElseStatementNoShortIf:
  if ( Expression ) StatementNoShortIf else StatementNoShortIf
```

The *Expression* must have type `boolean` or `Boolean`, or a compile-time error occurs.

## 14.9.1. The `if`-`then` Statement

An `if`-`then` statement is executed by first evaluating the *Expression*. If the result is of type `Boolean`, it is subject to unboxing conversion ([§5.1.8](5.1.8.md)).

If evaluation of the *Expression* or the subsequent unboxing conversion (if any) completes abruptly for some reason, the `if`-`then` statement completes abruptly for the same reason.

Otherwise, execution continues by making a choice based on the resulting value:

- If the value is `true`, then the contained *Statement* is executed; the `if`-`then` statement completes normally if and only if execution of the *Statement* completes normally.
- If the value is `false`, no further action is taken and the `if`-`then` statement completes normally.

## 14.9.2. The `if`-`then`-`else` Statement

An `if`-`then`-`else` statement is executed by first evaluating the *Expression*. If the result is of type `Boolean`, it is subject to unboxing conversion ([§5.1.8](5.1.8.md)).

If evaluation of the *Expression* or the subsequent unboxing conversion (if any) completes abruptly for some reason, then the `if`-`then`-`else` statement completes abruptly for the same reason.

Otherwise, execution continues by making a choice based on the resulting value:

- If the value is `true`, then the first contained *Statement* (the one before the `else` keyword) is executed; the `if`-`then`-`else` statement completes normally if and only if execution of that statement completes normally.
- If the value is `false`, then the second contained *Statement* (the one after the `else` keyword) is executed; the `if`-`then`-`else` statement completes normally if and only if execution of that statement completes normally.
