The if
statement selects a statement for execution based on the value of a boolean expression.
An else
part is associated with the nearest preceding if
statement that does not already have an else
part. Thus, an if
statement of the form
if (x) if (y) F(); else G();
is equivalent to
if (x) { if (y) { F(); } else { G(); } }
An if
statement is executed as follows:
true
, control is transferred to the first embedded statement. When and if control reaches the end point of that statement, control is transferred to the end point of the if
statement.false
and if an else
part is present, control is transferred to the second embedded statement. When and if control reaches the end point of that statement, control is transferred to the end point of the if
statement.false
and if an else
part is not present, control is transferred to the end point of the if
statement.The first embedded statement of an if
statement is reachable if the if
statement is reachable and the boolean expression does not have the constant value false
.
The second embedded statement of an if
statement, if present, is reachable if the if
statement is reachable and the boolean expression does not have the constant value true
.
The end point of an if
statement is reachable if the end point of at least one of its embedded statements is reachable. In addition, the end point of an if
statement with no else
part is reachable if the if
statement is reachable and the boolean expression does not have the constant value true
.