循環控制
可能存在一種情況,當我們需要執行的代碼塊數次,通常被稱為一個循環。
Java有非常靈活的三循環機制??梢允褂靡韵氯N循環之一:
- while 循環
- do...while 循環
- for 循環
截至Java5,對增強的for循環進行了介紹。這主要是用于數組。
while 循環
while循環是一個控制結構,可以重復的特定任務次數。
語法
while循環的語法是:
1
2
3
4
|
while (Boolean_expression) { //Statements } |
在執行時,如果布爾表達式的結果為真,則循環中的動作將被執行。只要該表達式的結果為真,執行將繼續下去。
在這里,while循環的關鍵點是循環可能不會永遠運行。當表達式進行測試,結果為假,循環體將被跳過,在while循環之后的第一個語句將被執行。
示例
1
2
3
4
5
6
7
8
9
10
11
12
|
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" ); } } } |
這將產生以下結果:
1
2
3
4
5
6
7
8
9
10
|
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19 |
do...while 循環
do ... while循環類似于while循環,不同的是一個do ... while循環是保證至少執行一次。
語法
do...while循環的語法是:
1
2
3
4
|
do { //Statements } while (Boolean_expression); |
請注意,布爾表達式出現在循環的結尾,所以在循環中的語句執行前一次布爾測試。
如果布爾表達式為真,控制流跳回,并且在循環中的語句再次執行。這個過程反復進行,直到布爾表達式為假。
示例
1
2
3
4
5
6
7
8
9
10
11
12
|
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 ); } } |
這將產生以下結果:
1
2
3
4
5
6
7
8
9
10
|
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19 |
for 循環
for循環是一個循環控制結構,可以有效地編寫需要執行的特定次數的循環。
知道一個任務要重復多少次的時候,for循環是有好處的。
語法
for循環的語法是:
1
2
3
4
|
for (initialization; Boolean_expression; update) { //Statements } |
下面是一個for循環的控制流程:
初始化步驟首先被執行,并且僅一次。這個步驟可聲明和初始化任何循環控制變量。不需要把一個聲明放在這里,只需要一個分號出現。
接下來,布爾表達式求值。如果是 true,則執行循環體。如果是false,則循環體不執行, 并且流程控制的跳轉到經過for循環的下一個語句。
之后循環體在for循環執行時,控制流程跳轉備份到更新語句。該語句允許更新任何循環控制變量。這個語句可以留空,只要一個分號出現在布爾表達式之后。
布爾表達式現在再次評估計算。如果是true,循環執行,并重復這個過程(循環體,然后更新的步驟,然后布爾表達式)。之后,布爾表達式為 false,則循環終止。
示例
1
2
3
4
5
6
7
8
9
10
|
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" ); } } } |
這將產生以下結果:
1
2
3
4
5
6
7
8
9
10
|
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19 |
for 循環在 Java 中新特性
截至Java5,對增強的for循環進行了介紹。這主要是用于數組。
語法
增強的for循環的語法是:
1
2
3
4
|
for (declaration : expression) { //Statements } |
聲明: 新聲明塊變量,這是一種與你所正在訪問數組中的元素兼容的變量。該變量在for塊內可被利用并且它的值作為當前的數組元素將是相同的。
表達: 這個計算結果完成需要循環數組。表達式可以是一個數組變量或返回一個數組的方法調用。
示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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( "," ); } } } |
這將產生以下結果:
1
2
|
10,20,30,40,50, James,Larry,Tom,Lacy, |
break 關鍵字
關鍵字break是用來停止整個循環的。 break關鍵字必須使用于任何循環中或一個switch語句中。
關鍵字break將停止最內層循環的執行,并開始執行在塊之后的下一行代碼。
語法
break語法是任何循環中一個單獨的語句:
示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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" ); } } } |
這將產生以下結果:
1
2
|
10 20 |
continue 關鍵字
continue關鍵字可以在任一環的控制結構使用。它使循環立即跳轉到循環的下一次迭代.
在for循環中,continue關鍵字會導致控制流立即跳轉到更新語句。
在一個while循環或do/while循環,控制流立即跳轉到布爾表達式。
語法
continue 語法是任何循環中一個單獨的語句:
示例
1
2
3
4
5
6
7
8
9
10
11
12
|
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" ); } } } |
這將產生以下結果:
1
2
3
4
|
10 20 40 50 |
條件判斷
在 Java 中有兩種類型的條件判斷語句,它們分別是:
- if 語句
- switch 語句
if 語句:
if 語句由一個布爾表達式后跟一個或多個語句組成。
語法
if 語句的語法是:
1
2
3
4
|
if (Boolean_expression) { //Statements will execute if the Boolean expression is true } |
如果布爾表達式的值為 true,那么代碼里面的塊 if 語句將被執行。如果不是 true,在 if 語句(大括號后)結束后的第一套代碼將被執行。
示例
1
2
3
4
5
6
7
8
9
10
|
public class Test { public static void main(String args[]){ int x = 10 ; if ( x < 20 ){ System.out.print( "This is if statement" ); } } } |
這將產生以下結果:
1
|
This is if statement |
if...else 語句
任何 if 語句后面可以跟一個可選的 else 語句,當布爾表達式為 false,語句被執行。
語法
if...else 的語法是:
1
2
3
4
5
|
if (Boolean_expression){ //Executes when the Boolean expression is true } else { //Executes when the Boolean expression is false } |
示例
1
2
3
4
5
6
7
8
9
10
11
12
|
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" ); } } } |
這將產生以下結果:
1
|
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 的語法是:
1
2
3
4
5
6
7
8
9
|
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. } |
示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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" ); } } } |
這將產生以下結果:
1
|
Value of X is 30 |
嵌套 if...else 語句
它始終是合法的嵌套 if-else 語句,這意味著你可以在另一個 if 或 else if 語句中使用一個 if 或 else if 語句。
語法
嵌套 if...else 的語法如下:
1
2
3
4
5
6
|
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。
示例
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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" ); } } } } |
這將產生以下結果:
1
|
X = 30 and Y = 10 |
switch 語句
switch 語句允許一個變量來對一系列值得相等性進行測試。每個值被稱為一 case,并且被啟動的變量會為每一個 case 檢查。
語法
增強的 for 循環的語法是:
1
2
3
4
5
6
7
8
9
10
11
|
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 語句中使用的變量只能是一個字節,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。
示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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); } } |
編譯并運行上面使用各種命令行參數的程序。這將產生以下結果:
1
2
3
|
$ java Test Well done Your grade is a C |