📄 right2-1.htm
字号:
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>第 2 章</title>
<base target="rbottom">
<style>
<!--
.右标题 { font-size: 10pt; color: #000080; text-indent: 0; margin: 0 }
.右内容 { font-size: 10pt; text-align: left; text-indent: 0; line-height: 100%; margin:
0 }
-->
</style>
</head>
<body>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="position: absolute; left: 4; top: -8"><img border="0" src="1.gif" width="63" height="70"></span></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left">
<span style="background-color: #CCFF99">第 2 章</span><span style="background-color: #CCFF99">
</span>><span style="background-color: #CCFF99"> 第 1 节 </span>><span style="background-color: #CCFF99">
2.1 基本结构与控制语句(1)</span></p>
<hr color="#008000" size="1">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font face="??ì?,SimSun" lang="ZH-CN" size="2">1.
程序设计的三种基本结构分别为:顺序结构、选择结构、循环结构,这三种基本结构是程序设计的基础。</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font face="??ì?,SimSun" lang="ZH-CN" size="2">2.
</font><font size="2">if</font><font FACE="??ì?,SimSun" LANG="ZH-CN"><font size="2">语句</font></p>
</font>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
(1)简单if<font FACE="??ì?,SimSun" LANG="ZH-CN">语句的语法格式为:</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN">
<span style="position: absolute; left: 509; top: 145"><img border="0" src="right24.gif" width="135" height="73"></span></font></p>
<b>
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 0; margin-bottom: 0"><font size="2">
</font>
</b>
<font size="2">if(<font FACE="??ì?,SimSun" LANG="ZH-CN">条件表达式</font>)</font></p>
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">
{</font></p>
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000"><font FACE="??ì?,SimSun" LANG="ZH-CN">
语句序列</font>1; </font></p>
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">
}</font></p>
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">
else</font></p>
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">
{</font></p>
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000"><font FACE="??ì?,SimSun" LANG="ZH-CN">
语句序列</font>2;</font></p>
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">
}</font></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">含义:如果条件表达式为真,则执行语句序列</font>1<font FACE="??ì?,SimSun" LANG="ZH-CN">,否则执行语句序列</font>2<font FACE="??ì?,SimSun" LANG="ZH-CN">。</font></font></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font lang="ZH-CN" size="2" face="??ì?,SimSun">
(2)<font LANG="ZH-CN">if语句的嵌套</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">if
<font FACE="??ì?,SimSun" LANG="ZH-CN">语句内还可以使用</font>if<font FACE="??ì?,SimSun" LANG="ZH-CN">语句,这样就构成了</font>if<font FACE="??ì?,SimSun" LANG="ZH-CN">语句的嵌套。</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN"><a href="example2-4.htm">【例</a></font><a href="example2-4.htm">2-4<font LANG="ZH-CN">】</font></a><font LANG="ZH-CN">输入三个整数,输出最大数。</font></font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 5; margin-bottom: 5"></font><font color="#FF6600"><u><font LANG="ZH-CN" size="2">注意:如果没有</font></u><font size="2"><font LANG="ZH-CN"><u>大括号对,</u></font><u>else<font LANG="ZH-CN">与最近的第一个没有配对的且为可见的</font>if<font LANG="ZH-CN">配对。</font></u></font></font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font LANG="ZH-CN" size="2">例如:</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
if(a==0)</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
p=0; //<font LANG="ZH-CN">如果</font>a<font LANG="ZH-CN">等于</font>0<font LANG="ZH-CN">时,</font>p<font LANG="ZH-CN">的值为</font>0</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
if (a<0) //<font LANG="ZH-CN">如果</font>a<font LANG="ZH-CN">小于</font>0<font LANG="ZH-CN">时,</font>p<font LANG="ZH-CN">的值为</font>-1</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
p=-1;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
else</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
p=1; //<font LANG="ZH-CN">由于</font>else<font LANG="ZH-CN">和</font>if<font LANG="ZH-CN">配对,因此当</font>a<font LANG="ZH-CN">大于或等于</font>0<font LANG="ZH-CN">时,</font>p<font LANG="ZH-CN">的值为</font>1</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="oúì?,SimHei" LANG="ZH-CN" size="2">3.
条件运算符与条件表达式</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">
语法格式为:</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; text-indent: 20; margin-top: 5; margin-bottom: 5"><font size="2" color="#008000">
(<font FACE="??ì?,SimSun" LANG="ZH-CN">条件表达式</font>)? (<font FACE="??ì?,SimSun" LANG="ZH-CN">条件为真时的表达式</font>1):
(<font FACE="??ì?,SimSun" LANG="ZH-CN">条件为假时的表达式</font>2)</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">例如:</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">cout
<<('A'<=ch && ch<='Z')? ('a'+ch-'A'): ch</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">结果是输出一个小写字母。</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">4.
switch<font FACE="??ì?,SimSun" LANG="ZH-CN">语句
<span style="position: absolute; left: 436; top: 609"><img border="0" src="right23.gif" width="295" height="107"></span></font></font></p>
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 5; margin-bottom: 0"><font size="2" color="#008000">
switch(<font FACE="??ì?,SimSun" LANG="ZH-CN">表达式</font>)</font></p>
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">
{</font></p>
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">
case <font FACE="??ì?,SimSun" LANG="ZH-CN">常量表达式</font>1: <font FACE="??ì?,SimSun" LANG="ZH-CN">语句组</font>1;</font></p>
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">
case <font FACE="??ì?,SimSun" LANG="ZH-CN">常量表达式</font>2: <font FACE="??ì?,SimSun" LANG="ZH-CN">语句组</font>2;</font></p>
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2" color="#008000">
…</font></p>
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">
case <font FACE="??ì?,SimSun" LANG="ZH-CN">常量表达式</font>n: <font FACE="??ì?,SimSun" LANG="ZH-CN">语句组</font>n;</font></p>
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 0; margin-bottom: 0"><font size="2"><font color="#008000">
default: <font FACE="??ì?,SimSun" LANG="ZH-CN">语句组</font>n+1; </font></font><font size="2"> </font></p>
<p ALIGN="justify" style="line-height: 100%; text-indent: 20; margin-top: 0; margin-bottom: 5"><font size="2" color="#008000">
}</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example2-5.htm">【例</a></font><a href="example2-5.htm">2-5<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">根据学生成绩输出优、良、中、及格和不及格。 </font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">说明:</font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">①</font>
switch<font FACE="??ì?,SimSun" LANG="ZH-CN">后面的表达式一般只能是整型、字符型或枚举类型。</font>case<font FACE="??ì?,SimSun" LANG="ZH-CN">后面的常量必须与常量表达式类型匹配。</font></font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">②</font>
<font FACE="??ì?,SimSun" LANG="ZH-CN">当表达式的值与某一个</font>case<font FACE="??ì?,SimSun" LANG="ZH-CN">后面的常量表达式相等时,就执行此</font>case<font FACE="??ì?,SimSun" LANG="ZH-CN">后面的语句,若没有匹配的常量表达式,就执行</font>default<font FACE="??ì?,SimSun" LANG="ZH-CN">后面的语句。</font></font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">③</font>
<font FACE="??ì?,SimSun" LANG="ZH-CN">每一个常量表达式的值都是惟一的,即常量表达式不能重复出现。</font></font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">④</font>
case<font FACE="??ì?,SimSun" LANG="ZH-CN">后面的语句结束时,会执行下一个</font>case<font FACE="??ì?,SimSun" LANG="ZH-CN">后面的语句,即多个</font>case<font FACE="??ì?,SimSun" LANG="ZH-CN">可以共用一组执行语句。因此,若要跳到</font>switch<font FACE="??ì?,SimSun" LANG="ZH-CN">语句外面,必须借助</font>break<font FACE="??ì?,SimSun" LANG="ZH-CN">语句。</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<hr size="1" color="#008000">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <span style="position: absolute; left: 32; top: 938"><a href="right1-5.htm" target="_self"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>
<span style="position: absolute; left: 493; top: 937"><a href="right2-1-2.htm" target="_self"><img border="0" src="rightd2.gif" width="124" height="63"></a></span></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -