JAVA - 程序流程控制
任何語言要想運行,都必須定義相應的流程控制規則,開發者按照規則實現相應的算法與功能,就像加減乘除的運算優先級,那麼Java中的流程控制有哪些那?又是怎麼執行的,總結如下:
流程控制的分類:
1、順序
2、分支
3、循環
順序:
順序執行,表示逐行執行相應的語句,不涉及任何的關鍵字。
舉例 1:(順序執行)
輸出:public class Test { public static void main(String[] args) { int i = 1; System.out.println("我是第 " + (i++) + " 行"); System.out.println("我是第 " + (i++) + " 行"); System.out.println("我是第 " + (i++) + " 行"); System.out.println("我是第 " + (i++) + " 行"); System.out.println("我是第 " + (i++) + " 行"); } }
我是第 1 行 我是第 2 行 我是第 3 行 我是第 4 行 我是第 5 行
分支:
分支控制,則需要按照判斷條件動態的去執行符合條件的語句,需要使用關鍵字:
【if】 ,【else】 , 【三目:condition?true:false】,switch case: break default;
舉例 1:(分支控制)
<pre name="code" class="java">public class Test { public static void main(String[] args) { People man = new People(true, 7000f); // 1. use if else if (man.getSex()) { System.out.println("this is a man"); } else { System.out.println("this is a female"); } // 2. use condition ? true :false float salary = man.getSalary(); String 薪資級別 = salary > 1000f ? "高" : "低"; System.out.println(薪資級別); } } class People { private Boolean sex;// true is man ,false is female private float salary; public People(boolean sex, float salary) { this.sex = sex; this.salary = salary; } public float getSalary() { return salary; } public Boolean getSex() { return sex; } }
輸出:this is a man 高
switch case: break default;
對於Switch語句的介紹,只能說盡量少用,只用於解決多分支情況時的過度if else重複邏輯,在Switch可以使用的值有char Enum 字符串(JDK 7),使用時要注意兩點:
1、每個case一定要跟上break,有些邏輯需要可能出現 case 1--> case 2 break; 這種需要特別的注意
2、必須指定default分支
注意:在JDK 1.7中支持了String ,方便了許多。如:
public class Test { public static void main(String[] args) { String name = "b"; int value = 0; switch(name) { case "a": value = 1; break; case "b": value = 2; break; case "c": value = 3; break; case "d": value = 4; break; case "e": value = 5; break; default: value = 6; } System.out.println(value); } }
循環
循環控制,則需要按照循環判斷條件動態的去執行循環語句,需要使用關鍵字:
【for / foreach】【while / do……while】【標記】【continue】【break】
for 循環使用方法:
1、原for循環
for(初始化語句;判斷語句;附加執行語句){ //附加執行語句可以不寫,或者寫在循環語句,主要用來對於循環條件的漸變
循環執行語句(可以配合【continue & break】控制循環)
}
2、新for循環
for(遍歷單個元素:元素集合){
循環執行語句(可以配合【continue & break】控制循環)
}
舉例 1: (for 循環控制)
public class Test { public static void main(String[] args) { int[] intArr = new int[10]; int j = 0; float z = 0; String s = ""; for (int i = 0; i < intArr.length; i++,j++,z--,s=""+i) {// 1、原for循環,i++可以不寫,或者寫在intArr[i]= i + 1;下一行,至於j++,z-- 這句也可以寫,可能在可讀性上就差了許多 intArr[i] = i + 1; if(i==5){ continue;//進行下次循環,後面的語句不執行,後面的執行語句 【i++,j++,z--,s=""+i --> i < intArr.length; --> intArr[i] = i + 1;】 } if(i==7){ break; } System.out.print( "j = " + j + " ,"); System.out.println( "z = " + z +" ,"); } for (int i : intArr) {// 2、新for循環,不可以寫附加執行語句 System.out.print(i + " ,"); } } }
輸出:
1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,
while 循環使用方法:
1、while 循環
while(循環判斷條件){
循環語句(可以配合【continue & break】控制循環)
}
舉例 1:(while / do ... while 循環)
public class Test { public static void main(String[] args) { boolean flag = false; //1、while 語句 while (flag) { System.out.println("while"); break; } System.out.println("--------------------------"); //2、do while 語句 do { System.out.println("do ... while"); break; } while (flag);// 注意while(flag)屬於判斷語句,不在屬於循環體,所以必須加上“;”結束該語句。 } }
輸出:
1、while 首先它回去判斷while中的條件,條件不符合則不執行循環語句(循環體)。-------------------------- do ... while
2、do……while,首先它不會去判斷while中的條件,而是直接執行循環語句,輸出了第一元素,再去判斷while中的條件。如果符合條件再次進入循環語句。
循環中標記的使用方法:
標記的定義:loop:循環語句
loop1:for(int i = 0;i<10;i++){
}
loop2:while(true){
}
注意:do...while 貌似不可以定義標記。舉例 1:(標記的使用)
public class Test { public static void main(String[] args) { int[] intArr = new int[] { 1, 2 }; int[] intArr2 = new int[] { 2, 3, 4 }; // 判斷intArr1中元素是否存在於intArr2 boolean result = false; loop: for (int i : intArr2) {// 定義標記loop 指向此行中的for語句段的結束,即執行的下一句是【System.out.println("\nresult = " +result);】 for (int j : intArr) { if (i == j) { result = true; <strong> break loop;</strong> } } System.out.print(i + " ,"); } System.out.println("\nresult = " + result); } }
注意:
標記只能定義在一個語句塊(for 或者 while循環條件行或者{……}),指定不再滿足條件,進行語句塊下面的語句,標記類於語句塊中的局部變量,所以可以重複定義,且語句塊外不可以使用該標記。