循環控制
可能存在一種情況,當我們需要執行的代碼塊數次,通常被稱為一個循環。
Java有非常靈活的三循環機制。可以使用以下三種循環之一:
截至Java5,對增強的for循環進行了介紹。這主要是用於數組。
while 循環
while循環是一個控制結構,可以重複的特定任務次數。
文法
while循環的語法是:
while(Boolean_expression){ //Statements}在執行時,如果布爾表達式的結果為真,則循環中的動作將被執行。只要該表達式的結果為真,執行將繼續下去。
在這裡,while循環的關鍵點是循環可能不會永遠運行。當表達式進行測試,結果為假,循環體將被跳過,在while循環之後的第一個語句將被執行。
示例
public class Test { public static void main(String args[]) { int x = 10; while( x < 20 ) { System.out.print("value of x : " + x ); x++; System.out.print("/n"); } }}這將產生以下結果:
value of x : 10value of x : 11value of x : 12value of x : 13value of x : 14value of x : 15value of x : 16value of x : 17value of x : 18value of x : 19
do...while 循環
do ... while循環類似於while循環,不同的是一個do ... while循環是保證至少執行一次。
文法
do...while循環的語法是:
do{ //Statements} while (Boolean_expression);請注意,布爾表達式出現在循環的結尾,所以在循環中的語句執行前一次佈爾測試。
如果布爾表達式為真,控制流跳回,並且在循環中的語句再次執行。這個過程反復進行,直到布爾表達式為假。
示例
public class Test { public static void main(String args[]){ int x = 10; do{ System.out.print("value of x : " + x ); x++; System.out.print("/n"); }while( x < 20 ); }}這將產生以下結果:
value of x : 10value of x : 11value of x : 12value of x : 13value of x : 14value of x : 15value of x : 16value of x : 17value of x : 18value of x : 19
for 循環
for循環是一個循環控制結構,可以有效地編寫需要執行的特定次數的循環。
知道一個任務要重複多少次的時候,for循環是有好處的。
文法
for循環的語法是:
for(initialization; Boolean_expression; update){ //Statements}下面是一個for循環的控制流程:
初始化步驟首先被執行,並且僅一次。這個步驟可聲明和初始化任何循環控制變量。不需要把一個聲明放在這裡,只需要一個分號出現。
接下來,布爾表達式求值。如果是true,則執行循環體。如果是false,則循環體不執行, 並且流程控制的跳轉到經過for循環的下一個語句。
之後循環體在for循環執行時,控制流程跳轉備份到更新語句。該語句允許更新任何循環控制變量。這個語句可以留空,只要一個分號出現在布爾表達式之後。
布爾表達式現在再次評估計算。如果是true,循環執行,並重複這個過程(循環體,然後更新的步驟,然後布爾表達式)。之後,布爾表達式為false,則循環終止。
示例
public class Test { public static void main(String args[]) { for(int x = 10; x < 20; x = x+1) { System.out.print("value of x : " + x ); System.out.print("/n"); } }}這將產生以下結果:
value of x : 10value of x : 11value of x : 12value of x : 13value of x : 14value of x : 15value of x : 16value of x : 17value of x : 18value of x : 19
for 循環在Java 中新特性
截至Java5,對增強的for循環進行了介紹。這主要是用於數組。
文法
增強的for循環的語法是:
for(declaration : expression){ //Statements}聲明: 新聲明塊變量,這是一種與你所正在訪問數組中的元素兼容的變量。該變量在for塊內可被利用並且它的值作為當前的數組元素將是相同的。
表達: 這個計算結果完成需要循環數組。表達式可以是一個數組變量或返回一個數組的方法調用。
示例
public class Test { public static void main(String args[]){ int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ){ System.out.print( x ); System.out.print(","); } System.out.print("/n"); String [] names ={"James", "Larry", "Tom", "Lacy"}; for( String name : names ) { System.out.print( name ); System.out.print(","); } }}這將產生以下結果:
10,20,30,40,50,James,Larry,Tom,Lacy,
break 關鍵字
關鍵字break是用來停止整個循環的。 break關鍵字必須使用於任何循環中或一個switch語句中。
關鍵字break將停止最內層循環的執行,並開始執行在塊之後的下一行代碼。
文法
break語法是任何循環中一個單獨的語句:
複製代碼代碼如下:break
示例
public class Test { public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ) { if( x == 30 ) { break; } System.out.print( x ); System.out.print("/n"); } }}這將產生以下結果:
1020
continue 關鍵字
continue關鍵字可以在任一環的控制結構使用。它使循環立即跳轉到循環的下一次迭代.
在for循環中,continue關鍵字會導致控制流立即跳轉到更新語句。
在一個while循環或do/while循環,控制流立即跳轉到布爾表達式。
文法
continue 語法是任何循環中一個單獨的語句:
複製代碼代碼如下:continue
示例
public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ) { if( x == 30 ) { continue; } System.out.print( x ); System.out.print("/n"); } }}這將產生以下結果:
10204050
條件判斷
在Java 中有兩種類型的條件判斷語句,它們分別是:
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...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 }}因為我們有嵌套的if 語句,所以可以用類似的方式嵌套else if...else。
示例
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 語句:
示例
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); }}編譯並運行上面使用各種命令行參數的程序。這將產生以下結果:
$ java TestWell doneYour grade is a C