如何在Java中使用“ do while循环”?

一个做...而循环类似于while循环" title="while循环">while循环,不同的是do ... while循环,保证至少执行一次。

语法

以下是do ... while循环的语法-

do {

   //声明

}while(Boolean_expression);

注意,布尔表达式出现在循环的末尾,因此循环中的语句在测试布尔之前执行一次。
如果布尔表达式为true,则控件跳回do语句,然后循环中的语句再次执行。重复此过程,直到布尔表达式为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 );

   }

}


输出结果

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

以上是 如何在Java中使用“ do while循环”? 的全部内容, 来源链接: utcz.com/z/316170.html

回到顶部