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

📄 forstatement.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>for 回圈</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;for 回圈</a></h1>

在C++中所提供的重复性计算陈述之一是 for 回圈式,它的基本语法如下: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">for(初始变
数; 判断式; 递增式) { </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>

如果陈述句只有一个,也就是非复合陈述句,{ } 可以省略不写;for
回圈的第一个初始变数陈述句只会执行一次,之后每次重新进行回圈时,都会根据判断式来判断是否执行下一个回圈,而每次执行完回圈之后,都会执行递增式一
次。 <br>

<br>

来看个简单的范例: <br>

<br>

<pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() { <br>    for(int i = 0; i &lt; 10; i++) <br>        cout &lt;&lt; " " &lt;&lt; i; <br><br>    return 0;<br>}</pre>

<br>

<span class="postbody">
执行结果:</span><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);">&nbsp;0 1 2 3 4 5 6 7 8 9</span></small><span style="color: rgb(255, 255, 255);"><br>

      </span></td>

    </tr>

  
  </tbody>
</table>

<br>

简单的例子,但说明for的作用再适合不过,在C++中您可以直接在for中宣告变数与指定初始值,这个宣告的变数在for回圈结束之后也会自动消失;初
始变数的陈述句只被执行一次,接下来回圈会根据 i 是否小于10来判断是否执行回圈,而每执行一次回圈就将 i 加1。 <br>

<br>

for回圈中也可以再使用for回圈,大家很喜欢用的例子就是显示九九乘法表,这边就用这个例子来说明: <br>

<br>

<pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() { <br>    for(int j = 1; j &lt; 10; j++) { <br>       for(int i = 2; i &lt; 10; i++) { <br>           int tmp = i * j; <br>           cout &lt;&lt; i &lt;&lt; "*" &lt;&lt; j; <br>           if(tmp &gt;= 10) <br>               cout &lt;&lt; "=" &lt;&lt; tmp; <br>           else <br>               cout &lt;&lt; "= " &lt;&lt; tmp; <br>           cout &lt;&lt; "  "; <br>       } <br>       cout &lt;&lt; endl; <br>    } <br><br>    return 0;<br>}</pre>

<br>

<span class="postbody">执行结果:</span><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);">2*1= &nbsp;2&nbsp; 3*1=
&nbsp;3&nbsp; 4*1= &nbsp;4&nbsp; 5*1= &nbsp;5&nbsp; 6*1= &nbsp;6&nbsp;
7*1= &nbsp;7&nbsp; 8*1= &nbsp;8&nbsp; 9*1= &nbsp;9<br>

2*2= &nbsp;4&nbsp; 3*2= &nbsp;6&nbsp; 4*2= &nbsp;8&nbsp; 5*2=10&nbsp;
6*2=12&nbsp; 7*2=14&nbsp; 8*2=16&nbsp; 9*2=18<br>

2*3= &nbsp;6&nbsp; 3*3= &nbsp;9&nbsp; 4*3=12&nbsp; 5*3=15&nbsp;
6*3=18&nbsp; 7*3=21&nbsp; 8*3=24&nbsp; 9*3=27<br>

2*4= &nbsp;8&nbsp; 3*4=12&nbsp; 4*4=16&nbsp; 5*4=20&nbsp; 6*4=24&nbsp;
7*4=28&nbsp; 8*4=32&nbsp; 9*4=36<br>

2*5=10&nbsp; 3*5=15&nbsp; 4*5=20&nbsp; 5*5=25&nbsp; 6*5=30&nbsp;
7*5=35&nbsp; 8*5=40&nbsp; 9*5=45<br>

2*6=12&nbsp; 3*6=18&nbsp; 4*6=24&nbsp; 5*6=30&nbsp; 6*6=36&nbsp;
7*6=42&nbsp; 8*6=48&nbsp; 9*6=54<br>

2*7=14&nbsp; 3*7=21&nbsp; 4*7=28&nbsp; 5*7=35&nbsp; 6*7=42&nbsp;
7*7=49&nbsp; 8*7=56&nbsp; 9*7=63<br>

2*8=16&nbsp; 3*8=24&nbsp; 4*8=32&nbsp; 5*8=40&nbsp; 6*8=48&nbsp;
7*8=56&nbsp; 8*8=64&nbsp; 9*8=72<br>

2*9=18&nbsp; 3*9=27&nbsp; 4*9=36&nbsp; 5*9=45&nbsp; 6*9=54&nbsp;
7*9=63&nbsp; 8*9=72&nbsp; 9*9=81</span></small><span style="color: rgb(255, 255, 255);"><br>

      </span></td>

    </tr>

  
  </tbody>
</table>

<br>

事实上,for回圈的语法其实只是将三个复合陈述区块写在一个括号中而已,所不同的是第一个陈述区块只会执行一次,第二个陈述区块专司判断,而第三个陈述
区块只是一般的陈述句,而不一定只放递增运算式。 <br>

<br>

for括号中的每个陈述区块是以分号(;)作区隔,
而在一个陈述区块中若想写两个以上的陈述句,则使用逗号(,)作区隔,有兴趣的话,看看下面这个九九乘法表的写法,它只使用了一个for回圈就可以完成九
九乘法表的列印: <br>

<br>

<pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() { <br>    for (int i = 2, j = 1; <br>         j &lt; 10; <br>         (i==9)?(i=(++j/j)+1):(i++)) { <br>                                     <br>        int tmp = i * j; <br>        cout &lt;&lt; i &lt;&lt; "*" &lt;&lt; j; <br>        cout &lt;&lt; ((tmp&gt;=10)? "=": "= ") ; <br>        if(i == 9) <br>            cout &lt;&lt; i*j &lt;&lt; endl; <br>        else <br>            cout &lt;&lt; i*j &lt;&lt; " "; <br>            <br>    } <br>    <br>    return 0;<br>}</pre>

<br>

执行结果是一样的,这个程式的重点在于让您看看for回圈的括号中之写法,当然原则上并不鼓励这么写,程式基本上还是以清楚易懂为原则,至于这个九九乘法
表的演算其实也不难,当中的语法之前都介绍过了,您可以亲自演算看看就知道怎么回事了。 <br>

<br>

<br>

</body>
</html>

⌨️ 快捷键说明

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