循环结构是指在程序中需要反复执行某个功能而设置的一种程序结构。
文章目录
- for
- do...while
- while
- 循环控制语句
- break
- continue
- goto
- 九九乘法表
- 判断某个数是否是质数
- 在[1, 100]内有多少个质数
- 最大公约数
- 枚举两个数的所有公约数
- 辗转相除法
- 计算整数的位数
- 整数分解
for
在很多时候,程序需要反复执行某段代码,例如,有:f(n) = 2 + 4 + 6 + ... + n
且 n > 2,那么当 n = 100 时求 f(n) 的值。这时需要循环执行累加操作,代码如下:
1
2
3
4
5
6
7
8
9
10
11#include<stdio.h> int main() { int i, n = 100, sum = 0; for(i = 2; i <= n; i += 2) { sum += i; // printf("f(%d)=%dn", i, sum); } printf("f(%d)=%d", n, sum); return 0; }
执行结果:
1
2f(100)=2550
同样的,有:f(n) = 1 + 1/2 + 1/4 + 1/6 + ... + 1/n
且 n > 2,那么当 n = 100 时求 f(n) 的值,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12#include<stdio.h> int main() { int i, n = 100; double sum = 1.0; for(i = 2; i <= n; i += 2) { sum += 1.0 / i; // printf("f(%d)=%fn", i, sum); } printf("f(%d)=%f", n, sum); return 0; }
执行结果:
1
2f(100)=3.249603
这两个例子都是使用到了 C 中的 for 循环结构,它的基本语法格式是:
1
2
3
4for([init]; [condition]; increment) { // do something here... }
在 for 结构的 ()
中 的三个子句均为可选,分别表示的含义是:循环开始前的对循环控制变量进行初始化,循环进行的条件和循环控制变量的自运算表达式。而结构中的 {}
及其内部的代码块被称之为循环体。例如:
1
2
3
4for(int i = 0; i < 100; i++) { // do something here... }
在该循环执行之前,先执行 int i = 0;
子句,初始化循环控制变量 i
,第二步,判断是否满足继续循环的条件,即判断逻辑表达式 i < 100
的结果是否为真,若表达式结果为真,则执行 {}
内的代码块,若表达式结果为假,则终止循环,每次走完循环体中的代码,都会执行 i++
,然后再进行第二步,判断是否满足继续循环的条件,以此循环下去。
要注意的是,如果将循环写成 for( ; ; ) {}
这样,表示结构中的条件恒成立,程序将在此结构中不断地循环,导致不能正常退出程序,要么将系统资源消耗殆尽被迫终止,要么采用外部程序或指令强制迫使程序退出。这种循环被称之为 无限循环 或 死循环。
do…while
do…while 循环另一种格式的循环结构,它的基本语法格式是:
1
2
3
4do { // do something here... } while(condition);
该循环结构相对 for 循环而言需要的子句更少,只需要一个条件表达式即可,只要条件表达式结果为真,则不断进行循环操作。若结构中的条件表达式若恒成立,则同样会发生死循环。
将上面使用 for 循环的代码调整为 do…while 循环后,如下所示:
- 有:
f(n) = 2 + 4 + 6 + ... + n
且 n > 2,那么当 n = 100 时求 f(n) 的值。
1
2
3
4
5
6
7
8
9
10
11
12#include<stdio.h> int main() { int i = 2, n = 100, sum = 0; do { sum += i; // printf("f(%d)=%dn", i, sum); i += 2; } while(i <= n); printf("f(%d)=%d", n, sum); return 0; }
- 有:
f(n) = 1 + 1/2 + 1/4 + 1/6 + ... + 1/n
且 n > 2,那么当 n = 100 时求 f(n) 的值
1
2
3
4
5
6
7
8
9
10
11
12
13#include<stdio.h> int main() { int i = 2, n = 100; double sum = 1.0; do { sum += 1.0 / i; // printf("f(%d)=%fn", i, sum); i += 2; } while(i <= n); printf("f(%d)=%f", n, sum); return 0; }
while
while 循环和 do…while 循环很相似,它的基本语法格式是:
1
2
3
4while(condition) { // do something here... }
它和 do…while 的区别在于:while 先执行逻辑判断,再执行循环体。比如需要计算 1+2+3+ ... + 100
,采用 while 和 do…while 两种循环来实现。如下例:
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#include<stdio.h> int main() { int i, sum; // do...while { i = 0; sum = 0; do { i++; sum += i; } while(i < 100); printf("do...while's result is %d, i=%dn", sum, i); } // while { i = 0; sum = 0; while(i < 100) { i++; sum += i; } printf("while's result is %d, i=%dn", sum, i); } return 0; }
执行结果:
1
2
3do...while's result is 5050, i=100 while's result is 5050, i=100
循环控制语句
break
用于在循环体中终止当前的循环结构,例如在 [123, 321] 中找到7的倍数,如果找到第一个则终止循环。代码如下:
1
2
3
4
5
6
7
8
9
10
11
12#include<stdio.h> int main() { int i, max = 321; for(i = 123; i <= max; i++) { if(i % 7 == 0) { break; } } printf("%d", i); return 0; }
执行结果:
1
2126
当 i = 126
时,循环体中的 if 结构条件成立,执行 break; 程序将不再循环。
continue
用于在循环体中终止本次的循环,继续下一次循环,例如将 [123, 321] 中所有的数字相加并忽略7的倍数。代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13#include<stdio.h> int main() { int i, max = 321, sum = 0; for(i = 123; i <= max; i++) { if(i % 7 == 0) { continue; } sum += i; } printf("%d", i); return 0; }
执行结果
1
2322
goto
将控制转移到被标记的语句。但是不建议在程序中使用 goto 语句。
九九乘法表
采用循环嵌套循环的方式在控制台输出九九乘法表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include<stdio.h> int main() { int x, y, product; for(x = 1; x <= 9; x++) { for(y = 1; y <= x; y++) { product = x * y; if(product > 9) { printf("%dx%d=%d", y, x, product); } else { printf("%dx%d=0%d", y, x, product); } if(y != x) { printf("t"); } } printf("n"); } return 0; }
执行结果:
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
461x1=01 1x2=02 2x2=04 1x3=03 2x3=06 3x3=09 1x4=04 2x4=08 3x4=12 4x4=16 1x5=05 2x5=10 3x5=15 4x5=20 5x5=25 1x6=06 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 1x7=07 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 1x8=08 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 1x9=09 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
判断某个数是否是质数
在数学中,质数又称素数。一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数;否则称为合数。
如果需要使用程序实现判断某个自然数是否是质数,按照定义,最起码需要从 2 开始逐数判断。这里使用 do…while 来实现,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include<stdio.h> int main() { int number, isPrime = 1; printf("please input a integer number:"); scanf("%d", &number); if(number < 1) { isPrime = 0; } else if(number < 4) { // 如果比 4 小,则只要是大于1 的自然数都是质数 isPrime = number > 1; } else { int i = 2; do { if(number % i == 0) { isPrime = 0; break; } i++; } while(i < number); } printf("is%sprime.", isPrime ? " " : " not "); return 0; }
执行程序,输入 89,得到结果:
1
2
3please input a integer number:89 is prime.
若输入 77,则得到结果:
1
2
3please input a integer number:77 is not prime.
在[1, 100]内有多少个质数
结合上一个判断质数的程序,写一个 while 和 do…while 相结合的源程序,代码如下:
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#include<stdio.h> int main() { int top = 100, number = 0, count = 0, isPrime; while(number < top) { number++; // 判断 number 是否是 质数 { isPrime = 1; if(number < 1) { isPrime = 0; } else if(number < 4) { // 如果比 4 小,则只要是大于1 的自然数都是质数 isPrime = number > 1; } else { int i = 2; do { if(number % i == 0) { isPrime = 0; break; } i++; } while(i < number); } } count += isPrime ? 1 : 0; }; printf("%d.n", count); return 0; }
执行结果:
1
225
从该程序中可以知道的是,当 do…while 被 break 之后,外部的 while 循环不受影响,这说明 break 只能跳出所在的循环。
最大公约数
输入两个数,求出他们的最大公约数。求最大公约数的方法这里介绍两种:
- 枚举两个数的所有公约数
- 辗转相除法
枚举两个数的所有公约数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include<stdio.h> int main() { int a, b, min, result = 0; scanf("%d %d", &a, &b); min = a < b ? a : b; for(int i = 1; i < min; i++) { if(a % i == 0 && b % i == 0) { result = i; // 若求最小公约数,此处新增 break; 即可 // break; } } if(result > 0) { printf("greatest common divisor is %dn", result); } else { printf("no greatest common divisor."); } return 0; }
执行程序,输入 12 18
后得到结果:
1
2
312 18 greatest common divisor is 6
辗转相除法
- 如果
b == 0
运算结束,a 就是最大公约数 - 否则 计算 a 除以 b,得到余数,使 b 的值赋予给 a,余数的值赋予给 b
- 回到第一步
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include<stdio.h> int main() { int a, b, remainder; scanf("%d %d", &a, &b); while(b != 0) { remainder = a % b; a = b; b = remainder; } if(a > 0) { printf("greatest common divisor is %dn", a); } else { printf("no greatest common divisor."); } return 0; }
计算整数的位数
输入一个正整数,输出该整数的位数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include<stdio.h> int main() { int x, number, bit; printf("please input a integer number:"); scanf("%d", &number); x = x > 0 ? number : 0 - number; bit = 0; while(x > 0) { x /= 10; bit++; } printf("%d is %d digits", number, bit); return 0; }
执行程序,输入 789, 得到结果:
1
2
3please input a integer number:789 789 is 3 digits
整数分解
输入一个正整数,将其全部分解,正序输出每一位数字并使用空格隔开。
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
50
51
52
53#include<stdio.h> int main() { int x, t, number, mask; printf("please input a integer number:"); scanf("%d", &number); x = number > 0 ? number : 0 - number; mask = 1; while(x > 0) { x /= 10; if(x > 0) { mask *= 10; } } // 789 / 100 = 7 // 789 % 100 = 89 // 100 / 10 = 10 // --- // 89 / 10 = 8 // 89 % 10 = 9 // 10 / 10 = 1 // --- // 9 / 1 = 9 // 9 % 1 = 0 // 1 / 10 = 0 x = number > 0 ? number : 0 - number; if(number < 0) { printf("-"); } while(mask > 0) { t = x / mask; x %= mask; mask /= 10; printf("%d", t); if(mask > 0) { printf(" "); } } return 0; }
执行程序,输入 789, 得到结果:
1
2
3please input a integer number:1200 1 2 0 0
若输入 -23,则得到结果:
1
2
3please input a integer number:-23 -2 3
最后
以上就是有魅力小蚂蚁最近收集整理的关于C —— 循环结构的全部内容,更多相关C内容请搜索靠谱客的其他文章。
发表评论 取消回复