全国计算机等级考试二级教程-C语言程序设计_第4章_选择结构


switch什么时候用break,什么时候不用break

调用break:一次执行一个分支,输入一个数据,对应一个级别

不调用break:连续执行多个分支

if...else

可以处理任何情况,大于小于等于与或非等复杂逻辑都可以处理,看起来不够简洁。

switch

只能处理常量,处理字符整数型常量,看起来很简洁。

case标签值必须是常量,只能判断相等。

在 if 或 else 后面总是用{}

即使只有一条语句的时候


if 最简单的用法

 1 #include <stdio.h>
 2 main()
 3 {
 4     if (3)
 5         printf("AAAA
");    //会输出
 6 
 7     if (0)
 8         printf("BBBB
");    //不会输出
 9 
10     if (0 == 0)
11         printf("CCCC
");    //会输出
12 }

if 的常见问题解析


1 空语句的问题

1 #include <stdio.h>
2 main()
3 {
4     if (3 > 2);    //等价于
5 
6     if (3 > 2)
7         ;    //这是一个空语句
8 }


2

 1 #include <stdio.h>
 2 main()
 3 {
 4     if (3 > 2)
 5         printf("AAAA
");
 6     else
 7         printf("BBBB
");    //是正确的
 8 
 9     if (3 > 2);
10         printf("AAAA
");
11     else
12         printf("BBBB
");    //是错误的
13 }


3

 1 #include <stdio.h>
 2 main()
 3 {
 4     if (3 > 2)
 5         printf("AAAA
");
 6     else if (4 > 2)
 7         printf("BBBB
");
 8     else if (5 > 2)
 9         printf("CCCC
");
10     else
11         printf("DDDD
");    //即便表达式1和2都成立,也只会执行A语句
12 }

4

 1 #include <stdio.h>
 2 main()
 3 {
 4     int x;
 5     scanf("%d", &x);
 6 
 7     if (x >= 90)
 8         printf("AAAA
");
 9     else if (x >= 80)
10         printf("BBBB
");
11     else if (x >= 70)
12         printf("CCCC
");
13     else if (x >= 60)
14         printf("DDDD
");    //最后不加上else,不会出错,但是逻辑上有漏洞
15 }

5

 1 #include <stdio.h>
 2 main()
 3 {
 4     int x;
 5     scanf("%d", &x);
 6 
 7     if (x >= 90)
 8         printf("AAAA
");
 9     else if (x >= 80)
10         printf("BBBB
");
11     else if (x >= 70)
12         printf("CCCC
");
13     else if (x >= 60)
14         printf("DDDD
");
15     else (x < 60);    //最后else不能加表达式
16         printf("EEEE
");
17 }

在计算机中可以精确地存放一个整数,不会出现误差,但整型数值的数值范围比实数小。实型数的数值范围较整型大,但往往存在误差。


如何判断实型x 是否为0

 1 #include <stdio.h>
 2 main()
 3 {
 4     double x = 0.000000;
 5 
 6     if (x - 0.000001 <= 0.0001 || x - 0.000001 >= -0.0001)
 7         printf("AAAA
");
 8     else
 9         printf("BBBB
");
10 }

在多层 switch 嵌套中,break 只能终止距离它最近的 switch

 

 1 #include <stdio.h>
 2 main()
 3 {
 4     int x = 1, y = 0, a = 0, b = 0;
 5 
 6     switch (x)
 7     {
 8     case 1:
 9         switch (y)
10         {
11         case 0:
12             a++;
13             break;
14         case 1:
15             b++;
16             break;
17         }
18         b = 100;
19         break;
20     case 2:
21         a++;
22         b++;
23         break;
24     }
25 
26     printf("a=%d,b=%d
", a, b);
27 }

输出格式:

a=1,b=100

请按任意键继续. . .

continue 在 for 语句

 1 #include <stdio.h>
 2 main()
 3 {
 4     for (1;2;3)
 5     {
 6         A;
 7         B;
 8         continue;    //    如果执行该语句,则执行完该语句后,会执行语句3,C和D会被跳过,C和D不会被执行
 9         C;
10         D;
11     }
12 }


continue 在 while 语句

 1 #include <stdio.h>
 2 main()
 3 {
 4     while (表达式)
 5     {
 6         A;
 7         B;
 8         continue;    //    如果执行该语句,则执行完该语句后,会执行表达式,C和D会被跳过,C和D不会被执行
 9         C;
10         D;
11     }
12 }


scanf 对用户非法输入的处理

 1 #include <stdio.h>
 2 main()
 3 {
 4     int i;
 5     char ch;
 6 
 7     scanf("%d", &i);
 8     printf("i=%d
", i);
 9 
10     while ((ch = getchar() != '
'))
11         continue;
12 
13     int j;
14     scanf("%d", &j);
15     printf("j=%d
", j);
16 }


1输入长方形的三个边,计算长方形的体积。

 1 #include <stdio.h>
 2 main()
 3 {
 4     double a, b, c, s, v;
 5     printf("input a,b,c: 
");
 6     scanf("%lf %lf %lf", &a, &b, &c);
 7     s = a*b;        /*计算长方形的面积*/
 8     v = a*b*c;        /*计算长方形的体积*/
 9     printf("a=%lf,b=%lf,c=%lf 
", a, b, c);
10     printf("s=%lf v=%lf", s, v);
11 }

2把560分钟换算成用小时和分钟表示,然后输出。

1 #include <stdio.h>
2 main()
3 {
4     int a = 560, h, m;
5     h = 560 / 60;
6     m = 560 % 60;
7     printf("560分钟=%d小时%d分钟", h, m);
8 }


3读入三个双精度数,求它们的平均值并保留此平均值小数点后一位数,对小数点后第二位数进行四舍五入,最后输出结果。


 1 #include <stdio.h>
 2 main()
 3 {
 4     double a, b, c, d;
 5     printf("input a,b,c: 
");
 6     scanf("%lf %lf %lf", &a, &b, &c);
 7     d = (a + b + c) / 3;
 8     d = d * 10;
 9     d = d + 0.5;
10     d = (int)d;
11     d = d / 10;
12     printf("平均值是%lf", d);
13 }

4读入三个整数给a, b, c,然后交换它们中的数,把a中原来的值给b,把b中原来的值给c,把c中原来的值给a,然后输出a,b,c。

 1 #include <stdio.h>
 2 main()
 3 {
 4     int a, b, c, t;
 5     printf("input a,b,c: 
");
 6     scanf("%d %d %d", &a, &b, &c);
 7     t = a;
 8     a = c;
 9     c = b;
10     b = t;
11     printf("%d %d %d", a, b, c);
12 }

5输入三个整数,分别放在变量a,b,c中,然后把输入的数据重新按由小到大的顺序放在变量a,b,c中,最后输出a,b,c中的值。


 1 #include <stdio.h>
 2 main()
 3 {
 4     int a, b, c, t;
 5     printf("input a,b,c: 
");
 6     scanf("%d %d %d", &a, &b, &c);
 7     printf("a=%d,b=%d,c=%d 
", a, b, c);
 8     if (a>b)        /*如果a比b大,则进行交换,把小的数放入a中*/
 9     {        t = a;a = b;b = t;    }
10     if (a>c)        /*如果a比c大,则进行交换,把小的数放入a中*/
11     {        t = a;a = c;c = t;    }
12     if (b>c)        /*如果b比c大,则进行交换,把小的数放入b中*/
13     {        t = b;b = c;c = t;    }
14     printf("%d %d %d", a, b, c);
15 }

6输入两个数,分别赋给x和y,输出其中的大数。

 1 #include <stdio.h>
 2 main()
 3 {
 4     int x, y;
 5     printf("input x&y: 
");
 6     scanf("%d %d", &x, &y);
 7     printf("x=%d,y=%d 
", x, y);
 8     if (x > y)
 9         printf("max=x=%d 
", x);
10     else
11         printf("max=y=%d 
", y);
12 }

7输入一个数,判别它是否能被3整除。若能被3整除,打印YES;不能被3整除,打印NO。

 1 #include <stdio.h>
 2 main()
 3 {
 4     int n;
 5     printf("input n:");
 6     scanf("%d", &n);
 7     if (n % 3 == 0)        /*判断n能否被3整除*/
 8         printf("n=%d YES 
", n);
 9     else printf("n=%d NO 
", n);    
10 }

8根据输入的学生成绩给出相应的等级,大于或等于90分以上的等级为A,60分以下的等级为E,其余每10分为一个等级。


8.1用 if,else if 方法

 1 #include <stdio.h>
 2 main()
 3 {
 4     int g;
 5     printf("enter g:");
 6     scanf("%d", &g);
 7     printf("g=%d:", g);
 8     if (g >= 90) printf("A 
");
 9     else if (g >= 80) printf("B 
");
10     else if (g >= 70) printf("C 
");
11     else if (g >= 60) printf("D 
");
12     else printf("E 
");
13 }


8.2用 switch,break 方法

 1 #include <stdio.h>
 2 main()
 3 {
 4     int g;
 5     printf("enter g:");
 6     scanf("%d", &g);
 7     printf("g=%d:", g);
 8     switch (g / 10)
 9     {
10     case 10:printf("A 
");break;
11     case 9:printf("A 
");break;
12     case 8:printf("B 
");break;
13     case 7:printf("C 
");break;
14     case 6:printf("D 
");break;
15     default:printf("E 
");break;
16     }
17 }

 

9若a的值小于100,a<30 m=1; a<40 m=2; a<50 m=3; a<60 m=4; else m=5;


9.1用 if,else if 方法。

 1 #include <stdio.h>  
 2 main()
 3 {
 4     int a, m;
 5     printf("input a:");
 6     scanf("%d", &a);    
 7     if (a < 30) m = 1;
 8     else if (a < 40) m = 2;
 9     else if (a < 50) m = 3;
10     else if (a < 60) m = 4;
11     else m = 5;
12     printf("%d", m);
13 }

9.2用 switch ,break 方法

 1 #include <stdio.h>
 2 main()
 3 {
 4     int a;
 5     printf("input a:");
 6     scanf("%d", &a);
 7     switch (a/10)
 8     {
 9     case 2:printf("1");break;
10     case 3:printf("2");break;
11     case 4:printf("3");break;
12     case 5:printf("4");break;
13     default:printf("5");
14     }
15 }


10输入一位学生的生日(年月日),并输入当前的日期(年月日),输出实际年龄。

 1 #include <stdio.h>
 2 main()
 3 {
 4     int y0, m0, d0, y1, m1, d1, age;
 5     printf("input birthday:");
 6     scanf("%d %d %d", &y0, &m0, &d0);
 7     printf("生日为%d年%d月%d日 
", y0, m0, d0);
 8     
 9     printf("input sysdate:");
10     scanf("%d %d %d", &y1, &m1, &d1);
11     printf("当前日期为%d年%d月%d日 
", y1, m1, d1);
12 
13     age = y1 - y0;
14     if (m1 < m0 || m1 == m0&&d1 < d0)
15         age = age - 1;
16 
17     printf("实际年龄为%d", age);
18 }

11输入一个整数,打印出它是奇数还是偶数。

1 #include <stdio.h>  
2 main()
3 {
4     int a;
5     printf("输入一个整数:");
6     scanf("%d", &a);
7     if (a % 2 == 0) printf("偶数 
");
8     else printf("奇数 
");
9 }

12输入a,b,c三个数,打印最大值。


 1 #include <stdio.h>  
 2 main()
 3 {
 4     int a, b, c, t;
 5     printf("input a,b,c:");
 6     scanf("%d %d %d", &a, &b, &c);
 7     printf("a=%d,b=%d,c=%d 
", a, b, c);
 8     
 9     if (a > b)
10     {
11         t = a;a = b;b = t;
12     };
13     if (a > c)
14     {
15         t = a;a = c;c = t;
16     };
17     if (b > c)
18     {
19         t = b;b = c;c = t;
20     };
21 
22     printf("max=%d", t);
23 }

13对于以下函数,要求输入x的值,输出y的值。

y=x (-5<x<0)

y=x-1 (x=0)

y=x+1 (0<x<10)


13.1不嵌套的if语句

 1 #include <stdio.h>  
 2 main()
 3 {
 4     double x, y;
 5     printf("input x:");
 6     scanf("%lf", &x);
 7     printf("x=%lf 
", x);
 8     
 9     if (x > (-5) && x < 0)
10         y = x;
11     if (x == 0)
12         y = x - 1;
13     if (x > 0 && x < 10)
14         y = x + 1;
15 
16     printf("y=%lf", y);
17 }

13.2嵌套的if语句


 1 #include <stdio.h>  
 2 main()
 3 {
 4     double x, y;
 5     printf("input x:");
 6     scanf("%lf", &x);
 7     printf("x=%lf 
", x);
 8     
 9     if (x > (-5) && x < 0) y = x;
10     else if (x == 0) y = x - 1;
11     else if (x > 0 && x < 10) y = x + 1;
12     
13     printf("y=%lf", y);
14 }

13.3 if-else的语句

 1 #include <stdio.h>  
 2 main()
 3 {
 4     double x, y;
 5     printf("input x:");
 6     scanf("%lf", &x);
 7     printf("x=%lf 
", x);
 8     
 9     if (x == 0) (y = x - 1);
10     else {
11         if (x > (-5) && x < 0) (y = x);
12         if (x > 0 && x < 10) (y = x + 1);
13     }
14         
15     printf("y=%lf", y);
16 }

13.4 switch 语句

 1 #include <stdio.h>  
 2 main()
 3 {
 4     double x, y;
 5     int n;
 6     printf("input x:");
 7     scanf("%lf", &x);
 8 
 9     if (x > (-5) && x < 0) n = 0;
10     if (x == 0) n = 1;
11     if (x > 0 && x < 10) n = 2;
12 
13     switch (n)
14     {
15     case 0:y = x;break;
16     case 1:y = x - 1;break;
17     case 2:y = x + 1;break;
18     }
19 
20     printf("y=%lf", y);
21 }
原文地址:https://www.cnblogs.com/denggelin/p/5379882.html