📄 c21.htm
字号:
<hr>
<h3><a name="c23"></a>3.if_else 语句和 else_if语句</h3>
<blockquote>
<p> if 语句是 C
语言中最简单的控制语句。让我们讨论一下它的最简单形式和怎样在一个实例中使用它。这个例子在下框中给出,
形式是: <br>
<font size="4" color="#800080"><strong><em>if (condition)<br>
statement;</em></strong></font></p>
<p><font size="3">例子:</font><font size="4" color="#800080"><strong><em>if (answer ==
'Y')<br>
printf("Yes\n");</em></strong></font></p>
<table border="0" width="400">
<tr>
<td width="220" align="left"> 看一下右边解释 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> </em></strong></font>
现在让我们更进一步学习if 语句。我们为if 语句加上else,
这时if 语句将成为if-else语句。这提供了一种在不同情况下选择不同语句的很有效的方法。在下面的例子中,
当"回答"是 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>
printf("Yes\n")<br>
else printf("No\n");</em></strong></font></p>
<p> 当条件为真时, 执行第一个语句,
否则执行第二个语句。它能被用来进行两路选择。</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> </em></strong></font>C语句提供了更简洁地表示
IF-ELSE 语句的方法。它被叫作"条件表达式", 运算符为" ? :
"。这是一个三元的特殊运算符。下面是具有相同作用的两个例子:</p>
<div align="center"><center><table border="5" cellspacing="0" width="280"
bordercolor="#FF9933" bgcolor="#CCFFFF" cellpadding="0">
<tr>
<td width="122">x=x<0?-x:x</td>
<td width="146">if (x<0)<br>
x=-x <br>
else x=x;</td>
</tr>
<tr>
<td width="122">max=a<b?a:b</td>
<td width="146">if (a<b)<br>
max=a <br>
else max=b;</td>
</tr>
</table>
</center></div><p align="left">条件表达式的一般形式是:表达式 ? 表达式 :
表达式。它在 C 语言里不是必需的。然而, 它比if-else语句紧凑多了。</p>
<p align="left"> 更方便的是, 你可以用嵌套 if
语句构造带有不同条件的多路选择。在 C 语言中, 这是有用的,
在编程中, 也是很有效的。请看例子和流图。</p>
<table border="0" width="528" bordercolor="#0000FF" bordercolorlight="#C0C0C0">
<tr>
<td width="295">例子:if (answer=='Y')<br>
printf("Yes!"\n");<br>
else if (answer=='N')<br>
printf("No!"\n");<br>
else<br>
printf("Wrong!"\n");</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
语句里, "else" 和 "if" 是怎样匹配的。这是 if
语句中的二义性问题。看右边, 这个图例说明了问题是怎样解决的。C
语言里, 采用的规则是和最邻近的if 语句组配。</td>
<td width="26%">if(条件) <br>
做这条语句;<br>
<font color="#FF0000"><strong>if</strong></font>(条件) <br>
做这条语句;<br>
<font color="#FF0000"><strong>else</strong></font> <br>
做这条语句; <br>
<br>
else 总是和最近的 if 匹配。</td>
<td width="39%"><font color="#FF0000"><strong>if</strong></font>(条件)<br>
{<br>
if(条件)<br>
做这条语句;<br>
}<br>
<font color="#FF0000"><strong>else</strong></font><br>
做这条语句;<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>
case constant:<br>
statement;...<br>
...<br>
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>
case '1':printf("Do");<br>
break;<br>
case '2':printf("Re");<br>
break;<br>
case '3':printf("Mi");<br>
break;<br>
default: printf("No!");<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> 在 () 中的表达式必须是<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 + -