Language syntax

Java if else statement

Select one of two branches when the condition evaluates normally. Use braces for clear nesting.

Formal Syntax & Structure

java syntaxjava
if (condition) { first } else { second }

Working Example

Example Snippetjava
public class Main {    public static void main(String[] args) {        boolean open = false;        if (open) { System.out.println("Open"); } else { System.out.println("Closed"); }    }}
Java Reference