循環控制
可能存在一種情況,當我們需要執行的代碼塊數次,通常被稱為一個循環。
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語法是任何循環中一個單獨的語句:
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 語法是任何循環中一個單獨的語句:
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
新聞熱點
疑難解答