An else statement pairs with the closest if statement that does not already have an else statement unless brackets indicate otherwise.

Brackets can be used to alter the behavior of code. Java uses brackets when determining else statement pairing. Brackets can also be used to make code easier to understand, even when the brackets do not affect the behavior.

Java ignores indentation when determining else statement pairing. Indentation should accurately reflect code behavior to make the code easier to understand.

else statement example 1

int a = 4;
int b = 10;

if(a == 5)
    if(b == 10)
        System.out.println("position 1");
    else
        System.out.println("position 2");

The code segment does not print anything.

The else statement pairs with if(b == 10), which is nested inside if(a == 5). Since a == 5 (4 == 5) evalutes to false, the nested condition b == 10 is not checked.

Example 1 with brackets for readability

int a = 4;
int b = 10;

if(a == 5)
{
    if(b == 10)
        System.out.println("position 1");
    else
        System.out.println("position 2");
}

Brackets can be used to more clearly indicate the intent.

The indentation of both the original example and the updated code with brackets accurately reflects the behavior.

Both code segments behave identically.

else statement example 2

int a = 4;
int b = 10;

if(a == 5)
{
    if(b == 10)
        System.out.println("position 3");
}
else
    System.out.println("position 4");

The code segment prints: position 4

The brackets form a block as the body of the conditional statement if(a == 5). The else statement pairs with if(a == 5). Since a == 5 (4 == 5) evalutes to false, the code inside the else statement body is executed.

else statement example 3 (with poor indentation)

int a = 4;
int b = 10;

if(a == 5)
if(b == 10)
System.out.println("position 5");
else
System.out.println("position 6");
else
System.out.println("position 7");

The code segment prints: position 7

The first else statement pairs with if(b == 10) because it is the closest if statement that does not have an else and there are no brackets to indicate otherwise.

The second else statement pairs with if(a == 5).

Since a == 5 (4 == 5) evalutes to false, the code inside the second else statement body is executed.

Example 3 with accurate indentation

int a = 4;
int b = 10;

if(a == 5)
    if(b == 10)
        System.out.println("position 5");
    else
        System.out.println("position 6");
else
    System.out.println("position 7");

Although Java ignores indentation, accurate indentation makes this code considerably easier to understand.

Example 3 with brackets for readability

int a = 4;
int b = 10;

if(a == 5)
{
    if(b == 10)
        System.out.println("position 5");
    else
        System.out.println("position 6");
}
else
    System.out.println("position 7");

The use of brackets clearly indicates that the body of if(a == 5) is an entire block of code.

The original (poorly indented) example and both modifications all behave identically.

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on else statement pairing