Codectionary / Developer documentation / Java

if / else if / else

The if statement executes a block of code only when its condition evaluates to true. Java lets you chain additional conditions with else if, and provide a fallback with else for when none of the preceding conditions match. Unlike some languages, Java requires the condition to be an actual boolean expression - it cannot use a number or object directly as a truthy/falsy value.

Syntax

if (condition) {\n  // code\n} else if (condition2) {\n  // code\n} else {\n  // code\n}

Examples

Basic if / else

Choosing between two branches based on a condition.

int age = 20;

if (age >= 18) {
    System.out.println("You are an adult");
} else {
    System.out.println("You are a minor");
}

Chaining with else if

Testing multiple conditions in sequence.

int score = 75;

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

Nested Conditionals

Placing an if statement inside another to check compound conditions.

int age = 25;
boolean hasLicense = true;

if (age >= 18) {
    if (hasLicense) {
        System.out.println("You can drive");
    } else {
        System.out.println("You need a license first");
    }
} else {
    System.out.println("You are too young to drive");
}

Best practices

  • Always use curly braces {} even for single-statement blocks - it prevents subtle bugs when code is later added to a branch
  • Order else if conditions from most specific to least specific when ranges overlap, since Java checks them top to bottom
  • Flatten deeply nested if statements where possible by combining conditions with && or using early returns
  • Consider a switch statement instead of a long else if chain when comparing a single variable against many discrete values

At a glance

Purpose
General-purpose application development
File extension
.java
Runs in
Java Virtual Machine
Usually used with
JDK and Java libraries

Specifications & further reading

Related Java documentation

Variables & Primitive Data Types
Java is a statically-typed language, meaning every variable must be declared with an explicit type before use, and that type cannot change afterward. Java has eight primitive types - byte, short, int, long, float, double, char, and boolean - which store simple values directly rather than references, making them fast and memory-efficient. Anything beyond primitives (String, arrays, custom classes) is a reference type instead.
System.out.println / print / printf
System.out is Java's standard output stream, and it exposes three main methods for writing text to the console. println() prints its argument followed by a newline, print() prints without adding a newline, and printf() gives C-style formatted output using format specifiers like %d, %s, and %.2f for precise control over how values are displayed.
Comments & Javadoc
Java supports single-line comments with //, multi-line comments with /* */, and a special documentation format called Javadoc, written as /** */. Javadoc comments support tags like @param, @return, and @throws, and can be processed by the javadoc tool to automatically generate HTML API documentation - the same format used for Java's own official standard library docs.
Arithmetic & Assignment Operators
Java provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder (modulus). It also has compound assignment operators (+=, -=, etc.) that combine an operation with assignment, along with increment (++) and decrement (--) operators in both prefix and postfix forms, which subtly differ in when the value is updated relative to being used.