The do while statement checks the condition after executing the code block first, and does not stop execution until the condition is false. 1) Execute the code block; 2) Check the condition; 3) Continue to execute the code block if the condition is true, and jump out of the loop if the condition is false. The difference from the while statement is that the do while loop executes the code block at least once, while the while statement may not execute.
do while statement
do while
statement is a loop statement that starts with The code block is executed first, then the condition is checked for pattern, and the code block is executed until the condition is false.
Syntax
<code class="c">do { // 代码块 } while (条件);</code>
How it works
The do while
statement first executes the code block. Then, it checks whether the condition is true. If true, it continues executing the block of code; if false, it breaks out of the loop.
Example
<code class="c">int i = 0; do { printf("%d\n", i); i++; } while (i < 5);</code>
This code will print numbers from 0 to 4 because even though the initial value of i
is 0 (condition is false) , it will also execute the code block once. The difference between
and while statement
do while
statement and while
statement are: do while A
statement always executes a block of code at least once, while a while
statement may not execute a block of code at all.
Advantages
The advantages of the do while
statement are:
Disadvantages
The disadvantages of the do while
statement are:
The above is the detailed content of What does do while mean in c language?. For more information, please follow other related articles on the PHP Chinese website!