Java 编程中的控制语句详解
一、switch 语句
1.1 无 break 语句的执行情况
在 Java 中,若 switch
语句里没有 break
语句,程序执行会继续进入下一个 case
。例如以下情况:
i is less than three
i is less than four
i is less than five
i is less than three
i is less than four
i is less than five
i is less than four
i is less than five
i is less than five
1.2 空 case 的使用
switch
语句可以有空的 case
,像下面这个例子:
switch(i) {
case 1:
case 2:
case 3: System.out.println("i is 1, 2 or 3");
break;
case 4: System.out.println("i is 4");
break;
}
当 i
的值为 1、2 或 3 时,会执行第一个 println()
语句;若