分支结构¶
if
语句switch
语句
可以嵌套
if…else语句¶
if 语句后面可以跟 else 语句, 当 if 语句的布尔表达式值为 false 时, else 语句块会被执行。
if(布尔表达式){
//如果布尔表达式的值为true
}else{
//如果布尔表达式的值为false
}
if…else if…else¶
使用 if, else if, else 语句的时候, 需要注意下面几点
- if 语句至多有 1 个
else
语句,else
语句在所有的else if
语句之后。 - if 语句可以有若干个
else if
语句, 它们必须在else
语句之前。 - 一旦其中一个
else if
语句检测为true
, 其他的else if
以及else
语句都将跳过执行。
if(布尔表达式 1){
//如果布尔表达式 1的值为true执行代码
}else if(布尔表达式 2){
//如果布尔表达式 2的值为true执行代码
}else if(布尔表达式 3){
//如果布尔表达式 3的值为true执行代码
}else {
//如果以上布尔表达式都不为true执行代码
}
switch 语句¶
switch 语句判断一个变量与一系列值中某个值是否相等, 每个值称为一个分支。
switch(expression){
case value :
//语句
break; //可选
case value :
//语句
break; //可选
//你可以有任意数量的case语句
default : //可选
//语句
}
switch 语句有如下规则
- switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始, switch 支持字符串类型了, 同时 case 标签必须为字符串常量或字面量。
- switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。
- case 语句中的值的数据类型必须与变量的数据类型相同, 而且只能是常量或者字面常量。
- 当变量的值与 case 语句的值相等时, 那么 case 语句之后的语句开始执行, 直到 break 语句出现才会跳出 switch 语句。
- 当遇到 break 语句时, switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现, 程序会继续执行下一条 case 语句, 直到出现 break 语句。
- switch 语句可以包含一个 default 分支, 该分支必须是 switch 语句的最后一个分支。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。
实例
public class Test {
public static void main(String args[]){
//char grade = args[0].charAt(0);
char grade = 'C';
switch(grade)
{
case 'A' :
System.out.println("优秀");
break;
case 'B' :
case 'C' :
System.out.println("良好");
break;
case 'D' :
System.out.println("及格");
case 'F' :
System.out.println("你需要再努力努力");
break;
default :
System.out.println("未知等级");
}
System.out.println("你的等级是 " + grade);
}
}