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

📄 c21.htm

📁 经典c语言教程
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<hr>

<h3><a name="c23"></a>3.if_else 语句和 else_if语句</h3>

<blockquote>
  <p>&nbsp;&nbsp;&nbsp; if 语句是 C 
  语言中最简单的控制语句。让我们讨论一下它的最简单形式和怎样在一个实例中使用它。这个例子在下框中给出, 
  形式是: <br>
  &nbsp;&nbsp;&nbsp;&nbsp; <font size="4" color="#800080"><strong><em>if (condition)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; statement;</em></strong></font></p>
  <p><font size="3">例子:</font><font size="4" color="#800080"><strong><em>if (answer == 
  'Y')<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  printf(&quot;Yes\n&quot;);</em></strong></font></p>
  <table border="0" width="400">
    <tr>
      <td width="220" align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 看一下右边解释 if 
      语句结构的流图。当条件(表达式)为真(非零值)时, 语句被执行, 否则, 
      忽略该语句而执行后面的语句。注意, 语句可以是简单的和复合的。</td>
      <td width="172" align="center"><p align="left">流图:<img src="../img/c214.gif"
      alt="c214.gif (1682 bytes)" align="top" WIDTH="100" HEIGHT="110"></td>
    </tr>
  </table>
  <p><font size="4" color="#00FF00"><strong><em>&nbsp;</em></strong></font> 
  &nbsp;&nbsp;&nbsp; 现在让我们更进一步学习if 语句。我们为if 语句加上else, 
  这时if 语句将成为if-else语句。这提供了一种在不同情况下选择不同语句的很有效的方法。在下面的例子中, 
  当&quot;回答&quot;是 Y 时, 将 打印 Yes, 否则打印 No。</p>
  <table border="0" width="77%">
    <tr>
      <td width="55%"> <p>例子: <font size="4" color="#800080"><strong><em>if (answer == 'Y')<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf(&quot;Yes\n&quot;)<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else printf(&quot;No\n&quot;);</em></strong></font></p>
      <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当条件为真时, 执行第一个语句, 
      否则执行第二个语句。它能被用来进行两路选择。</td>
      <td width="45%">流图:<img src="../img/c215.gif" alt="c215.gif (1922 bytes)" align="top"
      WIDTH="119" HEIGHT="114"></td>
    </tr>
  </table>
  <p align="left"><font size="4" color="#800080"><strong><em>&nbsp;&nbsp;&nbsp; </em></strong></font>C语句提供了更简洁地表示 
  IF-ELSE 语句的方法。它被叫作&quot;条件表达式&quot;, 运算符为&quot; ? : 
  &quot;。这是一个三元的特殊运算符。下面是具有相同作用的两个例子:</p>
  <div align="center"><center><table border="5" cellspacing="0" width="280"
  bordercolor="#FF9933" bgcolor="#CCFFFF" cellpadding="0">
    <tr>
      <td width="122">x=x&lt;0?-x:x</td>
      <td width="146">if (x&lt;0)<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x=-x <br>
      else x=x;</td>
    </tr>
    <tr>
      <td width="122">max=a&lt;b?a:b</td>
      <td width="146">if (a&lt;b)<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; max=a <br>
      else max=b;</td>
    </tr>
  </table>
  </center></div><p align="left">条件表达式的一般形式是:表达式 ? 表达式 : 
  表达式。它在 C 语言里不是必需的。然而, 它比if-else语句紧凑多了。</p>
  <p align="left">&nbsp;&nbsp;&nbsp; 更方便的是, 你可以用嵌套 if 
  语句构造带有不同条件的多路选择。在 C 语言中, 这是有用的, 
  在编程中, 也是很有效的。请看例子和流图。</p>
  <table border="0" width="528" bordercolor="#0000FF" bordercolorlight="#C0C0C0">
    <tr>
      <td width="295">例子:if (answer=='Y')<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;Yes!&quot;\n&quot;);<br>
      &nbsp;&nbsp;&nbsp;&nbsp; else if (answer=='N')<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;No!&quot;\n&quot;);<br>
      &nbsp;&nbsp;&nbsp;&nbsp; else<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;Wrong!&quot;\n&quot;);</td>
      <td width="225">流图:<img src="../img/c216.gif" alt="c216.gif (2470 bytes)" align="top"
      WIDTH="172" HEIGHT="114"></td>
    </tr>
  </table>
  <p align="left"> </p>
  <table border="0" width="80%" cellpadding="3">
    <tr>
      <td width="35%">在学习过 else-if 语句后, 你会提出一个问题: 在嵌套的 if 
      语句里, &quot;else&quot; 和 &quot;if&quot; 是怎样匹配的。这是 if 
      语句中的二义性问题。看右边, 这个图例说明了问题是怎样解决的。C 
      语言里, 采用的规则是和最邻近的if 语句组配。</td>
      <td width="26%">if(条件)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
      &nbsp; 做这条语句;<br>
      &nbsp;&nbsp;&nbsp; <font color="#FF0000"><strong>if</strong></font>(条件) <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 做这条语句;<br>
      &nbsp;&nbsp;&nbsp; <font color="#FF0000"><strong>else</strong></font> <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 做这条语句; <br>
      <br>
      else 总是和最近的 if 匹配。</td>
      <td width="39%"><font color="#FF0000"><strong>if</strong></font>(条件)<br>
      {<br>
      &nbsp;&nbsp;&nbsp; if(条件)<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 做这条语句;<br>
      }<br>
      <font color="#FF0000"><strong>else</strong></font><br>
      &nbsp;&nbsp;&nbsp; 做这条语句;<p>由于 {}的作用else和第一个 if 匹配。</td>
    </tr>
  </table>
  <a href="c21.htm#_top.html#_top"><p align="right">返回页首</a> </p>
</blockquote>

<hr>

<h3><a name="c24"></a>4.switch 语句</h3>

<blockquote>
  <p>Else-if 语句使写选择多种情况的程序简单化。但使用右边所的 switch 
  语句将更方便。它的一般形式是:<br>
  </p>
  <table border="0" width="531">
    <tr>
      <td width="214">switch(expression)<br>
      {<br>
      &nbsp;&nbsp;&nbsp; case constant:<br>
      &nbsp;&nbsp;&nbsp;&nbsp; statement;...<br>
      &nbsp;&nbsp;&nbsp; ...<br>
      &nbsp;&nbsp; default:statement;...<br>
      }</td>
      <td width="309"><img src="../img/c217.gif" alt="c217.gif (3836 bytes)" WIDTH="273"
      HEIGHT="182"></td>
    </tr>
  </table>
  <p><br>
  这里是一个说明它如何工作的例子:</p>
  <table border="0" width="548">
    <tr>
      <td width="264">switch ( tone )<br>
      {<br>
      &nbsp;&nbsp; case '1':printf(&quot;Do&quot;);<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br>
      &nbsp;&nbsp; case '2':printf(&quot;Re&quot;);<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br>
      &nbsp;&nbsp; case '3':printf(&quot;Mi&quot;);<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br>
      &nbsp;&nbsp; default: printf(&quot;No!&quot;);<br>
      }<br>
      这个语句根据不同的 tone 的值, 输出不同结果。</td>
      <td width="277"><img src="../img/c218.gif" alt="c218.gif (3913 bytes)" WIDTH="273"
      HEIGHT="182"></td>
    </tr>
  </table>
  <p>&nbsp;&nbsp;&nbsp; 在 () 中的表达式必须是<font color="#FF0000"><strong>常数值</strong></font>。当执行 
  switch 语句时, 
  首先计算该表达式的值。然后程序搜索条件表。直到发现了相匹配的标号, 
  并且程序从这里开始运行直到结尾或一个 break 
  语句。注意如果你想在不同情况下执行不同的语句, 必须使用 break 
  语句。如果没有匹配的标号, 程序将执行 default 后的语句。</p>
  <div align="center"><center><table border="0" width="72%">
    <tr>
      <td width="50%"><img src="../img/c219.gif" alt="c219.gif (4213 bytes)" WIDTH="167"
      HEIGHT="201"></td>
      <td width="50%"><img src="../img/c220.gif" alt="c220.gif (3906 bytes)" WIDTH="167"
      HEIGHT="201"></td>
    </tr>
    <tr>
      <td width="100%" colspan="2">两种情况number的值都是 2, 但右边是从 2 执行到 
      5, 而左边的仅执行到了第 2 句。</td>
    </tr>
  </table>
  </center></div><p align="right"><a href="c21.htm#_top.html#_top">返回页首</a></p>
</blockquote>

<p align="center"><a href="http://www.nec.sjtu.edu.cn/support/Course/C/c/c2/c22.htm"><img src="../img/next.gif" width="145" height="30"
alt="next.gif (3633 bytes)" border="0"></a></p>
</body>
</html>

⌨️ 快捷键说明

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