如何在 Java 中使用“do while 迴圈”?


do...while 迴圈類似於 while 迴圈,不同之處在於,無論如何會執行至少一次 do...while 迴圈。

語法

以下是 do...while 迴圈的語法 −

do {
   // Statements
}while(Boolean_expression);

請注意,布林表示式出現在迴圈的末尾,因此迴圈中的語句在測試布林值之前執行一次。
如果布林表示式為真,則控制權跳回到 do 語句,迴圈中的語句將再次執行。此過程會重複,直到布林表示式為假。

示例

public class Test {
   public static void main(String args[]) {
      int x = 10;
      do {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("
");       }while( x < 20 );    } }


輸出

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

更新時間:2020 年 2 月 25 日

189 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.