分支

if

用简单 if 实现单一选择

if 语句通用格式:

if (expression)
statement

当 expression 为真时执行 statement, 当 expression 为假时跳过 statement.

statement 可以是一条简单语句或复合语句.

if 只能实现单一选择, 执行这一条语句或者跳过这一条语句.

单个 if 的程序示例:

#include<stdio.h>
int main(void)
{
printf("Enter the temperature (Enter q to quit):");
float temp = 0.0f;
int all_days = 0;
int cold_days = 0;
while (scanf("%f",&temp)==1)
{
all_days++;
if (temp < 0)
cold_days++;
printf("Enter the temperature (Enter q to quit):");
}
printf("all days : %d.\n", all_days);
printf("cold days : %d.\n", cold_days);
return 0;
}

结果:

Enter the temperature (Enter q to quit):1
Enter the temperature (Enter q to quit):2
Enter the temperature (Enter q to quit):-1
Enter the temperature (Enter q to quit):-2
Enter the temperature (Enter q to quit):q
all days : 4.
cold days : 2.

用 if else 实现双重/多重选择

if else 语句的通用形式为:

if (expression)
statement1
else
statement2

C 不要求缩进.

if 和 else 之间只允许有一条语句 (简单语句或复合语句).

if else 可以实现双重选择.

用多个 if else 实现多重选择

用 else if 扩展 if 或 else 可以实现多重选择.

通用格式为:

if (expression1)
statements;
else if (expression2)
statements;
else if (expression3)
statements;
else
statements;

程序示例:

#include<stdio.h>
int main(void)
{
printf("Enter your score: ");
int score;
scanf("%d", &score);
if (score > 90)
printf("Great!\n");
else if (score > 80)
printf("Good!\n");
else if (score > 70)
printf("ok!\n");
else if (score > 60)
printf("so so!\n");
else
printf("Bad!\n");
return 0;
}

else 与离它最近的 if 配对, 除非这个 if 被花括号括起来了, 那就继续向上找最近的 if.

一条 if 语句被视为一条语句, 即便里面可能嵌套着其他语句. 一条 if else 语句也视为一条语句.

寻找约数的程序:

#include<stdio.h>
#include<stdbool.h>
int main(void)
{
unsigned long num; // 待测试的数
unsigned long div; // 可能的约数
bool isPrime; // 素数标记
printf("Enter an integer (Enter q to quit): ");
while (scanf("%d", &num) == 1)
{
for (div = 2, isPrime = true; (div * div) <= num; div++)
{
if (num % div == 0)
{
isPrime = false;
if (num / div == div)
printf("%lu is divisible by %lu.\n", num, div);
else
printf("%lu is divisible by %lu and %d.\n", num, div, num / div);
}
}
if (isPrime)
printf("%lu is prime.\n", num);
printf("Enter another integer (enter q to quit): ");
}
return 0;
}

结果:

123456 is divisible by 2 and 61728.
123456 is divisible by 3 and 41152.
123456 is divisible by 4 and 30864.
123456 is divisible by 6 and 20576.
123456 is divisible by 8 and 15432.
123456 is divisible by 12 and 10288.
123456 is divisible by 16 and 7716.
123456 is divisible by 24 and 5144.
123456 is divisible by 32 and 3858.
123456 is divisible by 48 and 2572.
123456 is divisible by 64 and 1929.
123456 is divisible by 96 and 1286.
123456 is divisible by 192 and 643.
Enter another integer (enter q to quit): 12345
12345 is divisible by 3 and 4115.
12345 is divisible by 5 and 2469.
12345 is divisible by 15 and 823.
Enter another integer (enter q to quit): 1234
1234 is divisible by 2 and 617.
Enter another integer (enter q to quit): 123
123 is divisible by 3 and 41.
Enter another integer (enter q to quit): 12
12 is divisible by 2 and 6.
12 is divisible by 3 and 4.
Enter another integer (enter q to quit): 1
1 is prime.
Enter another integer (enter q to quit): q

分析:

因为希望换一个数字时不用重新执行程序, 所以程序应该使用一个循环让程序能够连续输入待测试的数. 这种循环的伪代码:

提示用户输入数字
当scanf()的返回值为1
分析该数并报告结果
提示用户继续输入

在测试条件中使用 scanf() 可以将读取数字和判断测试条件确定是否结束循环合并在一起.

该程序将 1 认为是素数, 但其实 1 不是素数, 所以有待改进.

switch

通用形式:

switch (测试表达式)
{
case 常量1:
语句(可选)
break;(可选)
case 常量2:
语句(可选)
break;(可选)
default:(可选)
语句(可选)
break;(可选)
}

代码示例:

#include<stdio.h>
int main(void)
{
int a;
printf("Enter a integer number (1 - 7), <=0 to quit:");
while (scanf("%d", &a) > 0 && a > 0 && a < 8)
{
switch (a)
{
case 1:
printf("Monthday.\n");
printf("Enter a integer number (1 - 7), <=0 to quit:");
break;
case 2:
printf("Tuseday.\n");
printf("Enter a integer number (1 - 7), <=0 to quit:");
break;
case 3:
printf("Wednesday.\n");
printf("Enter a integer number (1 - 7), <=0 to quit:");
break;
case 4:
printf("Thurthday.\n");
printf("Enter a integer number (1 - 7), <=0 to quit:");
break;
case 5:
printf("Friday.\n");
printf("Enter a integer number (1 - 7), <=0 to quit:");
break;
case 6:
printf("Saturday.\n");
printf("Enter a integer number (1 - 7), <=0 to quit:");
break;
case 7:
printf("Sunday.\n");
printf("Enter a integer number (1 - 7), <=0 to quit:");
break;
}
}
printf("Bye!\n");
return 0;
}

结果:

Enter a integer number (1 - 7), <=0 to quit:1
Monthday.
Enter a integer number (1 - 7), <=0 to quit:2
Tuseday.
Enter a integer number (1 - 7), <=0 to quit:6
Saturday.
Enter a integer number (1 - 7), <=0 to quit:7
Sunday.
Enter a integer number (1 - 7), <=0 to quit:8
Bye!

代码示例:

#include<stdio.h>
int main(void)
{
int a;
printf("Enter a integer number (1 - 7), <=0 to quit:");
while (scanf("%d", &a) > 0 && a > 0 && a < 8)
{
switch (a)
{
case 1:
case 2:
case 3:
case 4:
case 5:
printf("Workday.\n");
printf("Enter a integer number (1 - 7), <=0 to quit:");
break;
case 6:
case 7:
printf("Weekend.\n");
printf("Enter a integer number (1 - 7), <=0 to quit:");
break;
}
}
printf("Bye!\n");
return 0;
}

结果:

Enter a integer number (1 - 7), <=0 to quit:1
Workday.
Enter a integer number (1 - 7), <=0 to quit:2
Workday.
Enter a integer number (1 - 7), <=0 to quit:4
Workday.
Enter a integer number (1 - 7), <=0 to quit:5
Workday.
Enter a integer number (1 - 7), <=0 to quit:6
Weekend.
Enter a integer number (1 - 7), <=0 to quit:7
Weekend.
Enter a integer number (1 - 7), <=0 to quit:8
Bye!

若 case 后面没有 break, 则会跳过标签而执行标签内的语句, 包括 default 标签内的语句.

代码示例:

#include<stdio.h>
int func(int a);
int main(void) {
int a = 1;
func(a);
return 0;
}
int func(int a) {
int b = 0;
switch (a) {
case 1:b = 10;
case 2: b = 20;
case 3: b = 30;
default:b = 40;
}
printf("%d\n", b);
}

结果:

40

程序示例:

#include<stdio.h>
int main(void)
{
int a;
printf("Enter a integer number (1 - 7), q to quit:");
while (scanf("%d", &a) > 0)
{
switch (a)
{
case 1:
case 2:
case 3:
case 4:
case 5:
printf("Workday.\n");
printf("Enter a integer number (1 - 7), q to quit:");
break;
case 6:
case 7:
printf("Weekend.\n");
printf("Enter a integer number (1 - 7), q to quit:");
break;
default:
printf("Input error.\n");
printf("Enter a integer number (1 - 7), q to quit:");
}
}
printf("Bye!\n");
return 0;
}

结果:

Enter a integer number (1 - 7), q to quit:0
Input error.
Enter a integer number (1 - 7), q to quit:1
Workday.
Enter a integer number (1 - 7), q to quit:7
Weekend.
Enter a integer number (1 - 7), q to quit:8
Input error.
Enter a integer number (1 - 7), q to quit:q
Bye!

case 标签必须是整型常量表达式, 如字符类型, 1+0 等.

case 一般都只能指定一个值, 不能使用一个范围.

switch 的圆括号的测试表达式必须是整数类型, 包括字符类型.

default 和 case 的先后顺序无关, 可以在前或后.

switch 允许嵌套使用.

程序示例:

#include<stdio.h>
int main(void)
{
int n = 1;
int m = 2;
switch (n)
{
case 1:m++;
case 2:n++;
case 3:
switch (n)
{
case 1:n++;
case 2:m++; n++; break;
}
case 4:m++;
break;
default:
break;
}
printf("m = %d, n = %d\n", m, n);
return 0;
}

结果:

m = 5, n = 3
posted @   有空  阅读(3)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示

目录导航