天天看點

Java循環結構與條件語句

循環結構

while

while( 布爾表達式 ) {
  // 循環體
}
      

隻要布爾表達式為True,就會一直反複執行循環體。

示例:

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");
      }
   }
}
      

do while

do {
       // 循環體
}while( 布爾表達式 );
      

無論表達式是否為True,都先執行一次循環體,然後就跟while一樣先判斷布爾表達式,如果為True再繼續執行循環,為False就退出循環。

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 );
   }
}
      

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");
      }
   }
}
      

Java也有更友善從數組周遊元素的for循環:

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(",");
      }
   }
}
      

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;  // x等于30時跳出循環,後面都不列印了
         }
System.out.print( x );
System.out.print("\n");
      }
   }
}
      

continue

跳過目前這次循環,執行下一次循環。

public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};

for(int x : numbers ) {
if( x == 30 ) {
continue;  // 不會列印30,但是會繼續列印後面元素
         }
System.out.print( x );
System.out.print("\n");
      }
   }
}
      

break和continue可以從字面意思來區分,break中斷循環,continue繼續下次循環。

條件語句

if

if(布爾表達式)
{
   //如果布爾表達式為true将執行的語句
}
      
public class Test {

public static void main(String args[]){
int x = 10;

if( x < 20 ){
System.out.print("這是 if 語句");
      }
   }
}
      

if else

if(布爾表達式){
   //如果布爾表達式的值為true
}else{
   //如果布爾表達式的值為false
}
      
public class Test {

public static void main(String args[]){
int x = 30;

if( x < 20 ){
System.out.print("這是 if 語句");
      }else{
System.out.print("這是 else 語句");
      }
   }
}
      

也可以跟多個if else:

if(布爾表達式 1){
   //如果布爾表達式 1的值為true執行代碼
}else if(布爾表達式 2){
   //如果布爾表達式 2的值為true執行代碼
}else if(布爾表達式 3){
   //如果布爾表達式 3的值為true執行代碼
}else {
   //如果以上布爾表達式都不為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("這是 else 語句");
      }
   }
}
      

嵌套的if else

if(布爾表達式 1){
   ////如果布爾表達式 1的值為true執行代碼
   if(布爾表達式 2){
      ////如果布爾表達式 2的值為true執行代碼
   }
}
      
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");
          }
       }
    }
}
      

switch case

switch(expression){
    case value :
       //語句
       break; //可選
    case value :
       //語句
       break; //可選
    //你可以有任意數量的case語句
    default : //可選
       //語句
}
      
  • expression:變量或傳回變量的方法,變量類型可以是byte、short、int或char,以及String類型。
  • value:字元串常量或字面量,且與表達式的變量類型相同。
  • break:可選,有break時會中斷後續比對跳出switch語句,沒有break時會繼續執行後面的case。
  • default:當所有case都沒有比對到時,會執行default語句,一般放在最後的位置。
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("優秀"); 
break;
case 'B' :
case 'C' :
System.out.println("良好");
break;
case 'D' :
System.out.println("及格");
break;
case 'F' :
System.out.println("你需要再努力努力");
break;
default :
System.out.println("未知等級");
      }
System.out.println("你的等級是 " + grade);
   }
}
      

參考資料:

​​https://www.runoob.com/java/java-loop.html​​

​​https://www.runoob.com/java/java-if-else-switch.html​​

​​https://www.runoob.com/java/java-switch-case.html​​

所有文章公衆号首發!

如果你覺得這篇文章寫的還不錯的話,關注公衆号“dongfanger”,你的支援就是我寫文章的最大動力。

Java循環結構與條件語句

版權申明:本文為部落客原創文章,轉載請保留原文連結及作者。