1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>3.2 <TT>if
</TT> Statements
</title>
10 <link href=
"sx3a.html" rev=precedes
>
11 <link href=
"sx3c.html" rel=precedes
>
12 <link href=
"sx3.html" rev=subdocument
>
15 <H2>3.2 <TT>if
</TT> Statements
</H2>
17 <p>[This section corresponds to K
&R Sec.
3.2]
18 </p><p>The simplest way to modify the control flow of a program is
19 with an
<TT>if
</TT> statement,
20 which in its simplest form looks
26 Even if you didn't know any C,
27 it would probably be pretty obvious
28 that what happens here is that
29 if
<TT>x
</TT> is greater than
<TT>max
</TT>,
30 <TT>x
</TT> gets assigned to
<TT>max
</TT>.
31 (We'd use code like this to keep track of the maximum value of
32 <TT>x
</TT> we'd seen--for each new
<TT>x
</TT>,
33 we'd compare it to the old maximum value
<TT>max
</TT>,
34 and if the new value was greater,
35 we'd update
<TT>max
</TT>.)
36 </p><p>More generally, we can say that
37 the syntax of an
<TT>if
</TT> statement is:
39 if(
<I>expression
</I> )
42 where
<I>expression
</I> is any expression and
<I>statement
</I>
45 </p><p>What if you have a series of statements,
46 all of which should be executed together or not at all
47 depending on whether some condition is true?
48 The answer is that you enclose them in braces:
50 if(
<I>expression
</I> )
52 <I>statement
<tt><sub
></tt>1<tt></sub
></tt></I>
53 <I>statement
<tt><sub
></tt>2<tt></sub
></tt></I>
54 <I>statement
<tt><sub
></tt>3<tt></sub
></tt></I>
58 anywhere the syntax of C calls for a statement,
59 you may write a series of statements enclosed by braces.
62 put a semicolon after the closing brace,
64 the series of statements enclosed by braces
68 </p><p>An
<TT>if
</TT> statement may also optionally contain a second
69 statement, the ``
<TT>else
</TT> clause,''
70 which is to be executed if the condition is not met.
76 printf(
"can't compute average\n");
80 The first statement or block of statements is executed
81 if the condition
<em>is
</em> true,
82 and the second statement or block of statements
83 (following the keyword
<TT>else
</TT>)
84 is executed if the condition is
<em>not
</em> true.
86 we can compute a meaningful average only if
<TT>n
</TT> is greater than
0;
88 we print a message saying that we cannot compute the average.
89 The general syntax of an
<TT>if
</TT> statement is therefore
91 if(
<I>expression
</I> )
92 <I>statement
<tt><sub
></tt>1<tt></sub
></tt></I>
94 <I>statement
<tt><sub
></tt>2<tt></sub
></tt></I>
97 <I>statement
<tt><sub
></tt>1<tt></sub
></tt></I> and
<I>statement
<tt><sub
></tt>2<tt></sub
></tt></I>
98 may be lists of statements enclosed in braces).
99 </p><p>It's also possible to nest one
<TT>if
</TT> statement inside another.
100 (For that matter, it's in general possible to nest any kind of
101 statement or control flow construct within another.)
103 here is a little piece of code which decides roughly which
104 quadrant of the compass you're walking into,
105 based on an
<TT>x
</TT> value which is positive if you're
106 walking east, and a
<TT>y
</TT> value which is positive if
107 you're walking north:
112 printf(
"Northeast.\n");
113 else printf(
"Southeast.\n");
117 printf(
"Northwest.\n");
118 else printf(
"Southwest.\n");
121 When you have one
<TT>if
</TT> statement (or loop) nested
123 it's a very good idea to use explicit braces
<TT>{}
</TT>, as
124 shown, to make it clear (both to you and to the compiler)
125 how they're nested and which
<TT>else
</TT> goes with which
127 It's also a good idea to indent the various levels,
128 also as shown, to make the code more readable to humans.
130 You use indentation to make the code visually more readable
131 to yourself and other humans,
132 but the compiler doesn't pay attention to the indentation
133 (since all whitespace is essentially equivalent and is essentially ignored).
134 Therefore, you also have to make sure that the punctuation is right.
135 </p><p>Here is an example of another common arrangement
136 of
<TT>if
</TT> and
<TT>else
</TT>.
137 Suppose we have a variable
<TT>grade
</TT>
138 containing a student's numeric grade,
139 and we want to print out the corresponding letter grade.
140 Here is code that would do the job:
144 else if(grade
>=
80)
146 else if(grade
>=
70)
148 else if(grade
>=
60)
152 What happens here is that exactly one of the five
153 <TT>printf
</TT> calls
155 depending on which of the conditions is true.
156 Each condition is tested in turn, and if one is true,
157 the corresponding statement is executed,
158 and the rest are skipped.
159 If none of the conditions is true,
160 we fall through to the last one, printing ``F''.
161 </p><p>In the cascaded
162 <TT>if
</TT>/
<TT>else
</TT>/
<TT>if
</TT>/
<TT>else
</TT>/... chain,
163 each
<TT>else
</TT> clause is another
<TT>if
</TT> statement.
164 This may be more obvious at first
165 if we reformat the example,
166 including every set of braces
167 and indenting each
<TT>if
</TT> statement relative to the previous one:
195 By examining the code this way,
196 it should be obvious that
197 exactly one of the
<TT>printf
</TT> calls is executed,
198 and that whenever one of the conditions is found true,
199 the remaining conditions do not need to be checked
200 and none of the later statements within the chain will be executed.
201 But once you've convinced yourself of this
202 and learned to recognize the idiom,
203 it's generally preferable to arrange the statements as in the first example,
204 without trying to indent each successive
<TT>if
</TT> statement
205 one tabstop further out.
206 (Obviously, you'd run into the right margin very quickly if the
207 chain had just a few more cases!)
212 <a href=
"sx3a.html" rev=precedes
>prev
</a>
213 <a href=
"sx3c.html" rel=precedes
>next
</a>
214 <a href=
"sx3.html" rev=subdocument
>up
</a>
215 <a href=
"top.html">top
</a>
218 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
219 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
220 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>