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);}