1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>switch Statement (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="switch Statement (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="switch Statement (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Statements.html" rel="up" title="Statements">
|
---|
33 | <link href="switch-Example.html" rel="next" title="switch Example">
|
---|
34 | <link href="continue-Statement.html" rel="prev" title="continue Statement">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="switch-Statement"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="switch-Example.html" accesskey="n" rel="next">switch Example</a>, Previous: <a href="Loop-Statements.html" accesskey="p" rel="prev">Loop Statements</a>, Up: <a href="Statements.html" accesskey="u" rel="up">Statements</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="switch-Statement-1"></span><h3 class="section">19.7 <code>switch</code> Statement</h3>
|
---|
64 | <span id="index-switch-statement"></span>
|
---|
65 | <span id="index-statement_002c-switch"></span>
|
---|
66 | <span id="index-switch"></span>
|
---|
67 | <span id="index-case"></span>
|
---|
68 | <span id="index-default"></span>
|
---|
69 |
|
---|
70 | <p>The <code>switch</code> statement selects code to run according to the value
|
---|
71 | of an expression. The expression, in parentheses, follows the keyword
|
---|
72 | <code>switch</code>. After that come all the cases to select among,
|
---|
73 | inside braces. It looks like this:
|
---|
74 | </p>
|
---|
75 | <div class="example">
|
---|
76 | <pre class="example">switch (<var>selector</var>)
|
---|
77 | {
|
---|
78 | <var>cases</var><span class="roman">…</span>
|
---|
79 | }
|
---|
80 | </pre></div>
|
---|
81 |
|
---|
82 | <p>A case can look like this:
|
---|
83 | </p>
|
---|
84 | <div class="example">
|
---|
85 | <pre class="example">case <var>value</var>:
|
---|
86 | <var>statements</var>
|
---|
87 | break;
|
---|
88 | </pre></div>
|
---|
89 |
|
---|
90 | <p>which means “come here if <var>selector</var> happens to have the value
|
---|
91 | <var>value</var>,” or like this (a GNU C extension):
|
---|
92 | </p>
|
---|
93 | <div class="example">
|
---|
94 | <pre class="example">case <var>rangestart</var> ... <var>rangeend</var>:
|
---|
95 | <var>statements</var>
|
---|
96 | break;
|
---|
97 | </pre></div>
|
---|
98 |
|
---|
99 | <p>which means “come here if <var>selector</var> happens to have a value
|
---|
100 | between <var>rangestart</var> and <var>rangeend</var> (inclusive).” See <a href="Case-Ranges.html">Case Ranges</a>.
|
---|
101 | </p>
|
---|
102 | <p>The values in <code>case</code> labels must reduce to integer constants.
|
---|
103 | They can use arithmetic, and <code>enum</code> constants, but they cannot
|
---|
104 | refer to data in memory, because they have to be computed at compile
|
---|
105 | time. It is an error if two <code>case</code> labels specify the same
|
---|
106 | value, or ranges that overlap, or if one is a range and the other is a
|
---|
107 | value in that range.
|
---|
108 | </p>
|
---|
109 | <p>You can also define a default case to handle “any other value,” like
|
---|
110 | this:
|
---|
111 | </p>
|
---|
112 | <div class="example">
|
---|
113 | <pre class="example">default:
|
---|
114 | <var>statements</var>
|
---|
115 | break;
|
---|
116 | </pre></div>
|
---|
117 |
|
---|
118 | <p>If the <code>switch</code> statement has no <code>default:</code> label, then it
|
---|
119 | does nothing when the value matches none of the cases.
|
---|
120 | </p>
|
---|
121 | <p>The brace-group inside the <code>switch</code> statement is a block, and you
|
---|
122 | can declare variables with that scope just as in any other block
|
---|
123 | (see <a href="Blocks.html">Blocks</a>). However, initializers in these declarations won’t
|
---|
124 | necessarily be executed every time the <code>switch</code> statement runs,
|
---|
125 | so it is best to avoid giving them initializers.
|
---|
126 | </p>
|
---|
127 | <p><code>break;</code> inside a <code>switch</code> statement exits immediately from
|
---|
128 | the <code>switch</code> statement. See <a href="break-Statement.html">break Statement</a>.
|
---|
129 | </p>
|
---|
130 | <p>If there is no <code>break;</code> at the end of the code for a case,
|
---|
131 | execution continues into the code for the following case. This
|
---|
132 | happens more often by mistake than intentionally, but since this
|
---|
133 | feature is used in real code, we cannot eliminate it.
|
---|
134 | </p>
|
---|
135 | <p><strong>Warning:</strong> When one case is intended to fall through to the
|
---|
136 | next, write a comment like ‘<samp>falls through</samp>’ to say it’s
|
---|
137 | intentional. That way, other programmers won’t assume it was an error
|
---|
138 | and “fix” it erroneously.
|
---|
139 | </p>
|
---|
140 | <p>Consecutive <code>case</code> statements could, pedantically, be considered
|
---|
141 | an instance of falling through, but we don’t consider or treat them that
|
---|
142 | way because they won’t confuse anyone.
|
---|
143 | </p>
|
---|
144 | <hr>
|
---|
145 | <div class="header">
|
---|
146 | <p>
|
---|
147 | Next: <a href="switch-Example.html" accesskey="n" rel="next">switch Example</a>, Previous: <a href="Loop-Statements.html" accesskey="p" rel="prev">Loop Statements</a>, Up: <a href="Statements.html" accesskey="u" rel="up">Statements</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
148 | </div>
|
---|
149 |
|
---|
150 |
|
---|
151 |
|
---|
152 | </body>
|
---|
153 | </html>
|
---|