Java 简明教程
Java - do…while Loop
Java do while Loop
一个 do while 循环类似于 while loop ,只不过 do while 循环保证至少执行一次。
A do while loop is similar to a while loop, except that a do while loop is guaranteed to execute at least one time.
do-while 循环是一个退出控制循环,其中在执行循环主体后检查条件。
The do-while loop is an exit control loop, where the condition is checked after executing the loop’s body.
Syntax of do while Loop
以下是 do…while 循环的语法:
Following is the syntax of a do…while loop −
do {
// Statements
}while(Boolean_expression);
Execution Process of a do while Loop
注意 loop 末尾出现了布尔表达式,因此循环中的语句在布尔运算测试之前执行一次。
Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested.
如果布尔表达式为真,控制权就会跳转回 do 语句,循环中的语句会再次执行。此过程会重复运行,直到布尔表达式为假为止。
If the Boolean expression is true, the control jumps back up to do statement, and the statements in the loop execute again. This process repeats until the Boolean expression is false.
Flow Diagram
下图显示了 Java 中 do while 循环的流程图(执行过程) -
The following diagram shows the flow diagram (execution process) of a do while loop in Java -
do while Loop Examples
Example 1: Printing Numbers in a Range Using do while
在此示例中,我们展示了 while 循环的使用,以打印从 10 到 19 的数字。在这里,我们使用值 10 初始化了一个 int variable x。然后在 do while 循环中,我们将在执行 do while 循环主体后检查 x 是否小于 20。在 do while 循环主体中,我们打印 x 的值并将 x 的值递增 1。while 循环将一直运行,直到 x 变成 20。一旦 x 为 20,循环将停止执行,程序退出。
In this example, we’re showing the use of a while loop to print numbers starting from 10 to 19. Here we’ve initialized an int variable x with a value of 10. Then in do while loop, we’re checking x as less than 20 after do while loop body.In do while loop body we’re printing the value of x and incrementing the value of x by 1. While loop will run until x becomes 20. Once x is 20, loop will stop execution and program exits.
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
Example 2: Printing Elements of an Array Using do while
在此示例中,我们展示了 do while 循环的使用,以打印 array 的内容。在这里,我们创建一个整数数组作为数字,并为其初始化一些值。我们创建了一个名为 index 的变量来表示我们在迭代时数组的索引。在 do while 循环中,我们检查索引是否小于循环主体后数组的大小,并使用索引符号打印数组的元素。在循环主体中,索引变量会递增 1,并且循环一直持续到索引变为数组的长度,然后循环退出。
In this example, we’re showing the use of a do while loop to print contents of an array. Here we’re creating an array of integers as numbers and initialized it some values. We’ve created a variable named index to represent index of the array while iterating it. In do while loop we’re checking the index to be less than size of the array after loop body and printed the element of the array using index notation. Within loop body index variable is incremented by 1 and loop continues till index becomes the sie of the array and loop exits.
public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
int index = 0;
do {
System.out.print("value of item : " + numbers[index] );
index++;
System.out.print("\n");
} while( index < 5 );
}
}
value of item : 10
value of item : 20
value of item : 30
value of item : 40
value of item : 50
do while Infinite Loop in Java
还可以在 Java 中使用 do do-while 循环语句将“true”作为条件语句来实现无限循环。
An infinite loop can also be implemented by writing "true" as the conditional statement using do do-while loop statement in Java.
Example: Implementing an infinite do while Loop
在这个示例中,我们展示了使用 while 循环的无限循环。它将继续打印数字,直到你按 ctrl+c 终止程序。
In this example, we’re showing the infinite loop using while loop. It will keep printing the numbers until you press ctrl+c to terminate the program.
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( true );
}
}
value of item : 10
value of item : 20
value of item : 30
value of item : 40
value of item : 50
...
ctrl+c