全面掌握J(rèn)ava中的循環(huán)控制語句與條件判斷語句的使用
循環(huán)控制
可能存在一種情況,當(dāng)我們需要執(zhí)行的代碼塊數(shù)次,通常被稱為一個(gè)循環(huán)。
Java有非常靈活的三循環(huán)機(jī)制。可以使用以下三種循環(huán)之一:
- while 循環(huán)
- do...while 循環(huán)
- for 循環(huán)
截至Java5,對增強(qiáng)的for循環(huán)進(jìn)行了介紹。這主要是用于數(shù)組。
while 循環(huán)
while循環(huán)是一個(gè)控制結(jié)構(gòu),可以重復(fù)的特定任務(wù)次數(shù)。
語法
while循環(huán)的語法是:
while(Boolean_expression) { //Statements }
在執(zhí)行時(shí),如果布爾表達(dá)式的結(jié)果為真,則循環(huán)中的動(dòng)作將被執(zhí)行。只要該表達(dá)式的結(jié)果為真,執(zhí)行將繼續(xù)下去。
在這里,while循環(huán)的關(guān)鍵點(diǎn)是循環(huán)可能不會(huì)永遠(yuǎn)運(yùn)行。當(dāng)表達(dá)式進(jìn)行測試,結(jié)果為假,循環(huán)體將被跳過,在while循環(huán)之后的第一個(gè)語句將被執(zhí)行。
示例
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"); } } }
這將產(chǎn)生以下結(jié)果:
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 循環(huán)
do ... while循環(huán)類似于while循環(huán),不同的是一個(gè)do ... while循環(huán)是保證至少執(zhí)行一次。
語法
do...while循環(huán)的語法是:
do { //Statements } while (Boolean_expression);
請注意,布爾表達(dá)式出現(xiàn)在循環(huán)的結(jié)尾,所以在循環(huán)中的語句執(zhí)行前一次布爾測試。
如果布爾表達(dá)式為真,控制流跳回,并且在循環(huán)中的語句再次執(zhí)行。這個(gè)過程反復(fù)進(jìn)行,直到布爾表達(dá)式為假。
示例
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 ); } }
這將產(chǎn)生以下結(jié)果:
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 循環(huán)
for循環(huán)是一個(gè)循環(huán)控制結(jié)構(gòu),可以有效地編寫需要執(zhí)行的特定次數(shù)的循環(huán)。
知道一個(gè)任務(wù)要重復(fù)多少次的時(shí)候,for循環(huán)是有好處的。
語法
for循環(huán)的語法是:
for(initialization; Boolean_expression; update) { //Statements }
下面是一個(gè)for循環(huán)的控制流程:
初始化步驟首先被執(zhí)行,并且僅一次。這個(gè)步驟可聲明和初始化任何循環(huán)控制變量。不需要把一個(gè)聲明放在這里,只需要一個(gè)分號(hào)出現(xiàn)。
接下來,布爾表達(dá)式求值。如果是 true,則執(zhí)行循環(huán)體。如果是false,則循環(huán)體不執(zhí)行, 并且流程控制的跳轉(zhuǎn)到經(jīng)過for循環(huán)的下一個(gè)語句。
之后循環(huán)體在for循環(huán)執(zhí)行時(shí),控制流程跳轉(zhuǎn)備份到更新語句。該語句允許更新任何循環(huán)控制變量。這個(gè)語句可以留空,只要一個(gè)分號(hào)出現(xiàn)在布爾表達(dá)式之后。
布爾表達(dá)式現(xiàn)在再次評(píng)估計(jì)算。如果是true,循環(huán)執(zhí)行,并重復(fù)這個(gè)過程(循環(huán)體,然后更新的步驟,然后布爾表達(dá)式)。之后,布爾表達(dá)式為 false,則循環(huán)終止。
示例
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"); } } }
這將產(chǎn)生以下結(jié)果:
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 循環(huán)在 Java 中新特性
截至Java5,對增強(qiáng)的for循環(huán)進(jìn)行了介紹。這主要是用于數(shù)組。
語法
增強(qiáng)的for循環(huán)的語法是:
for(declaration : expression) { //Statements }
聲明: 新聲明塊變量,這是一種與你所正在訪問數(shù)組中的元素兼容的變量。該變量在for塊內(nèi)可被利用并且它的值作為當(dāng)前的數(shù)組元素將是相同的。
表達(dá): 這個(gè)計(jì)算結(jié)果完成需要循環(huán)數(shù)組。表達(dá)式可以是一個(gè)數(shù)組變量或返回一個(gè)數(shù)組的方法調(diào)用。
示例
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(","); } } }
這將產(chǎn)生以下結(jié)果:
10,20,30,40,50, James,Larry,Tom,Lacy,
break 關(guān)鍵字
關(guān)鍵字break是用來停止整個(gè)循環(huán)的。 break關(guān)鍵字必須使用于任何循環(huán)中或一個(gè)switch語句中。
關(guān)鍵字break將停止最內(nèi)層循環(huán)的執(zhí)行,并開始執(zhí)行在塊之后的下一行代碼。
語法
break語法是任何循環(huán)中一個(gè)單獨(dú)的語句:
示例
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"); } } }
這將產(chǎn)生以下結(jié)果:
10 20
continue 關(guān)鍵字
continue關(guān)鍵字可以在任一環(huán)的控制結(jié)構(gòu)使用。它使循環(huán)立即跳轉(zhuǎn)到循環(huán)的下一次迭代.
在for循環(huán)中,continue關(guān)鍵字會(huì)導(dǎo)致控制流立即跳轉(zhuǎn)到更新語句。
在一個(gè)while循環(huán)或do/while循環(huán),控制流立即跳轉(zhuǎn)到布爾表達(dá)式。
語法
continue 語法是任何循環(huán)中一個(gè)單獨(dú)的語句:
示例
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"); } } }
這將產(chǎn)生以下結(jié)果:
10 20 40 50
條件判斷
在 Java 中有兩種類型的條件判斷語句,它們分別是:
- if 語句
- switch 語句
if 語句:
if 語句由一個(gè)布爾表達(dá)式后跟一個(gè)或多個(gè)語句組成。
語法
if 語句的語法是:
if(Boolean_expression) { //Statements will execute if the Boolean expression is true }
如果布爾表達(dá)式的值為 true,那么代碼里面的塊 if 語句將被執(zhí)行。如果不是 true,在 if 語句(大括號(hào)后)結(jié)束后的第一套代碼將被執(zhí)行。
示例
public class Test { public static void main(String args[]){ int x = 10; if( x < 20 ){ System.out.print("This is if statement"); } } }
這將產(chǎn)生以下結(jié)果:
This is if statement
if...else 語句
任何 if 語句后面可以跟一個(gè)可選的 else 語句,當(dāng)布爾表達(dá)式為 false,語句被執(zhí)行。
語法
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"); } } }
這將產(chǎn)生以下結(jié)果:
This is else statement
if...else if...else 語句
if 后面可以跟一個(gè)可選的 else if...else 語句,在測試不同條件下單一的 if 語句和 else if 語句是非常有用的。
當(dāng)使用 if , else if , else 語句時(shí)有幾點(diǎn)要牢記。
- 一個(gè) if 語句可以有0個(gè)或一個(gè) else 語句 且它必須在 else if 語句的之后。
- 一個(gè) if 語句 可以有0個(gè)或多個(gè) else if 語句且它們必須在 else 語句之前。
- 一旦 else if 語句成功, 余下 else if 語句或 else 語句都不會(huì)被測試執(zhí)行。
語法
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"); } } }
這將產(chǎn)生以下結(jié)果:
Value of X is 30
嵌套 if...else 語句
它始終是合法的嵌套 if-else 語句,這意味著你可以在另一個(gè) if 或 else if 語句中使用一個(gè) 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 } }
因?yàn)槲覀冇星短椎?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"); } } } }
這將產(chǎn)生以下結(jié)果:
X = 30 and Y = 10
switch 語句
switch 語句允許一個(gè)變量來對一系列值得相等性進(jìn)行測試。每個(gè)值被稱為一 case,并且被啟動(dòng)的變量會(huì)為每一個(gè) case 檢查。
語法
增強(qiáng)的 for 循環(huán)的語法是:
switch(expression){ case value : //Statements break; //optional case value : //Statements break; //optional //You can have any number of case statements. default : //Optional //Statements }
以下規(guī)則適用于 switch 語句:
- 在 switch 語句中使用的變量只能是一個(gè)字節(jié),short,int 或 char。
- 在一個(gè) switch 語句中可以有任何數(shù)量的 case 語句。每個(gè) case 后跟著即將被比較的值和一個(gè)冒號(hào)。
- 對于 case 的值必須是相同的數(shù)據(jù)類型作為開關(guān)變量,它必須是一個(gè)常量或文字。
- 當(dāng)被啟動(dòng)了的變量與 case 是相等的,那 case 后的語句將執(zhí)行,一直到 break 為止。
- 當(dāng)達(dá)到一個(gè) break 語句,switch 終止,并且控制流跳轉(zhuǎn)到跟著 switch 語句的下一行。
- 不是每一個(gè) case 需要包含一個(gè) break。如果沒有出現(xiàn) break,控制流將貫穿到后面的 case 直到 break 為止。
- switch 語句可以有一個(gè)可選默認(rèn) case ,它必須出現(xiàn)在 switch 的結(jié)束處。在執(zhí)行一項(xiàng)任務(wù)時(shí)沒有任何 case 是真,那默認(rèn) case 可被使用。在默認(rèn) 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); } }
編譯并運(yùn)行上面使用各種命令行參數(shù)的程序。這將產(chǎn)生以下結(jié)果:
$ java Test Well done Your grade is a C
相關(guān)文章
hibernate-validator后端表單數(shù)據(jù)校驗(yàn)的使用示例詳解
這篇文章主要介紹了hibernate-validator后端表單數(shù)據(jù)校驗(yàn)的使用,hibernate-validator提供的校驗(yàn)方式為在類的屬性上加入相應(yīng)的注解來達(dá)到校驗(yàn)的目的,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08Java Web使用POI導(dǎo)出Excel的方法詳解
這篇文章主要介紹了Java Web使用POI導(dǎo)出Excel的方法,結(jié)合實(shí)例形式詳細(xì)分析了Java Web使用POI導(dǎo)出Excel的具體操作步驟、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-06-06Maven中Junit測試@Test等注解無法識(shí)別的問題及解決
這篇文章主要介紹了Maven中Junit測試@Test等注解無法識(shí)別的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11spring cloud-給Eureka Server加上安全的用戶認(rèn)證詳解
這篇文章主要介紹了spring cloud-給Eureka Server加上安全的用戶認(rèn)證詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01Java實(shí)現(xiàn)的圖片高質(zhì)量縮放類定義與用法示例
這篇文章主要介紹了Java實(shí)現(xiàn)的圖片高質(zhì)量縮放類定義與用法,涉及java針對圖片的運(yùn)算與轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Java終止循環(huán)體的具體實(shí)現(xiàn)
這篇文章主要介紹了Java終止循環(huán)體的具體實(shí)現(xiàn),需要的朋友可以參考下2014-02-02Spring Cloud 服務(wù)網(wǎng)關(guān)Zuul的實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud 服務(wù)網(wǎng)關(guān)Zuul的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07