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. 2014第一周五开发问题记URL传参乱码等

    今天修改了页面中URL传中文参数乱码问题,本来远离通过在tomcat中配置URIEncoder是可以解决所有乱码问题的,但怕以后有人下载一个新的tomcat然后直接把程序放里面运行然后再发现乱码问题而 ...

  2. 8个华丽的HTML5相册动画欣赏

    HTML5的图片动画非常丰富,我们也在网站上分享过很多关于HTML5的图片动画.相册在网络中也十分常见,本文我们要分享一些比较华丽的jQuery/HTML5相册动画,希望大家喜欢. 1.HTML5 3 ...

  3. iOS学习之iOS沙盒(sandbox)机制和文件操作(一)

    1.iOS沙盒机制 iOS应用程序仅仅能在为该改程序创建的文件系统中读取文件,不能够去其他地方訪问,此区域被成为沙盒,所以全部的非代码文件都要保存在此,比如图像,图标,声音,映像,属性列表,文本文件等 ...

  4. net 2.0使用ajax

    asp.net ajax中用到了几个dll文件,这些可以从网上下载.http://ajax.asp.net站点下可以找到相关的下载.这其中包括:System.Web.Extensions.dll.Sy ...

  5. 将json格式日期(毫秒数)转成日常日期格式和日常格式时间对比

    第一:是把生成的Json格式的时间转换,注意要看清楚时间的格式 function (cellval) { var date = new Date(parseInt(cellval.replace(&q ...

  6. ASP.NET属性之AssociatedControlID

    AssociatedControlID 是用在Asp.Net 中的 Label 控件上.给label控件关联一个ASP.NET的控件,在点击 这个 LABEL的时候,所关联的ASP.NET控件会获得焦 ...

  7. 【转】获取CID 和 LAC的方法

    原文地址:http://stackoverflow.com/questions/13399659/get-cellid-mcc-mnc-lac-and-network-in-ios-5-1 在iOS5 ...

  8. C#调用SAPWebService

    其实和调用其它WebService 没有很大不同  只是不了解SAP的人 可能不太明白 SAP接口中的相关参数 //调用接口 , 创建对象 ServiceReference1.Z_IF_MM_VEND ...

  9. HDU 2604 - Queuing

    长度为 n 有男有女的队伍里没有 fmf 和 fff 的序列有多少 判断最后一个人无法得出结论 于是判断最后两人的递推式: fm(n) =   mm(n-1) //最后两人为fm的长度为n的队伍 只能 ...

  10. 从汇编看c++初始化列表初始化成员变量

    简略来说,编译器会对初始化列表按照成员变量的声明顺序重新一一排序,安插到构造函数中进行初始化操作,而且这些初始化操作在构造函数里面用户自己定义的任何代码之前. 下面是c++源码: class X { ...