Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 196
The do/while Statement
ОглавлениеThe second form a while
loop can take is called a do/while loop, which, like a while
loop, is a repetition control structure with a termination condition and statement, or a block of statements, as shown in Figure 3.6.
FIGURE 3.6 The structure of a do
/while
statement
Unlike a while
loop, though, a do
/while
loop guarantees that the statement or block will be executed at least once. For example, what is the output of the following statements?
int lizard = 0; do { lizard++; } while(false); System.out.println(lizard); // 1
Java will execute the statement block first and then check the loop condition. Even though the loop exits right away, the statement block is still executed once, and the program prints 1
.