全面掌握Java中的循環(huán)控制語句與條件判斷語句的使用
循環(huán)控制
可能存在一種情況,當我們需要執(zhí)行的代碼塊數(shù)次,通常被稱為一個循環(huán)。
Java有非常靈活的三循環(huán)機制??梢允褂靡韵氯N循環(huán)之一:
- while 循環(huán)
- do...while 循環(huán)
- for 循環(huán)
截至Java5,對增強的for循環(huán)進行了介紹。這主要是用于數(shù)組。
while 循環(huán)
while循環(huán)是一個控制結構,可以重復的特定任務次數(shù)。
語法
while循環(huán)的語法是:
while(Boolean_expression)
{
//Statements
}
在執(zhí)行時,如果布爾表達式的結果為真,則循環(huán)中的動作將被執(zhí)行。只要該表達式的結果為真,執(zhí)行將繼續(xù)下去。
在這里,while循環(huán)的關鍵點是循環(huán)可能不會永遠運行。當表達式進行測試,結果為假,循環(huán)體將被跳過,在while循環(huán)之后的第一個語句將被執(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)生以下結果:
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),不同的是一個do ... while循環(huán)是保證至少執(zhí)行一次。
語法
do...while循環(huán)的語法是:
do
{
//Statements
} while (Boolean_expression);
請注意,布爾表達式出現(xiàn)在循環(huán)的結尾,所以在循環(huán)中的語句執(zhí)行前一次布爾測試。
如果布爾表達式為真,控制流跳回,并且在循環(huán)中的語句再次執(zhí)行。這個過程反復進行,直到布爾表達式為假。
示例
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)生以下結果:
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)是一個循環(huán)控制結構,可以有效地編寫需要執(zhí)行的特定次數(shù)的循環(huán)。
知道一個任務要重復多少次的時候,for循環(huán)是有好處的。
語法
for循環(huán)的語法是:
for(initialization; Boolean_expression; update)
{
//Statements
}
下面是一個for循環(huán)的控制流程:
初始化步驟首先被執(zhí)行,并且僅一次。這個步驟可聲明和初始化任何循環(huán)控制變量。不需要把一個聲明放在這里,只需要一個分號出現(xiàn)。
接下來,布爾表達式求值。如果是 true,則執(zhí)行循環(huán)體。如果是false,則循環(huán)體不執(zhí)行, 并且流程控制的跳轉到經(jīng)過for循環(huán)的下一個語句。
之后循環(huán)體在for循環(huán)執(zhí)行時,控制流程跳轉備份到更新語句。該語句允許更新任何循環(huán)控制變量。這個語句可以留空,只要一個分號出現(xiàn)在布爾表達式之后。
布爾表達式現(xiàn)在再次評估計算。如果是true,循環(huán)執(zhí)行,并重復這個過程(循環(huán)體,然后更新的步驟,然后布爾表達式)。之后,布爾表達式為 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)生以下結果:
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,對增強的for循環(huán)進行了介紹。這主要是用于數(shù)組。
語法
增強的for循環(huán)的語法是:
for(declaration : expression)
{
//Statements
}
聲明: 新聲明塊變量,這是一種與你所正在訪問數(shù)組中的元素兼容的變量。該變量在for塊內(nèi)可被利用并且它的值作為當前的數(shù)組元素將是相同的。
表達: 這個計算結果完成需要循環(huán)數(shù)組。表達式可以是一個數(shù)組變量或返回一個數(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)生以下結果:
10,20,30,40,50, James,Larry,Tom,Lacy,
break 關鍵字
關鍵字break是用來停止整個循環(huán)的。 break關鍵字必須使用于任何循環(huán)中或一個switch語句中。
關鍵字break將停止最內(nèi)層循環(huán)的執(zhí)行,并開始執(zhí)行在塊之后的下一行代碼。
語法
break語法是任何循環(huán)中一個單獨的語句:
示例
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)生以下結果:
10 20
continue 關鍵字
continue關鍵字可以在任一環(huán)的控制結構使用。它使循環(huán)立即跳轉到循環(huán)的下一次迭代.
在for循環(huán)中,continue關鍵字會導致控制流立即跳轉到更新語句。
在一個while循環(huán)或do/while循環(huán),控制流立即跳轉到布爾表達式。
語法
continue 語法是任何循環(huán)中一個單獨的語句:
示例
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)生以下結果:
10 20 40 50
條件判斷
在 Java 中有兩種類型的條件判斷語句,它們分別是:
- if 語句
- switch 語句
if 語句:
if 語句由一個布爾表達式后跟一個或多個語句組成。
語法
if 語句的語法是:
if(Boolean_expression)
{
//Statements will execute if the Boolean expression is true
}
如果布爾表達式的值為 true,那么代碼里面的塊 if 語句將被執(zhí)行。如果不是 true,在 if 語句(大括號后)結束后的第一套代碼將被執(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)生以下結果:
This is if statement
if...else 語句
任何 if 語句后面可以跟一個可選的 else 語句,當布爾表達式為 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)生以下結果:
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 語句都不會被測試執(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)生以下結果:
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");
}
}
}
}
這將產(chǎn)生以下結果:
X = 30 and Y = 10
switch 語句
switch 語句允許一個變量來對一系列值得相等性進行測試。每個值被稱為一 case,并且被啟動的變量會為每一個 case 檢查。
語法
增強的 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 語句中使用的變量只能是一個字節(jié),short,int 或 char。
- 在一個 switch 語句中可以有任何數(shù)量的 case 語句。每個 case 后跟著即將被比較的值和一個冒號。
- 對于 case 的值必須是相同的數(shù)據(jù)類型作為開關變量,它必須是一個常量或文字。
- 當被啟動了的變量與 case 是相等的,那 case 后的語句將執(zhí)行,一直到 break 為止。
- 當達到一個 break 語句,switch 終止,并且控制流跳轉到跟著 switch 語句的下一行。
- 不是每一個 case 需要包含一個 break。如果沒有出現(xiàn) break,控制流將貫穿到后面的 case 直到 break 為止。
- switch 語句可以有一個可選默認 case ,它必須出現(xiàn)在 switch 的結束處。在執(zhí)行一項任務時沒有任何 case 是真,那默認 case 可被使用。在默認 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);
}
}
編譯并運行上面使用各種命令行參數(shù)的程序。這將產(chǎn)生以下結果:
$ java Test Well done Your grade is a C
相關文章
java 利用java反射機制動態(tài)加載類的簡單實現(xiàn)
下面小編就為大家?guī)硪黄猨ava 利用java反射機制動態(tài)加載類的簡單實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09
踩坑之spring事務,非事務方法與事務方法執(zhí)行相互調(diào)用方式
這篇文章主要介紹了踩坑之spring事務,非事務方法與事務方法執(zhí)行相互調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
spring boot-2.1.16整合swagger-2.9.2 含yml配置文件的代碼詳解
這篇文章主要介紹了spring boot-2.1.16整合swagger-2.9.2 含yml配置文件,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
詳解Java?SSM項目部署上線配置方法(阿里云服務器ECS?+?云數(shù)據(jù)庫RDS?MySQL)(寶塔)
這篇文章主要介紹了Java?SSM項目部署上線(阿里云服務器ECS?+?云數(shù)據(jù)庫RDS?MySQL)(寶塔)的圖文教程,本文通過圖文并茂的形式給大家介紹的非常詳細,感興趣的朋友一起看看吧2024-01-01

