The statement inside of an if
or else
option can be another if
-else
statement. Placing an if
-else
inside another is known as nested if
-else
constructions. For example:
if (expression1)
if (expression2)
statement1;
else
statement2;
else
statement3;
The else
option will be paired with the nearest unpaired if
. statement2
is the alternative action of the inner if
, while statement3
is the alternative action of the outer if
.
The above example has three possible different outcomes as shown in the following chart:
expression1 expression2 statement executed
true true statement1
true false statement2
false true statement3
false false statement3
Caution must be shown when using else
statements inside of nested if
-else
structures. For example:
if (expression1)
if (expression2)
statement1;
else
statement2;
Indentation is ignored by the compiler, hence it will pair the else
statement with the inner if
. If you want the else
to get paired with the outer if
as the indentation indicates, you need to add braces:
if (expression1)
{
if (expression2)
statement1;
}
else
statement2;
The braces allow the else statement to be paired with the outer if
.
Another alternative to the example in Section 4. makes use of the &&
operator. A pair of nested if
statements can be coded as a single compound &&
statement.
if (expression1 && expression2)
statement1;
else
statement2;
The most common and effective use of nested if
-else
statements is called an if
-else
chain. See the following formatting styles:
| Formatting style 1 | Formatting style 2 |
|
if (expression1)
statement1;
else
if (expression2)
statement2;
else
if (expression3)
statement3;
else
statement4;
|
if (expression1)
statement1;
else if (expression2)
statement2;
else if (expression3)
statement3;
else
statement4;
|
Notice that each successive if
-else
statement is buried deeper in the overall structure. statement4
will only be executed if the first three expressions are false
.
Formatting style 1 lines up the if
with its counterpart else
keyword. This makes it easy to check syntax but the indentation can get rather deep. Formatting style 2 is a more compact version but you need to be careful about which else
statement belongs to which if
. Formatting style 1 is more appropriate if
the statements are compound statements. Formatting style 2 is appropriate if
the statements are single-line statements.
The advantage of such an if
-else
chain is efficiency in execution. If a true
value is encountered at any level, that statement is executed and the rest of the structure is ignored.
Consider the following example of determining the type of triangle given the three sides A
, B
, and C
.
if ( (A == B) && (B == C) )
System.out.println("Equilateral triangle");
else if ( (A == B) || (B == C) || (A == C) )
System.out.println("Isosceles triangle");
else
System.out.println("Scalene triangle");
If an equilateral triangle is encountered, the rest of the code is ignored. Such a chain is best constructed by placing the most demanding case at the top and the least demanding case at the bottom.