intage=6;System.out.println("Hello!");if(age<13){System.out.println("I'm a child.");}if(age>20){System.out.println("I'm an adult.");}System.out.println("Bye!");
代码部分 3.22 的输出
Hello!
I'm a child
Bye!
如果 if 代码块之后只执行一条语句,则无需将其括在花括号中。例如,if (i == 0) i = 1; 是 Java 代码的有效部分。这适用于大多数控制结构,例如 else 和 while。但是,Oracle 的 Java 代码约定 明确规定应始终使用花括号。
当需要检查多个条件时,可以使用 else-if 代码块。else-if 语句位于 if 代码块之后,但在 else 代码块之前。if 代码块的结构如下所示
if (boolean expression1) {
statement1.1
statement1.2
...
statementn
} else if (boolean expression2) {
statement2.1
statement2.2
...
statement2.n
} else {
statement3.1
statement3.2
...
statement3.n
}
以下是一个说明示例
代码清单 3.3:MyConditionalProgram.java
publicclassMyConditionalProgram{publicstaticvoidmain(String[]args){inta=5;if(a>0){// a is greater than 0, so this statement will executeSystem.out.println("a is positive");}elseif(a>=0){// a case has already executed, so this statement will NOT executeSystem.out.println("a is positive or zero");}else{// a case has already executed, so this statement will NOT executeSystem.out.println("a is negative");}}}
代码清单 3.3 的输出
a is positive
请记住,只执行一个代码块,它将是第一个为真的条件。
当遇到 if 时,会评估所有条件,无论条件的结果如何,在执行 if 代码块之后。
代码部分 3.23:变量 a 的新值。
inta=5;if(a>0){// a is greater than 0, so this statement will executeSystem.out.println("a is positive");a=-5;}elseif(a<0){// a WAS greater than 0, so this statement will not executeSystem.out.println("a is negative");}else{// a does not equal 0, so this statement will not executeSystem.out.println("a is zero");}
inti=3;switch(i){case1:// i doesn't equal 1, so this code won't executeSystem.out.println("i equals 1");break;case2:// i doesn't equal 2, so this code won't executeSystem.out.println("i equals 2");break;default:// i has not been handled so far, so this code will executeSystem.out.println("i equals something other than 1 or 2");}
代码部分 3.26 的输出
i equals something other than 1 or 2
如果 case 没有以 break 语句结尾,则将检查下一个 case,否则执行将跳转到 switch 语句的末尾。
查看此示例以了解如何执行
代码部分 3.27:一个包含没有 break 的 case 的 switch 代码块。
inti=-1;switch(i){case-1:case1:// i is -1, so it will fall through to this case and execute this codeSystem.out.println("i is 1 or -1");break;case0:// The break command is used before this case, so if i is 1 or -1, this will not executeSystem.out.println("i is 0");}
Dayday=Day.MONDAY;// Day is a fictional enum type containing the days of the weekswitch(day){caseMONDAY:// Since day == Day.MONDAY, this statement will executeSystem.out.println("Mondays are the worst!");break;caseTUESDAY:caseWEDNESDAY:caseTHURSDAY:System.out.println("Weekdays are so-so.");break;caseFRIDAY:caseSATURDAY:caseSUNDAY:System.out.println("Weekends are the best!");break;}
Stringday="Monday";switch(day){case"Monday":// Since day == "Monday", this statement will executeSystem.out.println("Mondays are the worst!");break;case"Tuesday":case"Wednesday":case"Thursday":System.out.println("Weekdays are so-so.");break;case"Friday":case"Saturday":case"Sunday":System.out.println("Weekends are the best!");break;default:thrownewIllegalArgumentException("Invalid day of the week: "+day);}