fall through switch的用法
来源:互联网 发布:招商证券交易软件下载 编辑:程序博客网 时间:2024/04/30 10:41
switch 在其合适的case中开始运行,一直到遇到break或者跳出switch。那么如果你的case中没有break,那就会出现所谓的fall through现象,就像是滑滑梯一样。
以下是一段示例程序,有助于你理解fall through:
#include "stdio.h"
int main(int argc, char* argv[])
{
printf("Hello World!/n");
int num_of_operands = 2;
//fall through 的设计可以把代码设计得很简约,
//以下的这个操作数处理是一个很好的例子,它可以很简约的去处理每一个操作数
//当然不得不说 switch 将 fall through 设计为默认操作是引起许多bug的原因
//session 1:
switch(num_of_operands){
case 2://process_operand(operator->operand_2);
case 1://process_operand(operator->operand_1);
default:;
}
//session 2:即使是default 没有break,一样会出现fall through现象
num_of_operands = 2;
switch(num_of_operands){
case 1:
printf("case 1 fall through/n");
default:
printf("default fall through/n");
//num_of_operands = 10;
case 3:
printf("case 2 fall through/n");
}
//conclusion :在使用switch时需要注意,如果是一般应用,请别忘了加上 break
return 0;
}
当然不得不提,fall through 也是很多时候你忘记break带来的bug。
- fall through switch的用法
- switch 结构的 fall through
- Switch Fall Through
- switch fall-through
- break的fall through
- fall through C语言的一个失误
- Fall through the cracks
- switch-case的用法
- java switch的用法
- switch的一点用法
- svn switch的用法
- switch的方便用法
- switch的神奇用法
- switch的用法
- SWITCH的特殊用法
- switch 的用法
- switch的用法
- svn switch的用法
- ANSI C 自动将相邻的字符串常量合并埋下的BUG
- 《深入理解Linux内核》--第七章:进程调度:读书笔记
- FastDFS源码阅读笔记(一)
- 天河排第一 操作系统是麒麟
- Couldn't find the kernel version the module was compiled for Linux 驱动 编译 错误
- fall through switch的用法
- JavaScript如何收集JSP页面的采集到的数据
- tcp/udp客户端连接secondary IP的情况
- 源码网(http://www.prjcode.com)是个神马网站!!!
- FastDFS源码阅读笔记(二)
- 查看QQ的端口号
- Firefox、Chrome跟IE的差别:对以"/>"结尾的标签的不同处理效果
- WinEyes的重新实现--windows系统及其消息机制
- 用友EAI二次开发