C/C++/C# 中的switch case 比较
2011-05-13 16:01
369 查看
C
codeblocks+gcc
输出结果为:周一 周二 其它
当将nWeek = 2
输出结果为:周二 其他
理解:switch 通过nWeek的值找到入口标号,然后从这个标号开始顺序往下执行
可以通过在每一个case的结尾添加break语句来实现跳出,加有break语句的case类似else if 功能。
C++
与C相同
C#
每个case下面必须有break语句,除非该case下面为空语句,否则出错。
codeblocks+gcc
#include <stdio.h> #include <stdlib.h> int main() { int nWeek = 1; switch (nWeek) { case 1: printf("周一 "); case 2: printf("周二 "); default: printf("其它 "); break; } return 0; }
输出结果为:周一 周二 其它
当将nWeek = 2
输出结果为:周二 其他
理解:switch 通过nWeek的值找到入口标号,然后从这个标号开始顺序往下执行
可以通过在每一个case的结尾添加break语句来实现跳出,加有break语句的case类似else if 功能。
C++
与C相同
C#
每个case下面必须有break语句,除非该case下面为空语句,否则出错。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ImplicitTypeVariable { class Program { static void Main(string[] args) { int nWeek = 1; switch (nWeek) { case 1: Console.Write("周一 "); break; //break 必不可少,除非case 下面是空的语句 case 2: Console.Write("周二 "); break; default: Console.Write("其它 "); break; } Console.ReadKey(); } } }
相关文章推荐
- C/C++/C# 中的switch case 比较
- C++ 和 C# 之间的比较
- abstract virtual 在 C++和C#中的比较
- C# 与 C++ 数据类型比较及结构体转换
- C# 和本机 C++(它不使用 /clr)之间的重要比较信息(二)
- C# 与 C++ 数据类型比较及结构体转换
- Java,C#,C++在继承,覆盖和多态,抽象类等几个方面的比较归纳
- C#与C++数据类型比较及结构体转换(搜集整理二)
- C++、Java和C#的特性比较
- Visual Basic、C# 和 C++ 的数据类型比较(转)
- C#和java,C#和C++各有什么不同(2) -- 语言规范的比较
- 实例比较C#与C++字符串操作
- abstract virtual 在 C++和C#中的比较
- [转载] C++和C#“事件”比较
- .NET 3.5 SP1的发布让C#和C++的性能比较问题有了定论
- C#与C++的比较
- Visual Basic、C# 和 C++ 的数据类型比较(转)
- 比较C++和C#
- C# 与 C 和 C++ 比较
- C++、Java和C#的特性比较