-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch06s05.html
49 lines (47 loc) · 4.94 KB
/
ch06s05.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>5. 嵌套循环</title><link rel="stylesheet" href="styles.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /><link rel="start" href="index.html" title="Linux C编程一站式学习" /><link rel="up" href="ch06.html" title="第 6 章 循环语句" /><link rel="prev" href="ch06s04.html" title="4. break和continue语句" /><link rel="next" href="ch06s06.html" title="6. goto语句和标号" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">5. 嵌套循环</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06s04.html">上一页</a> </td><th width="60%" align="center">第 6 章 循环语句</th><td width="20%" align="right"> <a accesskey="n" href="ch06s06.html">下一页</a></td></tr></table><hr /></div><div class="sect1" lang="zh-cn" xml:lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id2727530"></a>5. 嵌套循环</h2></div></div></div><p>上一节求素数的例子在循环中调用一个函数,而那个函数里面又有一个循环,这其实是一种嵌套循环。如果把那个函数的代码拿出来写就更清楚了:</p><div class="example"><a id="id2727541"></a><p class="title"><b>例 6.2. 用嵌套循环求1-100的素数</b></p><div class="example-contents"><pre class="programlisting">#include <stdio.h>
int main(void)
{
int i, j;
for (i = 1; i <= 100; i++) {
for (j = 2; j < i; j++)
if (i % j == 0)
break;
if (j == i)
printf("%d\n", i);
}
return 0;
}</pre></div></div><br class="example-break" /><p>现在内循环的循环变量就不能再用<code class="literal">i</code>了,而是改用<code class="literal">j</code>,原来程序中<code class="literal">is_prime</code>函数的参数<code class="literal">n</code>现在直接用<code class="literal">i</code>代替。在有多层循环或<code class="literal">switch</code>嵌套的情况下,<code class="literal">break</code>只能跳出最内层的循环或<code class="literal">switch</code>,<code class="literal">continue</code>也只能终止最内层循环并回到该循环的开头。</p><p>用循环也可以打印表格式的数据,比如打印小九九乘法表:</p><div class="example"><a id="id2727617"></a><p class="title"><b>例 6.3. 打印小九九</b></p><div class="example-contents"><pre class="programlisting">#include <stdio.h>
int main(void)
{
int i, j;
for (i=1; i<=9; i++) {
for (j=1; j<=9; j++)
printf("%d ", i*j);
printf("\n");
}
return 0;
}</pre></div></div><br class="example-break" /><p>内循环每次打印一个数,数与数之间用两个空格隔开,外循环每次打印一行。结果如下:</p><pre class="screen">1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81</pre><p>结果有一位数的有两位数的,这个表格很不整齐,如果把打印语句改为<code class="literal">printf("%d\t", i*j);</code>就整齐了,所以Tab字符称为制表符。</p><div class="simplesect" lang="zh-cn" xml:lang="zh-cn"><div class="titlepage"><div><div><h3 class="title"><a id="id2727653"></a>习题</h3></div></div></div><p>1、上面打印的小九九有一半数据是重复的,因为8*9和9*8的结果一样。请修改程序打印这样的小九九:</p><pre class="screen">1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81</pre><p>2、编写函数<code class="literal">diamond</code>打印一个菱形。如果调用<code class="literal">diamond(3, '*')</code>则打印:</p><pre class="screen"> *
* * *
*</pre><p>如果调用<code class="literal">diamond(5, '+')</code>则打印:</p><pre class="screen"> +
+ + +
+ + + + +
+ + +
+</pre><p>如果用偶数做参数则打印错误提示。</p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch06s04.html">上一页</a> </td><td width="20%" align="center"><a accesskey="u" href="ch06.html">上一级</a></td><td width="40%" align="right"> <a accesskey="n" href="ch06s06.html">下一页</a></td></tr><tr><td width="40%" align="left" valign="top">4. break和continue语句 </td><td width="20%" align="center"><a accesskey="h" href="index.html">起始页</a></td><td width="40%" align="right" valign="top"> 6. goto语句和标号</td></tr></table></div></body></html>