Java和C++中的switch语句
2012-03-16 23:32
531 查看
在Java和C++中,switch语句表示有多种条件可供选择,在Java和C++中用法基本相同。
switch语句的格式如下:
switch (expression - E){
case constant - expression -1: statement -1;break;
case constant - expression -2: statement -2;
case constant - expression -3: statement -3;return;
default:throw something;
}
对于switch语句中的每个case,要用break,return或者throw来进行结束。
下面以Java中的一个程序为例,来说明switch语句的用法和break,return,throw使用注意事项。
switch语句的格式如下:
switch (expression - E){
case constant - expression -1: statement -1;break;
case constant - expression -2: statement -2;
case constant - expression -3: statement -3;return;
default:throw something;
}
对于switch语句中的每个case,要用break,return或者throw来进行结束。
下面以Java中的一个程序为例,来说明switch语句的用法和break,return,throw使用注意事项。
public class Switch { public static void main(String args[]){ int currentValue=Integer.parseInt(args[0]); int colour=currentValue; switch (colour){ case 0:System.out.println("red"); break; case 1:System.out.println("amber"); return; case 2:System.out.println("green"); default:System.out.println("error"); } System.out.println("out of switch but still in main()"); } }
相关文章推荐
- C++程序员学Java系列之十一:流程控制语句switch,case,break,default
- Java和C++中switch语句的区别
- C/C++/Java for语句规范写法浅析(二重及以上循环)
- java switch语句的一个例子
- 尚硅谷 第三课全 java-变量类型转换-字符与字符串-算数运算符-赋值运算符-三元运算符-.switch语句-循环结构-while 循环语句 do-while 循环语句
- Java开关语句-switch
- Java温故知新 - Switch语句
- Java中的switch语句后面的控制表达式的数据类型
- JAVA-switch 选择语句
- JAVA编写元音字母(Switch语句)
- Java If、Switch 语句
- java 利用Java if和switch语句实现判断闰年
- java入门-枚举类型与for/switch语句
- Java基础之switch语句
- java中,switch语句的陷阱
- 更深入一点理解 switch 语句 及 c/c++ 对 const 的处理
- C++ switch语句(多选分支结构)
- JAVA学习笔记4——if语句+switch语句+while语句
- [转载]精解C++的switch语句
- java中switch语句的测试