流程控制
一、什么是流程控制
代码执行的顺序
二、流程控制的分类
1. 顺序
2. 选择
2.1. if
1. if
| if (/* condition */)
{
/* code */
}
|
2. if else
| if (/* condition */)
{
/* code */
} else
{
/* code */
}
|
3. if else if
1
2
3
4
5
6
7
8
9
10
11
12 | if (/* condition */)
{
/* code */
}
else if (/* condition */)
{
/* code */
}
else
{
/* code */
}
|
2.2 switch
| switch (expression)
{
case /* constant-expression */:
/* code */
break;
default:
break;
}
|
例1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 | #include <stdio.h>
int main()
{
int type;
scanf("%d", &type);
switch (type)
{
case 1:
printf("早上好\n");
break;
case 2:
printf("中午好\n");
break;
case 3:
printf("晚上好\n");
break;
default:
printf("啊,什么啊?\n");
break;
}
return 0;
}
|
3. 循环
3.1 while
语法格式:
| while (/* condition */)
{
/* code */
}
|
例1:求正整数的位数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | #include <stdio.h>
int main()
{
int n = 0;
int x;
scanf("%d", &x);
while (x > 0)
{
n++;
x /= 10;
}
printf("%d\n", n);
return 0;
}
|
例2:整数求逆
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 | #include <stdio.h>
int main()
{
//定义要求逆的整数
int x = 12345;
//保存单个整数
int digit;
//保存最后结果
int ret = 0;
while (x > 0)
{
//取出整数的个位
digit = x % 10;
ret = ret * 10 + digit;
//去除原整数的末位
x = x / 10;
}
printf("%d\n", ret); // 54321
return 0;
}
|
3.2 do while
语法格式:
| do
{
/* code */
} while (/* condition */);
|
例1:猜数游戏
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 | #include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
srand(time(0));
//生成1到100的随机数
int number = rand() % 100 + 1;
int i = 0;
printf("我已经想好了1到100之间一个数。\n");
do
{
printf("请猜1到100之间的数:");
scanf("%d", &i);
if (i > number)
{
printf("你猜的数大了\n");
}
else if (i < number)
{
printf("你猜的数小了\n");
}
} while (i != number);
return 0;
}
|
3.3 for
语法格式:
| for (int i = 0; i < count; i++)
{
/* code */
}
|
例1:
\[
f(n)=1+\frac{1}{2} +\frac{1}{3}+\frac{1}{4}+\frac{1}{5}+\cdots+\frac{1}{n}
\]
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | #include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
double sum = 0;
for (int i = 1; i <= n; i++)
{
sum += 1.0 / i;
printf("%f\n", sum);
}
printf("f(%d)=%f", n, sum);
return 0;
}
|
例2:
\[
f(n)=1-\frac{1}{2} +\frac{1}{3}-\frac{1}{4}+\frac{1}{5}-\cdots+\frac{1}{n}
\]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | #include <stdio.h>
int main()
{
int n;
double sum = 0;
double sign = 1.0;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
sum += sign / i;
sign = -sign;
}
printf("f(%d)=%f", n, sum);
return 0;
}
|
4. break
和continue
4.1 break
- break如果用于循环是用来终止循环
- break如果用于switch则是用来终止switch,在多层switch嵌套中,break只能终止具体它最近的switch
- break不能直接用于if,除非if属于循环内部的一个子句
接力break
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 | #include <stdio.h>
int main(void)
{
int money;
scanf("%d", &money);
int one, two, five;
int exit = 0;
for (one = 1; one <= money * 10; one++)
{
for (two = 1; two <= money * (10 / 2); two++)
{
for (five = 1; five <= money * (10 / 5); five++)
{
if (one + two * 2 + five * 5 == money * 10)
{
printf("%d个一角%d个两角%d个五角可以凑出%d元人民币\n",
one, two, five, money);
exit = 1;
break;
}
}
if (exit == 1)
break;
}
if (exit == 1)
break;
}
return 0;
}
|
4.2 continue
用于跳过本次循环余下的语句,转去判断是否需要执行下一次循环