In PHP, the do-while loop is a variation of the while loop that guarantees the code inside the loop will run at least once, regardless of the condition.
This is because the condition is evaluated after the code block has been executed.
This makes the do-while loop useful when you need to ensure that the loop body executes at least once before checking the condition.
In this tutorial, we will cover:
Let’s dive into each of these concepts with examples and explanations.
1. What is a do-while Loop?
A do-while loop in PHP is a control structure that repeatedly executes a block of code at least once, and then continues to execute the code as long as the condition remains true.
Unlike the while loop, which checks the condition before the first iteration, the do-while loop checks the condition after the first iteration, ensuring that the loop executes at least one time.
2. Basic Syntax of do-while Loop
The do-while loop consists of two parts:
- The code block, which is executed once unconditionally.
- The condition, which is checked after the code block runs.
Syntax:
do { // Code to execute at least once } while (condition);
- condition: The expression that is evaluated after the block of code is executed. If it evaluates to true, the loop runs again; if false, the loop terminates.
3. How the do-while Loop Works
- The code inside the do block is executed once.
- After the code block is executed, the condition is evaluated.
- If the condition is true, the loop starts again; if false, the loop terminates.
- This guarantees the code inside the do block will always execute at least once.
4. Examples of Using the do-while Loop
Example 1: Simple do-while Loop
<?php $count = 1; do { echo "Count: $count\n"; $count++; } while ($count <= 5); ?>
Output:
Count: 1 Count: 2 Count: 3 Count: 4 Count: 5
- In this example, the do-while loop prints the value of $count starting from 1. The loop continues as long as $count <= 5. After each iteration, $count is incremented by 1.
Example 2: Loop with Condition that Initially Fails
<?php $count = 10; do { echo "Count: $count\n"; $count++; } while ($count <= 5); ?>
Output:
Count: 10
- In this example, the condition $count <= 5 is false from the beginning, but the do-while loop still executes once before terminating. This demonstrates that the code inside the do block is executed at least once, even when the condition is initially false.
Example 3: User Input with do-while Loop
<?php $number = 0; do { $number = (int) readline("Enter a positive number: "); } while ($number <= 0); echo "You entered: $number\n"; ?>
- In this example, the user is repeatedly asked to enter a positive number until they do so. The loop ensures that the prompt is shown at least once, even if the user enters invalid input multiple times.
5. Difference Between while and do-while Loops
while Loop:
- The condition is checked first, before the code block is executed.
- If the condition is false initially, the loop may not execute at all.
do-while Loop:
- The code block is executed first, and then the condition is checked.
- The loop always executes at least once, even if the condition is false.
Example: Comparison Between while and do-while Loops
<?php $count = 10; // While Loop while ($count <= 5) { echo "This will not print in while loop.\n"; } // Do-While Loop do { echo "This will print once in do-while loop.\n"; } while ($count <= 5); ?>
Output:
This will print once in do-while loop.
- In this example, the while loop does not execute because the condition is false from the start. However, the do-while loop runs once before checking the condition.
6. Common Use Cases for do-while Loop
The do-while loop is useful in situations where you want the loop to run at least once, regardless of the condition.
Some common use cases include:
- User input validation: You might want to keep asking the user for valid input until they provide it.
- Menus: In a command-line application, a menu might need to display at least once, even if the user immediately chooses to exit.
- Repeating a task until a condition is met: For example, retrying an operation until it succeeds.
Example: Input Validation with do-while
<?php $password = ""; do { $password = readline("Enter your password (at least 6 characters): "); } while (strlen($password) < 6); echo "Password accepted!\n"; ?>
- In this example, the do-while loop ensures that the user is asked for a password repeatedly until they enter a string with at least 6 characters.
7. Using break and continue in do-while Loop
break Statement:
The break statement is used to exit the loop early, even if the condition has not yet been evaluated as false.
Example (Using break in do-while Loop):
<?php $count = 1; do { echo "Count: $count\n"; if ($count >= 3) { break; // Exit the loop when count reaches 3 } $count++; } while ($count <= 5); ?>
Output:
Count: 1 Count: 2 Count: 3
- In this example, the loop is terminated early using the break statement when $count reaches 3, even though the condition would allow it to continue until 5.
continue Statement:
The continue statement skips the remaining code in the current iteration and moves on to the next iteration.
Example (Using continue in do-while Loop):
<?php $count = 0; do { $count++; if ($count == 3) { continue; // Skip the rest of this iteration when count is 3 } echo "Count: $count\n"; } while ($count <= 5); ?>
Output:
Count: 1 Count: 2 Count: 4 Count: 5
- In this example, the continue statement skips the output for when $count is 3. The loop continues to the next iteration without printing the value 3.
Summary of PHP do-while Loop:
Concept | Description |
---|---|
Basic Syntax | The loop runs at least once before checking the condition. |
Condition Checked After Loop | The condition is evaluated after each iteration, allowing for at least one run. |
Guaranteed Execution | The code inside the do block is always executed once, even if the condition is false. |
Using break | The break statement can be used to exit the loop early. |
Using continue | The continue statement skips the remaining code in the current iteration and proceeds to the next iteration. |
Difference from while Loop | The while loop checks the condition first, whereas the do-while loop checks the condition after the first execution. |
Conclusion
The do-while loop in PHP is a valuable tool when you need to ensure that a block of code runs at least once, regardless of the condition.
In this tutorial, we covered:
- The syntax and workings of the do-while loop.
- Several examples to illustrate how the do-while loop works with different conditions.
- The differences between while and `do-while loops.
- How to use the break and continue statements within a do-while loop to control execution.