* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / c.programming.notes / sx3b.html
blobd21224b0e1b4885a2a2119539ccf1f755932fd2f
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. -->
5 <html>
6 <head>
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>
13 </head>
14 <body>
15 <H2>3.2 <TT>if</TT> Statements</H2>
17 <p>[This section corresponds to K&amp;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
21 like this:
22 <pre>
23 if(x &gt; max)
24 max = x;
25 </pre>
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:
38 <pre>
39 if( <I>expression</I> )
40 <I>statement</I>
41 </pre>
42 where <I>expression</I> is any expression and <I>statement</I>
43 is any statement.
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:
49 <pre>
50 if( <I>expression</I> )
52 <I>statement<tt>&lt;sub&gt;</tt>1<tt>&lt;/sub&gt;</tt></I>
53 <I>statement<tt>&lt;sub&gt;</tt>2<tt>&lt;/sub&gt;</tt></I>
54 <I>statement<tt>&lt;sub&gt;</tt>3<tt>&lt;/sub&gt;</tt></I>
56 </pre>
57 As a general rule,
58 anywhere the syntax of C calls for a statement,
59 you may write a series of statements enclosed by braces.
60 (You do not need to,
61 and should not,
62 put a semicolon after the closing brace,
63 because
64 the series of statements enclosed by braces
65 is not itself a
66 simple expression
67 statement.)
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.
71 Here is an example:
72 <pre>
73 if(n &gt; 0)
74 average = sum / n;
75 else {
76 printf("can't compute average\n");
77 average = 0;
79 </pre>
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.
85 In this example,
86 we can compute a meaningful average only if <TT>n</TT> is greater than 0;
87 otherwise,
88 we print a message saying that we cannot compute the average.
89 The general syntax of an <TT>if</TT> statement is therefore
90 <pre>
91 if( <I>expression</I> )
92 <I>statement<tt>&lt;sub&gt;</tt>1<tt>&lt;/sub&gt;</tt></I>
93 else
94 <I>statement<tt>&lt;sub&gt;</tt>2<tt>&lt;/sub&gt;</tt></I>
95 </pre>
96 (where both
97 <I>statement<tt>&lt;sub&gt;</tt>1<tt>&lt;/sub&gt;</tt></I> and <I>statement<tt>&lt;sub&gt;</tt>2<tt>&lt;/sub&gt;</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.)
102 For example,
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:
108 <pre>
109 if(x &gt; 0)
111 if(y &gt; 0)
112 printf("Northeast.\n");
113 else printf("Southeast.\n");
115 else {
116 if(y &gt; 0)
117 printf("Northwest.\n");
118 else printf("Southwest.\n");
120 </pre>
121 When you have one <TT>if</TT> statement (or loop) nested
122 inside another,
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
126 <TT>if</TT>.
127 It's also a good idea to indent the various levels,
128 also as shown, to make the code more readable to humans.
129 Why do both?
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:
141 <pre>
142 if(grade &gt;= 90)
143 printf("A");
144 else if(grade &gt;= 80)
145 printf("B");
146 else if(grade &gt;= 70)
147 printf("C");
148 else if(grade &gt;= 60)
149 printf("D");
150 else printf("F");
151 </pre>
152 What happens here is that exactly one of the five
153 <TT>printf</TT> calls
154 is executed,
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:
168 <pre>
169 if(grade &gt;= 90)
171 printf("A");
173 else {
174 if(grade &gt;= 80)
176 printf("B");
178 else {
179 if(grade &gt;= 70)
181 printf("C");
183 else {
184 if(grade &gt;= 60)
186 printf("D");
188 else {
189 printf("F");
194 </pre>
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!)
209 </p><hr>
211 Read sequentially:
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>
216 </p>
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>
221 </p>
222 </body>
223 </html>