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. -->
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
>
15 <H3>18.3.1:
<TT>switch
</TT></H3>
17 <p>[This section corresponds to K
&R Sec.
3.4]
18 </p><p>A frequent sort of pattern is exemplified by
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''.
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:
66 The expression
<I>expr
</I>
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)
72 execution jumps to there,
77 <TT>break
</TT> statement.
79 if there is a
<TT>default
</TT> label,
80 execution jumps to there
83 <TT>break
</TT> statement).
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>,
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
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
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,
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
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
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:
160 /* code for newline */
164 /* code for other whitespace */
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 */
171 /* code for all other characters */
175 It's also common to have a set of
<TT>#define
</TT>d values,
176 and to switch on those:
188 printf(
"turnover"); break;
190 printf(
"marmalade"); break;
192 printf(
"pie"); break;
194 printf(
"wait a minute... that's not a fruit"); break;
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>
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>