以下內容引用自http://wiki.jikexueyuan.com/project/java/decision-making.html:
在 Java中有兩種類型的條件判斷語句,它們分別是:
- if語句
- switch語句
一、if 語句:
if語句由一個布爾表達式后跟一個或多個語句組成。
語法:
if語句的語法是:
if(Boolean_expression) { //Statements will execute if the Boolean expression is true }
如果布爾表達式的值為true,那么代碼里面的塊if語句將被執行。如果不是true,在if語句(大括號后)結束后的第一套代碼將被執行。
示例:
public class Test { public static void main(String args[]){ int x = 10; if( x < 20 ){ System.out.print("This is if statement"); } } } //這將產生以下結果: This is if statement
二、if...else語句
任何if語句后面可以跟一個可選的else語句,當布爾表達式為false,語句被執行。
語法:
if...else的語法是:
if(Boolean_expression){ //Executes when the Boolean expression is true }else{ //Executes when the Boolean expression is false }
示例:
public class Test { public static void main(String args[]){ int x = 30; if( x < 20 ){ System.out.print("This is if statement"); }else{ System.out.print("This is else statement"); } } } //這將產生以下結果: This is else statement
三、if...else if...else語句
if后面可以跟一個可選的else if...else語句,在測試不同條件下單一的if語句和else if語句是非常有用的。
當使用if,else if,else語句時有幾點要牢記。
- 一個if語句可以有0個或一個else語句 且它必須在else if語句的之后。
- 一個if語句可以有0個或多個else if語句且它們必須在else語句之前。
- 一旦else if語句成功, 余下else if語句或else語句都不會被測試執行。
語法:
if...else的語法是:
if(Boolean_expression 1){ //Executes when the Boolean expression 1 is true }else if(Boolean_expression 2){ //Executes when the Boolean expression 2 is true }else if(Boolean_expression 3){ //Executes when the Boolean expression 3 is true }else { //Executes when the none of the above condition is true. }
示例:
public class Test { public static void main(String args[]){ int x = 30; if( x == 10 ){ System.out.print("Value of X is 10"); }else if( x == 20 ){ System.out.print("Value of X is 20"); }else if( x == 30 ){ System.out.print("Value of X is 30"); }else{ System.out.print("This is else statement"); } } } //這將產生以下結果: Value of X is 30
四、嵌套if...else語句
它始終是合法的嵌套if else語句,這意味着可以在另一個if或else if語句中使用一個if或else if語句。
語法:
嵌套if...else的語法如下:
if(Boolean_expression 1){ //Executes when the Boolean expression 1 is true if(Boolean_expression 2){ //Executes when the Boolean expression 2 is true } }
示例:
public class Test { public static void main(String args[]){ int x = 30; int y = 10; if( x == 30 ){ if( y == 10 ){ System.out.print("X = 30 and Y = 10"); } } } } //這將產生以下結果: X = 30 and Y = 10
五、switch語句
switch語句允許一個變量來對一系列值得相等性進行測試。每個值被稱為case,並且被啟動的變量會為每一個case檢查。
語法:
增強的for循環的語法是:
switch(expression){ case value : //Statements break; //optional case value : //Statements break; //optional //You can have any number of case statements. default : //Optional //Statements }
以下規則適用於switch語句:
- 在switch語句中使用的變量只能是一個byte,short,int或char。
- 在一個switch語句中可以有任何數量的case語句。每個case后跟着即將被比較的值和一個冒號。
- 對於case的值必須是相同的數據類型作為開關變量,它必須是一個常量或文字。
- 當被啟動了的變量與case是相等的,那case后的語句將執行,一直到break為止。
- 當達到一個break語句,switch終止,並且控制流跳轉到跟着switch語句的下一行。
- 不是每一個case需要包含一個break。如果沒有出現break,控制流將貫穿到后面的case直到break為止。
- switch語句可以有一個可選默認case,它必須出現在switch的結束處。在執行一項任務時沒有任何case是真,那默認case可被使用。在默認case中不需要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("Excellent!"); break; case 'B' : case 'C' : System.out.println("Well done"); break; case 'D' : System.out.println("You passed"); case 'F' : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); } System.out.println("Your grade is " + grade); } } //編譯並運行上面使用各種命令行參數的程序。這將產生以下結果: Well done Your grade is a C
測試工程:https://github.com/easonjim/5_java_example/tree/master/javabasicstest/test6
