source: public/doc/gnu-c/switch-Statement.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 6.6 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
71of an expression. The expression, in parentheses, follows the keyword
72<code>switch</code>. After that come all the cases to select among,
73inside 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">&hellip;</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 &ldquo;come here if <var>selector</var> happens to have the value
91<var>value</var>,&rdquo; 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 &ldquo;come here if <var>selector</var> happens to have a value
100between <var>rangestart</var> and <var>rangeend</var> (inclusive).&rdquo; 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.
103They can use arithmetic, and <code>enum</code> constants, but they cannot
104refer to data in memory, because they have to be computed at compile
105time. It is an error if two <code>case</code> labels specify the same
106value, or ranges that overlap, or if one is a range and the other is a
107value in that range.
108</p>
109<p>You can also define a default case to handle &ldquo;any other value,&rdquo; like
110this:
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
119does 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
122can 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&rsquo;t
124necessarily be executed every time the <code>switch</code> statement runs,
125so it is best to avoid giving them initializers.
126</p>
127<p><code>break;</code> inside a <code>switch</code> statement exits immediately from
128the <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,
131execution continues into the code for the following case. This
132happens more often by mistake than intentionally, but since this
133feature 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
136next, write a comment like &lsquo;<samp>falls through</samp>&rsquo; to say it&rsquo;s
137intentional. That way, other programmers won&rsquo;t assume it was an error
138and &ldquo;fix&rdquo; it erroneously.
139</p>
140<p>Consecutive <code>case</code> statements could, pedantically, be considered
141an instance of falling through, but we don&rsquo;t consider or treat them that
142way because they won&rsquo;t confuse anyone.
143</p>
144<hr>
145<div class="header">
146<p>
147Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.