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

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

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

if...else

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

switch

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

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

在 if 或 else 后面总是用{}

即使只有一条语句的时候

if 最简单的用法

 #include <stdio.h>
main()
{
if ()
printf("AAAA\n"); //会输出 if ()
printf("BBBB\n"); //不会输出 if ( == )
printf("CCCC\n"); //会输出
}

if 的常见问题解析

1 空语句的问题

 #include <stdio.h>
main()
{
if ( > ); //等价于 if ( > )
; //这是一个空语句
}

2

 #include <stdio.h>
main()
{
if ( > )
printf("AAAA\n");
else
printf("BBBB\n"); //是正确的 if ( > );
printf("AAAA\n");
else
printf("BBBB\n"); //是错误的
}

3

 #include <stdio.h>
main()
{
if ( > )
printf("AAAA\n");
else if ( > )
printf("BBBB\n");
else if ( > )
printf("CCCC\n");
else
printf("DDDD\n"); //即便表达式1和2都成立,也只会执行A语句
}

4

 #include <stdio.h>
main()
{
int x;
scanf("%d", &x); if (x >= )
printf("AAAA\n");
else if (x >= )
printf("BBBB\n");
else if (x >= )
printf("CCCC\n");
else if (x >= )
printf("DDDD\n"); //最后不加上else,不会出错,但是逻辑上有漏洞
}

5

 #include <stdio.h>
main()
{
int x;
scanf("%d", &x); if (x >= )
printf("AAAA\n");
else if (x >= )
printf("BBBB\n");
else if (x >= )
printf("CCCC\n");
else if (x >= )
printf("DDDD\n");
else (x < ); //最后else不能加表达式
printf("EEEE\n");
}

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

如何判断实型x 是否为0

 #include <stdio.h>
main()
{
double x = 0.000000; if (x - 0.000001 <= 0.0001 || x - 0.000001 >= -0.0001)
printf("AAAA\n");
else
printf("BBBB\n");
}

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

 #include <stdio.h>
main()
{
int x = , y = , a = , b = ; switch (x)
{
case :
switch (y)
{
case :
a++;
break;
case :
b++;
break;
}
b = ;
break;
case :
a++;
b++;
break;
} printf("a=%d,b=%d\n", a, b);
}

输出格式:

a=1,b=100

请按任意键继续. . .

continue 在 for 语句

 #include <stdio.h>
main()
{
for (;;)
{
A;
B;
continue; // 如果执行该语句,则执行完该语句后,会执行语句3,C和D会被跳过,C和D不会被执行
C;
D;
}
}

continue 在 while 语句

 #include <stdio.h>
main()
{
while (表达式)
{
A;
B;
continue; // 如果执行该语句,则执行完该语句后,会执行表达式,C和D会被跳过,C和D不会被执行
C;
D;
}
}

scanf 对用户非法输入的处理

 #include <stdio.h>
main()
{
int i;
char ch; scanf("%d", &i);
printf("i=%d\n", i); while ((ch = getchar() != '\n'))
continue; int j;
scanf("%d", &j);
printf("j=%d\n", j);
}

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

 #include <stdio.h>
main()
{
double a, b, c, s, v;
printf("input a,b,c: \n");
scanf("%lf %lf %lf", &a, &b, &c);
s = a*b; /*计算长方形的面积*/
v = a*b*c; /*计算长方形的体积*/
printf("a=%lf,b=%lf,c=%lf \n", a, b, c);
printf("s=%lf v=%lf", s, v);
}

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

 #include <stdio.h>
main()
{
int a = , h, m;
h = / ;
m = % ;
printf("560分钟=%d小时%d分钟", h, m);
}

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


 #include <stdio.h>
main()
{
double a, b, c, d;
printf("input a,b,c: \n");
scanf("%lf %lf %lf", &a, &b, &c);
d = (a + b + c) / ;
d = d * ;
d = d + 0.5;
d = (int)d;
d = d / ;
printf("平均值是%lf", d);
}

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

 #include <stdio.h>
main()
{
int a, b, c, t;
printf("input a,b,c: \n");
scanf("%d %d %d", &a, &b, &c);
t = a;
a = c;
c = b;
b = t;
printf("%d %d %d", a, b, c);
}

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

 #include <stdio.h>
main()
{
int a, b, c, t;
printf("input a,b,c: \n");
scanf("%d %d %d", &a, &b, &c);
printf("a=%d,b=%d,c=%d \n", a, b, c);
if (a>b) /*如果a比b大,则进行交换,把小的数放入a中*/
{ t = a;a = b;b = t; }
if (a>c) /*如果a比c大,则进行交换,把小的数放入a中*/
{ t = a;a = c;c = t; }
if (b>c) /*如果b比c大,则进行交换,把小的数放入b中*/
{ t = b;b = c;c = t; }
printf("%d %d %d", a, b, c);
}

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

 #include <stdio.h>
main()
{
int x, y;
printf("input x&y: \n");
scanf("%d %d", &x, &y);
printf("x=%d,y=%d \n", x, y);
if (x > y)
printf("max=x=%d \n", x);
else
printf("max=y=%d \n", y);
}

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

 #include <stdio.h>
main()
{
int n;
printf("input n:");
scanf("%d", &n);
if (n % == ) /*判断n能否被3整除*/
printf("n=%d YES \n", n);
else printf("n=%d NO \n", n);
}

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

8.1用 if,else if 方法

 #include <stdio.h>
main()
{
int g;
printf("enter g:");
scanf("%d", &g);
printf("g=%d:", g);
if (g >= ) printf("A \n");
else if (g >= ) printf("B \n");
else if (g >= ) printf("C \n");
else if (g >= ) printf("D \n");
else printf("E \n");
}

8.2用 switch,break 方法

 #include <stdio.h>
main()
{
int g;
printf("enter g:");
scanf("%d", &g);
printf("g=%d:", g);
switch (g / )
{
case :printf("A \n");break;
case :printf("A \n");break;
case :printf("B \n");break;
case :printf("C \n");break;
case :printf("D \n");break;
default:printf("E \n");break;
}
}

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 方法。

 #include <stdio.h>
main()
{
int a, m;
printf("input a:");
scanf("%d", &a);
if (a < ) m = ;
else if (a < ) m = ;
else if (a < ) m = ;
else if (a < ) m = ;
else m = ;
printf("%d", m);
}

9.2用 switch ,break 方法

 #include <stdio.h>
main()
{
int a;
printf("input a:");
scanf("%d", &a);
switch (a/)
{
case :printf("");break;
case :printf("");break;
case :printf("");break;
case :printf("");break;
default:printf("");
}
}

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

 #include <stdio.h>
main()
{
int y0, m0, d0, y1, m1, d1, age;
printf("input birthday:");
scanf("%d %d %d", &y0, &m0, &d0);
printf("生日为%d年%d月%d日 \n", y0, m0, d0); printf("input sysdate:");
scanf("%d %d %d", &y1, &m1, &d1);
printf("当前日期为%d年%d月%d日 \n", y1, m1, d1); age = y1 - y0;
if (m1 < m0 || m1 == m0&&d1 < d0)
age = age - ; printf("实际年龄为%d", age);
}

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

 #include <stdio.h>
main()
{
int a;
printf("输入一个整数:");
scanf("%d", &a);
if (a % == ) printf("偶数 \n");
else printf("奇数 \n");
}

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

 #include <stdio.h>
main()
{
int a, b, c, t;
printf("input a,b,c:");
scanf("%d %d %d", &a, &b, &c);
printf("a=%d,b=%d,c=%d \n", a, b, c); if (a > b)
{
t = a;a = b;b = t;
};
if (a > c)
{
t = a;a = c;c = t;
};
if (b > c)
{
t = b;b = c;c = t;
}; printf("max=%d", t);
}

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

y=x (-5<x<0)

y=x-1 (x=0)

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

13.1不嵌套的if语句

 #include <stdio.h>
main()
{
double x, y;
printf("input x:");
scanf("%lf", &x);
printf("x=%lf \n", x); if (x > (-) && x < )
y = x;
if (x == )
y = x - ;
if (x > && x < )
y = x + ; printf("y=%lf", y);
}

13.2嵌套的if语句

 #include <stdio.h>
main()
{
double x, y;
printf("input x:");
scanf("%lf", &x);
printf("x=%lf \n", x); if (x > (-) && x < ) y = x;
else if (x == ) y = x - ;
else if (x > && x < ) y = x + ; printf("y=%lf", y);
}

13.3 if-else的语句

 #include <stdio.h>
main()
{
double x, y;
printf("input x:");
scanf("%lf", &x);
printf("x=%lf \n", x); if (x == ) (y = x - );
else {
if (x > (-) && x < ) (y = x);
if (x > && x < ) (y = x + );
} printf("y=%lf", y);
}

13.4 switch 语句

 #include <stdio.h>
main()
{
double x, y;
int n;
printf("input x:");
scanf("%lf", &x); if (x > (-) && x < ) n = ;
if (x == ) n = ;
if (x > && x < ) n = ; switch (n)
{
case :y = x;break;
case :y = x - ;break;
case :y = x + ;break;
} printf("y=%lf", y);
}

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

  1. 全国计算机等级考试二级教程-C语言程序设计_第8章_地址和指针

    面试: unsigned int *p1 = &num; int *p2 = &num; #define _CRT_SECURE_NO_WARNINGS #include<std ...

  2. 全国计算机等级考试二级教程-C语言程序设计_第15章_位运算

    位运算,不适用于实数,仅仅适用于整数.字符. C语言的位运算只能操作整数.字符,实数是指数方式表示的,不适用于位运算. #define _CRT_SECURE_NO_WARNINGS #include ...

  3. 全国计算机等级考试二级教程-C语言程序设计_第9章_数组

    四维数组,可用于航天卫星,三维+时间 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...

  4. 全国计算机等级考试二级教程-C语言程序设计_第14章_结构体、共用体和用户定义类型

    函数的返回值是结构体类型 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct ...

  5. 全国计算机等级考试二级教程-C语言程序设计_第10章_字符串

    字符型指针数组 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> //参数中,int a ...

  6. 全国计算机等级考试二级教程-C语言程序设计_第5章_循环结构

    for循环结构的嵌套 外层循环每循环一次,内层循环会完整循环一次. 外层循环是竖. 内层循环是横. for, do...while, while的选择: 如果有固定次数,如阶乘! ,判断素数,用 fo ...

  7. 全国计算机等级考试二级教程-C语言程序设计_第3章_顺序结构

    1输入两个整数给变量x和y:然后输出x和y:在交换x和y中的值后,在输出x和y. #include <stdio.h> main() { int x, y, t; printf(" ...

  8. 全国计算机等级考试二级教程-C语言程序设计_第2章_C程序设计的初步知识

    正负号与被除数一致. 3 % (-5) == 3 (-3) % 5 == -3 不用求余运算符,求出余数. int x, y; 答:x - x / y * y; const int i = 10; c ...

  9. 全国计算机等级考试二级教程-C语言程序设计_第7章_函数

    函数执行,从右到左执行 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> main() ...

随机推荐

  1. UVA 10152-ShellSort(映射+栈)

    题意: 给出一堆乌龟名字,乌龟能从本身位置爬到顶端. 要求求出从原本的顺序到目标顺序的最小操作.输出每次操作移到顶端的乌龟的名字. 解析:名字用映射对应编号,把目标状态的乌龟从上到下的编号按1到N编好 ...

  2. 过程化开发2048智力游戏WebApp

    时间荏苒,唯编程与青春不可辜负,感觉自己一直没有专心去提升编程的技能,甚是惭愧!!! 周五,无意间看到一个开发2048的视频,有点兴趣就动起手来了,虽然不擅长前端开发,在此献丑,分享一下自己使用过程化 ...

  3. Python IDE的选择和安装

    安装好Python后我们需要选择合适自己的IDE进行学习,虽然利用python默认的编辑器,或者直接文档编辑也可以进行基础的学习,但总归不是太方便,能够开发python项目的IDE很多,如sublim ...

  4. HBase--DependentColumnFilter(参考例过滤器 )详解

    DependentColumnFilter是一种允许用户指定一个参考列或引用列来过滤其他列的过滤器,过滤的原则是基于参考列的时间戳来进行筛选 . 官方说明: 大意:此过滤器提供两个参数--列族和列限定 ...

  5. Android_Fragment_Fragment具体解释

    Android_Fragment_Fragment具体解释 分类: Android基础2013-10-03 08:23 92人阅读 评论(0) 收藏 举报 AndroidFragmentFragmen ...

  6. java学习笔记day01

    1.Java JDK:简称为java开发工具集 2.下载JDK后安装,可以下载绿色版本,即不用安装,直接将其放在磁盘根目录  如:C:\Java\jdk1.6.0_10 3.在任意磁盘路径下都可以编译 ...

  7. PC和ARM平台编译Qt的命令

    编译for PC 的Qt过程是: (1)qmake -project (qmake命令,用于创建hello.pro,将所有的文件编译成一个与平台无关的工程文件).(注意:按照前面步骤安装好Qt环境之后 ...

  8. 新生赛(2) problem 2 丁磊养猪

    Problem B Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  9. sql 中的时间处理问题

    select GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName(month,GetDate()) as '月',DateName ...

  10. C#钩子应用实例

    C#钩子应用实例一.写在最前 本文的内容只想以最通俗的语言说明钩子的使用方法,具体到钩子的详细介绍可以参照下面的网址: http://www.microsoft.com/china/community ...