📄 forstatement.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: 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;">
陈述句一; </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 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 <iostream><br>using namespace std;<br><br>int main() { <br> for(int i = 0; i < 10; i++) <br> cout << " " << 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);"> 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 <iostream><br>using namespace std;<br><br>int main() { <br> for(int j = 1; j < 10; j++) { <br> for(int i = 2; i < 10; i++) { <br> int tmp = i * j; <br> cout << i << "*" << j; <br> if(tmp >= 10) <br> cout << "=" << tmp; <br> else <br> cout << "= " << tmp; <br> cout << " "; <br> } <br> cout << 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= 2 3*1=
3 4*1= 4 5*1= 5 6*1= 6
7*1= 7 8*1= 8 9*1= 9<br>
2*2= 4 3*2= 6 4*2= 8 5*2=10
6*2=12 7*2=14 8*2=16 9*2=18<br>
2*3= 6 3*3= 9 4*3=12 5*3=15
6*3=18 7*3=21 8*3=24 9*3=27<br>
2*4= 8 3*4=12 4*4=16 5*4=20 6*4=24
7*4=28 8*4=32 9*4=36<br>
2*5=10 3*5=15 4*5=20 5*5=25 6*5=30
7*5=35 8*5=40 9*5=45<br>
2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
7*6=42 8*6=48 9*6=54<br>
2*7=14 3*7=21 4*7=28 5*7=35 6*7=42
7*7=49 8*7=56 9*7=63<br>
2*8=16 3*8=24 4*8=32 5*8=40 6*8=48
7*8=56 8*8=64 9*8=72<br>
2*9=18 3*9=27 4*9=36 5*9=45 6*9=54
7*9=63 8*9=72 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 <iostream><br>using namespace std;<br><br>int main() { <br> for (int i = 2, j = 1; <br> j < 10; <br> (i==9)?(i=(++j/j)+1):(i++)) { <br> <br> int tmp = i * j; <br> cout << i << "*" << j; <br> cout << ((tmp>=10)? "=": "= ") ; <br> if(i == 9) <br> cout << i*j << endl; <br> else <br> cout << i*j << " "; <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 + -