The compiler always associates an else
with the immediately preceding if
unless told to do otherwise by the placement of braces ({
and }
). This behavior can lead to what’s referred to as the dangling-else problem. For example,
if ( x > 5 )
if ( y > 5 )
cout << "x and y are > 5";
else
cout << "x is <= 5";
appears to indicate that if x
is greater than 5
, the nested if
statement determines whether y
is also greater than 5
. If so, "x and y are > 5"
is output. Otherwise, it appears that if x
is not greater than 5
, the else
part of the if
...else
outputs "x is <= 5"
.
Beware! This nested if
...else
statement does not execute as it appears. The compiler actually interprets the statement as
if ( x > 5 )
if ( y > 5 )
cout << "x and y are > 5";
else
cout << "x is <= 5";
in which the body of the first if
is a nested if
...else
. The outer if
statement tests whether x
is greater than 5
. If so, execution continues by testing whether y
is also greater than 5
. If the second condition is true, the proper string—"x and y are > 5"
—is displayed. However, if the second condition is false, the string "x is <= 5"
is displayed, even though we know that x
is greater than 5
.
To force the nested if
...else
statement to execute as originally intended, we can write it as follows:
if ( x > 5 )
{
if ( y > 5 )
cout << "x and y are > 5";
}
else
cout << "x is <= 5";
The braces ({}
) indicate to the compiler that the second if
statement is in the body of the first if
and that the else
is associated with the first if
.
3.128.170.27