c语言中的switch函数

wwc386646959 2012-03-01 09:29:27
c语言中的switch函数怎么用啊,求人指点。
...全文
1294 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
czzdcn123 2012-03-12
  • 打赏
  • 举报
回复
楼主你可以结贴了。。楼上的都讲的挺详细的
鲁邦 2012-03-12
  • 打赏
  • 举报
回复
switch(表达式)
{
case 表达式值1: 处理一
case 表达式值2: 处理二
。。。
}
lys86_1205 2012-03-12
  • 打赏
  • 举报
回复
重要的要记得case :后面要常量表达式
breaksss 2012-03-12
  • 打赏
  • 举报
回复
通常不写break是不对的
mymtom 2012-03-08
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 mymtom 的回复:]

至于break; 根据需要使用。
需要记住的是:无论是case 还是 default 都只是标记,
[/Quote]
因为case 和 default 都是标记下面的程序才能运行

send(to, from, count)
register short *to, *from;
register count;
{
register n = (count + 7) / 8;
switch(count % 8) {
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while(--n > 0);
}
}
mymtom 2012-03-08
  • 打赏
  • 举报
回复
至于break; 根据需要使用。
需要记住的是:无论是case 还是 default 都只是标记,
JackyRao 2012-03-08
  • 打赏
  • 举报
回复
这种问题不该在这里问吧
mymtom 2012-03-08
  • 打赏
  • 举报
回复
switch 是语句 (statement)
case 和 default 是标记(label)
简单来说,
switch (表达式) {
case 常量1:
case 常量2:
default:
}

如果表达式与 case 标记的常量相等,就跳转到对应的标记,
如果表达式与所有case 标记常量都不等而且有default标记的话,跳转到default标记。
标号的顺序是任意的。

英文原文如下:
A switch statement causes control to jump to, into, or past the statement that is the switch body, depending on the value of a controlling expression, and on the presence of a default label and the values of any case labels on or in the switch body. A case or default label is accessible only within the closest enclosing switch statement.

The integral promotions are performed on the controlling expression. The constant expression in each case label is converted to the promoted type of the controlling expression. If a converted value matches that of the promoted controlling expression, control jumps to the statement following the matched case label. Otherwise, if there is a default label, control jumps to the labeled statement. If no converted case constant expression matches and there is no default label, no part of the switch body is executed.
lbq199204 2012-03-08
  • 打赏
  • 举报
回复
上面已经很多人举过例子了,这里我就不举了,就提醒一下,switch(x)
X的取值只能是字符型和整型。
wizard_tiger 2012-03-08
  • 打赏
  • 举报
回复
LS都说了学习一下。
zz432524 2012-03-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 joeblackzqq 的回复:]
int main()
{
int s = 10;

switch(s) // s相当于是条件语句 当s = 10的时候 执行 case 10:printf("10");break;
// 当s = 20的时候 执行语句printf("20"); 执行完之后 结束switch语句 也就是break的作用
{
case 10:printf("10");break;
caes 20:printf("20");break;
default:printf("errot!");
}
return 0;
}

[/Quote]

嘿嘿 懒的打字 改了1楼的
  • 打赏
  • 举报
回复
当然,也可以这么用

int i = 5;
switch(i)
{
case 1: case 3: case 5:
printf("qi");
break;
case 2: case 4: case 6:
printf("ou");
break;
default:
printf("other > 6");
}
Defonds 2012-03-08
  • 打赏
  • 举报
回复
多摸索
啊kun 2012-03-08
  • 打赏
  • 举报
回复
。。这个楼主该你自己看书了,感觉你连switch有什么用都不知道。。
Xomic 2012-03-05
  • 打赏
  • 举报
回复
楼主,那些说switch不是函数的,这是你在这篇帖子上最该学到知识点!
v风雪山神庙v 2012-03-05
  • 打赏
  • 举报
回复
switch不是函数,注意
加哥 2012-03-05
  • 打赏
  • 举报
回复
菜单语句嘛,
switch()
{
case ‘ ’: ;break;
case。。。。。。。。




default:
printf(“error”);
exit(0);
}
‘’中为switch括号中的内容,判断一不一样,一样就执行case中:后的语句,break根据实际需要加,break是在执行了case:的那语句后直接跳出switch。
EmbeddedLong 2012-03-05
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 zhao4zhong1 的回复:]
switch(整形变量)
[/Quote]

字符 也可以的啊
strugglelif 2012-03-02
  • 打赏
  • 举报
回复 1
这个真的很基础了,楼主要加油看基础类型的书啊!
qixing1115 2012-03-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 czh3642210 的回复:]
以上是switch-case的正规写法,default语句总是写在最后。但是,如果把default语句间在了case的中间,执行的结果又是怎样的呢?笔者测试了一下几种有代表性的情况,把结果简单罗列如下:
A. 每个语句中break齐整

C/C++ code
switch(c)
{
case '1':
printf("1/n");
brea……
[/Quote]

挺详细的,switch好像不是函数是一种……叫做啥来着
加载更多回复(16)

70,038

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧