Java do-while 循环示例
java programming java8java technologies object oriented programming
do...while 循环与 while 循环类似,不同之处在于 do...while 循环保证至少执行一次。
语法
以下是 do...while 循环的语法 −
do { // 语句 }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