* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes.int / sx4ac.html
blob345b7c9d72fcc3e0935d6e5e19e1893f103c4060
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 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>18.3.1: <TT>switch</TT></title>
10 <link href="sx4c.html" rev=precedes>
11 <link href="sx4bc.html" rel=precedes>
12 <link href="sx4c.html" rev=subdocument>
13 </head>
14 <body>
15 <H3>18.3.1: <TT>switch</TT></H3>
17 <p>[This section corresponds to K&amp;R Sec. 3.4]
18 </p><p>A frequent sort of pattern is exemplified by
19 the sequence
20 <pre>
21 if(x == e1)
22 /* some code */
23 else if(x == e2)
24 /* other code */
25 else if(x == e3)
26 /* some more code */
27 else if(x == e4)
28 /* yet more code */
29 else
30 /* default code */
31 </pre>
32 Depending on the value of <TT>x</TT>,
33 we have one of several chunks of code to execute,
34 which we select with a long
35 <TT>if</TT>/<TT>else</TT>/<TT>if</TT>/<TT>else</TT>... chain.
36 When the value we're selecting on is an integer,
37 and when the values we're selecting among are all constant,
38 we can use a <TT>switch</TT> statement, instead.
39 The <TT>switch</TT> statement evaluates an expression
40 and matches the result against a series of
41 ``<TT>case</TT> labels''.
42 The code
43 beginning
44 with the matching <TT>case</TT> label (if any) is executed.
45 A <TT>switch</TT> statement can also have a <TT>default</TT> case
46 which is executed if none of the explicit <TT>case</TT>s match.
47 </p><p>A <TT>switch</TT> statement looks like this:
48 <pre>
49 switch( <I>expr</I> )
51 case <I>c1</I> :
52 <I>... code ...</I>
53 break;
54 case <I>c2</I> :
55 <I>... code ...</I>
56 break;
57 case <I>c3</I> :
58 <I>... code ...</I>
59 break;
60 ...
61 default:
62 <I>... code ...</I>
63 break;
65 </pre>
66 The expression <I>expr</I>
67 is evaluated.
68 If one of the <TT>case</TT> labels
69 (<I>c1</I>, <I>c2</I>, <I>c3</I>, etc.,
70 which must all be integral constants)
71 matches,
72 execution jumps to there,
73 and continues
74 until
75 the
76 next
77 <TT>break</TT> statement.
78 Otherwise,
79 if there is a <TT>default</TT> label,
80 execution jumps to there
81 (and continues to the
82 next
83 <TT>break</TT> statement).
84 Otherwise,
85 none of the code in the <TT>switch</TT> statement is executed.
86 (Yes, the <TT>break</TT> statement is also used to break out of loops.
87 It breaks out of the nearest enclosing loop
88 or <TT>switch</TT> statement it finds itself in.)
89 </p><p>The <TT>switch</TT> statement only works on
90 integral arguments and expressions
91 (<TT>char</TT>, the various sizes of <TT>int</TT>,
92 and <TT>enum</TT>s,
93 though we haven't met <TT>enum</TT>s yet).
94 There is no direct way to switch on strings,
95 or on floating-point values.
96 The target <TT>case</TT> labels must be specified explicitly;
97 there is no general way to
98 specify a case which
99 corresponds to
100 a range of values.
101 </p><p>One peculiarity of the <TT>switch</TT> statement
102 is that the <TT>break</TT>
103 at the end of one case's block of code
105 optional.
106 If you leave it out, control will ``fall through''
107 from one case to the next.
108 Occasionally, this is what you want, but usually not,
109 so remember to put a <TT>break</TT> statement after most cases.
110 (Since falling through is so rare,
111 many programmers highlight it,
112 when they do
113 mean to
114 use it,
115 with a comment like <TT>/* FALL THROUGH */</TT>,
116 to indicate that it's not a mistake.)
117 One way to make use of ``fallthrough''
118 is when you have a small set or range of cases
119 which should all map to the same code.
120 Since the <TT>case</TT> labels are just labels,
121 and since there doesn't have to be a statement
122 immediately following a <TT>case</TT> label,
123 you can associate several <TT>case</TT> labels with one block of
124 code:
125 <pre>
126 switch(x)
128 case 1:
129 <I>... code ...</I>
130 break;
131 case 2:
132 <I>... code ...</I>
133 break;
134 case 3:
135 case 4:
136 case 5:
137 <I>... code ...</I>
138 break;
139 default:
140 <I>... code ...</I>
141 break;
143 </pre>
144 Here, the same chunk of code is executed when <TT>x</TT> is 3, 4, or 5.
145 </p><p>The <TT>case</TT> labels do not have to be in any particular order;
146 the compiler is smart enough to find the matching one if it's
147 there.
148 The <TT>default</TT> case doesn't have to go at the end, either.
149 </p><p>It's common to switch on characters:
150 <pre>
151 switch(c)
153 case '+':
154 /* code for + */
155 break;
156 case '-':
157 /* code for - */
158 break;
159 case '\n':
160 /* code for newline */
161 /* FALL THROUGH */
162 case ' ':
163 case '\t':
164 /* code for other whitespace */
165 break;
166 case '0': case '1': case '2': case '3': case '4':
167 case '5': case '6': case '7': case '8': case '9':
168 /* code for digits */
169 break;
170 default:
171 /* code for all other characters */
172 break;
174 </pre>
175 It's also common to have a set of <TT>#define</TT>d values,
176 and to switch on those:
177 <pre>
178 #define APPLE 1
179 #define ORANGE 2
180 #define CHERRY 3
181 #define BROCCOLI 4
185 switch(fruit)
187 case APPLE:
188 printf("turnover"); break;
189 case ORANGE:
190 printf("marmalade"); break;
191 case CHERRY:
192 printf("pie"); break;
193 case BROCCOLI:
194 printf("wait a minute... that's not a fruit"); break;
197 </p><hr>
199 Read sequentially:
200 <a href="sx4c.html" rev=precedes>prev</a>
201 <a href="sx4bc.html" rel=precedes>next</a>
202 <a href="sx4c.html" rev=subdocument>up</a>
203 <a href="top.html">top</a>
204 </p>
206 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
207 // <a href="copyright.html">Copyright</a> 1996-1999
208 // <a href="mailto:scs@eskimo.com">mail feedback</a>
209 </p>
210 </body>
211 </html>