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

📄 whilestatement.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>while 回圈</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: while 回圈</a></h1>

<br>

C++提供while回圈式,它根据您所指定的条件式来判断是否执行回圈本体,语法如下所示:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">while(条
件式) { </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;">&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>

如果回圈本体只有一个陈述句,则while的{
}可以省略不写;while有些像是没有起始陈述与终止陈述的for回圈,它通常用于重复性的动作,而停止条件未知何时发生的情况,例如一个使用者输入介
面,使用者可能输入10次,也可能输入20次,这时回圈停止的时机是未知的,您可以使用while回圈来作这个事。 <br>

<br>

一个计算输入成绩平均的程式如下所示:<br>

<br>

<pre>#include &lt;iostream&gt; <br>using namespace std; <br><br>int main() { <br>    int score = 0; <br>    int sum = 0; <br>    int count = -1; <br><br>    while(score != -1) { <br>        count++; <br>        sum += score; <br>        cout &lt;&lt; "输入分数(-1结束):"; <br>        cin &gt;&gt; score; <br>    } <br><br>    cout &lt;&lt; "平均:" <br>         &lt;&lt; static_cast&lt;double&gt;(sum) / count <br>         &lt;&lt; endl; <br>   <br>    return 0; <br>}</pre>

<br>

执行结果:<br>

<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);">输入分数(-1结束):10<br>

输入分数(-1结束):20<br>

输入分数(-1结束):30<br>

输入分数(-1结束):40<br>

输入分数(-1结束):50<br>

输入分数(-1结束):-1<br>

平均:30</span></small></td>

    </tr>

  
  </tbody>
</table>

<br>

在这个程式中,使用者的输入次数是未知的,所以您使用while回圈来判断使用者的输入是否为-1,以作为回圈执行的条件。 <br>

<br>

while可以用作无穷回圈,无穷回圈并不是不能用,事实上很多地方都用到的到无穷回圈,例如游戏设计中对使用者输入装置的轮询(poll),或是动画程
式的播放,一个无穷回圈如下所示: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">while(true) { </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;">&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>

无穷回圈可以由自己回圈中的某个条件式来结束,也可以由外部程式的终止或函式呼叫的终止来结束,例如下面是一个回圈内部终止的例子:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">while(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;">&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;
if(条件式) </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;&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;.... </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>

别忘了在C++中非0值在条件式中都会被视为真,所以上面的while中您也可以用1当作回圈执行的条件;当条件式成立时,会执行break离开
while回圈,这个break与switch中的作用是一样的,都是要离开当时的程式区块时使用。 <br>

<br>

while回圈称之为“前测式回圈”,因为它在回圈执行前就会进行条件判断,而do
while称之“后测式回圈”,它会先执行回圈本体,然后再进行条件判断,do while的语法如下所示: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">do { </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;">&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; ....
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">} while(条件式);</span><br>

</div>

<br>

注意while后面是以分号(;)作为结束,这个很常被忽略;由于do
while会先执行回圈,所以它通常用于进行一些初始化或介面沟通的动作,例如下面这个程式: <br>

<br>

<pre>#include &lt;iostream&gt; <br>using namespace std; <br><br>int main() { <br>    int input = 0; <br>    bool replay = false; <br><br>    do { <br>       cout &lt;&lt; "输入整数值:"; <br>       cin &gt;&gt; input; <br>       cout &lt;&lt; "输入数为奇数?" &lt;&lt; ((input % 2) ? 'Y': 'N') &lt;&lt; "\n"; <br>       cout &lt;&lt; "继续(1:继续 0:结束)?"; <br>       cin &gt;&gt; replay; <br>    } while(replay); <br>    <br>    return 0; <br>}</pre>

<br>

执行结果:<br>

<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);">输入整数值:11<br>

输入数为奇数?Y<br>

继续(1:继续 0:结束)?0</span></small></td>

    </tr>

  
  </tbody>
</table>

<br>

如果您日后学会函式的使用,您还可以将这个回圈写的更漂亮一些,而不用使用replay变数,例如若下面more()函式询问使用者是否继续,如果是会传
回1,否则传回0:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">do { </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;cout
&lt;&lt; "输入整数值:"; </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;cin
&gt;&gt; input; </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;cout
&lt;&lt; "输入数为奇数?" &lt;&lt; ((input % 2) ? 'Y': 'N') &lt;&lt; "\n"; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

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

</div>

<br>

一个小小的改变,就可以使程式更为简洁,逻辑上也较为清楚。 <br>

</body>
</html>

⌨️ 快捷键说明

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