Definition and Usage
- PHP Version
- 4+
The do...while statement consists of a code block and a condition.
It first executes the code block once unconditionally, then creates a loop that executes the code block while the condition is true.
When the condition becomes false, the do...while statement terminates.
A loop refers to a structure in which a program repeatedly performs the same task until a specific condition is met.
The term comes from the idea that the structure is connected like a loop.
Basic Example
$i = 1;
do { /* Code block ({}) */
// Execute the code block first, then evaluate the condition
echo $i . ' ';
$i++;
} while ($i <= 5); // Repeat the code block if the condition is true
// Output: 1 2 3 4 5
Syntax
do {
code block
} while (condition);
code block
The code block is executed once unconditionally when the do...while statement starts.
After that, if the condition evaluated after executing the code block is true, the code block is executed again.
condition
The condition that is evaluated after the code block is executed to determine whether to execute the code block again. It cannot be omitted.
- If this condition evaluates to
true, thecode blockis executed. - If this condition evaluates to
false, thedo...whilestatement is no longer executed and terminates. - Omitting the
conditionresults in a syntax error.
Execution Order of the do...while Statement
When the do...while statement is executed, it follows the sequence below.
- When the
do...whilestatement starts, thecode blockis executed once unconditionally. - The
conditionis evaluated.
If theconditionevaluates totrue, thecode blockis executed in the next step. However, if it evaluates tofalse, thedo...whilestatement is no longer executed and terminates. - If the
conditionevaluates totrue, thecode blockis executed. - After the
code blockis executed, the process returns to the second step, where theconditionis evaluated. These subsequent steps are then repeated.
The following example code explains the execution process.
$i = 1; // Initialize the variable $i to 1
do {
echo $i . ' '; // Output the current value of the variable $i
$i++; // Increment the value of the variable $i by 1
} while ($i <= 5);
// Output: 1 2 3 4 5
- When the
do...whilestatement starts, thecode blockis executed once unconditionally.echo $i . ' 'is executed, outputting1, and$ibecomes2due to$i++. - The
conditionis evaluated. Since the condition$i <= 5evaluates totrue, the loop continues. - This process is repeated until
$ibecomes6. In other words,1,2,3,4, and5are output in order. - When
$ibecomes6, the condition$i <= 5becomesfalse, and the loop terminates. In other words, thedo...whilestatement is no longer executed and terminates.
Using with the break Statement
The break statement exits the current loop and terminates it completely.
$i = 1;
do {
if ($i == 3) { // Terminate the loop if $i is 3
break;
}
echo $i . ' ';
$i++;
} while ($i <= 5);
// Output: 1 2
Using with the continue Statement
The continue statement skips the remaining part of the current iteration and immediately starts the next iteration.
$i = 1;
do {
if ($i == 3) {
$i++; // Increment $i to prevent an infinite loop
continue; // Skip the current iteration and move to the next iteration
}
echo $i . ' ';
$i++;
} while ($i <= 5);
// Output: 1 2 4 5