The if Statement
The if statement allows a program to choose between two
alternatives by testing the value of an expression. In its simplest form, the
if statement has the form
if
( expression ) statement
Notice that the parentheses around
the expression are mandatory; they're part of the if statement, not part of the
expression. Also note that the word then doesn't come after the parentheses, as
it would in some programming languages.
When an if
statement is executed, the expression in the parentheses is evaluated; if the
value of the expression is nonzero—which C interprets as true—the statement
after the parentheses is executed. Here's an example:
if (line_num == MAX LINES) line_num
= 0;
The
statement line_num =0; is executed if the condition line_num == MAX LINES is
true (has a nonzero value).
The else Clause
An
if statement may have an else clause:
if
( expression ) statement else statement
The statement that follows the word
else is executed if the expression in parentheses has the value 0.
Here's an example of an if statement with an else clause:
if (i > j)
max = i; else
max = j ;
Notice that both “inner” statements
end with a semicolon.
When an if statement contains an else clause, a layout issue
arises: where should the else be placed? Many C programmers align it with the
if at the beginning of the statement, as in the previous example. The inner
statements are usually indented, but if they're short they can be put on the
same line as the if and else:
if (i > j) max = i; else max = j;
There are
no restrictions on what kind of statements can appear inside an if statement.
In fact, it's not unusual for if statements to be nested inside other if
statements. Consider the following if statement, which finds the largest of the
numbers stored in i. j, and k and stores that value in max:
if (i > j) if (i > k) max = i;
else
max = k;
else
if (j > k) max = j; else
max = k;
if
statements can be nested to any depth. Notice how aligning each else with the
matching if makes the nesting easier to see. If you still find the nesting
confusing, don't hesitate to add braces:
if (i > j)
{ if (i > k) max = i; else
max = k;
}
else
{
if (j > k) max = j;
else
max = k;
}
Adding braces to statements—even when they're not
necessary—is like using parentheses in expressions: both techniques help make a
program more readable while at the same time avoiding the possibility that the
compiler won't understand the program the way we thought it did.
Some
programmers use as many braces as possible inside if statements (and iteration
statements as well). A programmer who adopts this convention would include a
pair of braces for every if clause and every else clause:
if (i > j) { if (i > k) {
max = i; } else { max = k;
}
} else
{
if (j > k)
{
max = j;
} else
{ max = k;
}
}
Using
braces even when they're not required has two advantages. First, the program
becomes easier to modify, because more statements can easily be added to any if
or else clause. Second, it helps avoid errors that can result from forgetting
to use braces when adding statements to an if or else clause.
Cascaded i
f Statements
We'll
often need to test a series of conditions, stopping as soon as one of them is
true. A “cascaded” if statement is often the best way to write such a series of
tests. For example, the following cascaded if statement tests whether n is less
than 0, equal to 0, or greater than 0:
if
(n < 0)
printf(“n is less than 0\n”); else
if
(n == 0)
printf(“n is equal to 0\n”),- else
printf(“'n is greater than 0\n”);
Although
the second if statement is nested inside the first, C programmers don't usually
indent it. Instead, they align each else with the original if:
if
(n < 0)
printf(“n is less than 0\n”); else
if (n == 0)
printf(“n is equal to 0\n”); else
printf(“'n
is greater than 0\n”);
This arrangement gives the cascaded
if a distinctive appearance:
if (
expression )
statement else
if ( expression ) statement
else
if ( expression )
statement else
statement
The
last two lines (else statement) aren't always present, of course. This
way of indenting the cascaded if statement avoids the problem of excessive
indentation when the number of tests is large. Moreover, it assures the reader
that the statement is nothing more than a series of tests.
Keep
in mind that a cascaded if statement isn't some new kind of statement; it's
just an ordinary if statement that happens to have another if statement as its
else clause (and that if statement has another if statement as its else
clause, ad infinitum).
No comments:
Post a Comment