1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
<html>
<head>
<title>EAGLE Help: switch</title>
</head>
<body bgcolor=white>
<font face=Helvetica,Arial>
<hr>
<i>EAGLE Help</i>
<h1><center>switch</center></h1>
<hr>
The <i>switch</i> statement has the general syntax
<pre>
switch (sw_exp) {
case case_exp: case_statement
...
[default: def_statement]
}
</pre>
and allows for the transfer of control to one of several
<tt>case</tt>-labeled statements, depending on the value of
<tt>sw_exp</tt> (which must be of integral type).
<p>
Any <tt>case_statement</tt> can be labeled by one or more <tt>case</tt>
labels. The <tt>case_exp</tt> of each <tt>case</tt> label must evaluate
to a constant integer which is unique within it's enclosing <tt>switch</tt>
statement.
<p>
There can also be at most one <tt>default</tt> label.
<p>
After evaluating <tt>sw_exp</tt>, the <tt>case_exp</tt> are checked for
a match. If a match is found, control passes to the <tt>case_statement</tt>
with the matching <tt>case</tt> label.
<p>
If no match is found and there is a <tt>default</tt> label, control
passes to <tt>def_statement</tt>. Otherwise none of the statements in the
<tt>switch</tt> is executed.
<p>
Program execution is not affected when <tt>case</tt> and <tt>default</tt>
labels are encountered. Control simply passes through the labels to the
following statement.
<p>
To stop execution at the end of a group of statements for a particular
<tt>case</tt>, use the <a href=219.htm>break</a> statement.
<p>
<b>Example</b>
<pre>
string s = "Hello World";
int vowels = 0, others = 0;
for (int i = 0; s[i]; ++i)
switch (toupper(s[i])) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U': ++vowels;
break;
default: ++others;
}
printf("There are %d vowels in '%s'\n", vowels, s);
</pre>
<hr>
<table width=100% cellspacing=0 border=0><tr><td align=left><font face=Helvetica,Arial>
<a href=index.htm>Index</a>
</font></td><td align=right><font face=Helvetica,Arial size=-1>
<i>Copyright © 2005 CadSoft Computer GmbH</i>
</font></td></tr></table>
<hr>
</font>
</body>
</html>
|