⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 switchstatement.html

📁 关于 C++ 的历史无须我来介绍了
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

  
  <link rel="stylesheet" href="css/stdlayout.css" type="text/css">

  
  <link rel="stylesheet" href="css/print.css" type="text/css">

  
  <meta content="text/html; charset=gb2312" http-equiv="content-type">

  
  <title>switch 条件判断</title>
</head>


<body>

<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>

<h1><a href="CppGossip.html">C++ Gossip:&nbsp;switch 条件判断</a></h1>

switch是C++提供的条件判断陈述式,它只能比较数值或字元,不过别以为这样它就比if
来得没用,使用适当的话,它可比if判断式来得有效率;switch的语法架构如下:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">switch
(变数名称或运算式) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;
&nbsp; case 符合数字或字元: </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;
&nbsp; &nbsp; &nbsp; 陈述句一; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;
&nbsp; &nbsp; &nbsp; break; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;
&nbsp; case 符合数字或字元: </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;
&nbsp; &nbsp; &nbsp; 陈述句二; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;
&nbsp; &nbsp; &nbsp; break; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;
&nbsp; default: </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;
&nbsp; &nbsp; &nbsp; 陈述三; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>

</div>

<br>

首先看看switch的括号,当中置放您要取出数值的变数,取出数值之后,程式会开始与case中所设定的数字或字元作比对,如果符合就执行以下的陈述
句,直到遇到break后离开switch区块,如果没有符合的数值或字元,则会执行default后的陈述句,default不一定需要,您可以省去这
个部份。 <br>

<br>

来看看上一个范例的成绩等级比对如何使用switch来改写: <br>

<br>

<pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() { <br>    int score = 0; <br>    int level = 0; <br><br>    cout &lt;&lt; "输入分数:"; <br>    cin &gt;&gt; score; <br>    level = (int) score/10; <br><br>    switch(level) { <br>        case 10: <br>        case 9: <br>            cout &lt;&lt; "得A" &lt;&lt; endl; <br>            break; <br>        case 8: <br>            cout &lt;&lt; "得B" &lt;&lt; endl; <br>            break; <br>        case 7: <br>            cout &lt;&lt; "得C" &lt;&lt; endl; <br>            break; <br>        case 6: <br>            cout &lt;&lt; "得D" &lt;&lt; endl; <br>            break; <br>        default: <br>            cout &lt;&lt; "得E(不及格)" &lt;&lt; endl; <br>    } <br>    <br>    return 0;<br>}</pre>

<br>

执行结果:<br>

<small> </small><small> </small><small> </small><small> </small><small>
</small>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">

  <tbody>

    <tr>

      <td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">输入分数:85<br>

得B</span><span style="color: rgb(255, 255, 255);"></span></small></td>

    </tr>

  
  </tbody>
</table>

<br>

在这个程式中,您使用除法并取得运算后的商数,如果大于90的话,除以10的商数一定是9或10(100分时),在case
10中没有任何的陈述,也没有使用break,所以会继续往下执行,直到遇到break离开switch为止,所以学生成绩100分的话,也会显示A的成
绩等级;如果比对的条件不在10到6这些值的话,会执行default下的陈述,这表示商数小于6,所以学生的成绩等级就显示为E了。 <br>

<br>

注意在case后的等号是冒号而不是分号,这是个很常键错的符号;如果您比对的是字元,则记得加上单引号(' '),例如: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">case 'A';</span><br>

</div>

<br>

这个程式与使用if来判断成绩等级的程式有何不同?如果纯綷比对数字或字元的话,建议使用switch,因为它只会在一开始的switch括号中取出变数
值一次,然后将这个值与下面所设定的case比对,但如果您使用if的话,每次遇到条件式时,都要取出变数值,效率的差异就在这,例如: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">if(a == 1) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; //
.... </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">else if(a == 2) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; //
.... </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">else if(a == 3) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; //
....</span><br>

</div>

<br>

这个程式片段中我们在最差的状况下,也就是a = 3时,共需三次比对,而每次比对都必须取出变数a的值一次,如果换成switch的话:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">switch(a) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; case
1: </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; // .. </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; break; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; case
2: </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; // .. </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; break; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; case
3: </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; // .. </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; break; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>

</div>

<br>

在这个程式片段中,只在一开头switch的括号中取出变数a的值,然后逐一比对下面的case,效率的差别就在于这边。 <br>

<br>

当然并不是使用if就不好,遇到复合条件时,switch就帮不上忙了,您无法在switch中组合复杂的条件陈述,这时就得使用if了,简单的说,if
与switch两者可以搭配着灵活使用。 <br>

<br>

如果您比对的是一个数值范围,在gcc的C++编译器中还允许这样的比对方式:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">switch(i) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; case
1 ... 30: </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; // statements </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; case
31 ... 1000: </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; // statements </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
default: </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; // statements </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>

</div>

<br>

如上所示范的,您可以使用...来设定一个范围的数值,而不用连续的撰写case来比对。 <br>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -