do is the keyword to create a do-while loop in Java. Its characteristics are: the loop condition is checked after the loop body is executed; no matter whether the condition is true or not, the loop body will be executed at least once; different from the while loop , the execution order is to execute the loop body first and then check the conditions.
What is do in Java?
do is a keyword in Java that is used to create do-while loops. A do-while loop is similar to a while loop, but its loop condition is checked after the loop body is executed.
Usage
<code class="java">do { // 循环体 } while (循环条件);</code>
Difference from while loop
The main difference between do-while loop and while loop is:
Example
<code class="java">int i = 0; do { System.out.println(i); i++; } while (i < 5);</code>
In this example, the loop body is executed once (output i is 0), and then the loop condition is checked (i < 5) . The loop condition is true, so the loop continues executing with outputs i being 1, 2, 3, and 4. When i equals 5, the loop condition becomes false and the loop terminates.
The above is the detailed content of What is the meaning and usage of do in java. For more information, please follow other related articles on the PHP Chinese website!