Although you can rely on the rule that if-false statements belong to the
nearest unmatched if
statement, it is better programming practice to
use braces to avoid potential misreading.
In the following example, the question mark would be
replaced by expensive
, because the braces clearly group the if-false
statement with the second if
statement.
if (ratio > 10) { if (ratio < 30) printf ("It is in the normal range.\n"); else printf ("It is ?.\n"); }
On the other hand, in the following example, it is clear that the question
mark would be replaced by cheap
, because this time the braces clearly
group the if-false statement with the first if
statement.
if (ratio > 10) { if (ratio < 30) printf ("It is in the normal range.\n"); } else printf ("It is ?.\n");