如何使用 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

更新時間: 25-2-2020

190 次觀看

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.