In this article do-while loop in PHP we give the information about do-while loop is a loop in which the code block must run at least once, even if the condition is false. This is a little different from a while loop, because in do-while the condition is checked later.
do-while Loop in PHP:
Syntax of do-while Loop
do {
// This code must run at least once.
} while(condition);
Description of parts of syntax
- do Block:
It contains the code that runs first.
- Condition:
The condition is checked after the code is run. If the condition is true, the loop runs again.
Execution Process of do-while Loop
- First, the do block of code is run.
- After that, the condition is checked.
- If the condition is true, the loop runs again.
- This process continues until the condition becomes false.
Example 1: Printing numbers from 1 to 5
<?php
$i = 1; //initial value
do {
echo “Number: $i<br>”;
$i++; //increment the variable by 1
} while ($i<= 5);
?>
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Example 2: Printing Reverse Numbers from 10 to 1
<?php
$i = 10;
do {
echo “Number: $i<br>”;
$i–; //decrement the variable by 1
} while ($i >= 1);
?>
Output:
Number: 10
Number: 9
Number: 8
Number: 7
Number: 6
Number: 5
Number: 4
Number: 3
Number: 2
Number: 1
Example 3: Running code once even if condition is false
<?php
$i = 10;
do {
echo “This code must run once.<br>”;
} while ($i< 5); // condition is wrong
?>
Output:
This code will definitely run once.
Infinite Loop
If the condition is always true, the loop will continue an infinite number of times.
Example
<?php
$i = 1;
do {
echo “Number: $i<br>”;
// Forgot to increment $i, which will cause the loop to become infinite.
} while ($i<= 5);
?>
Stopping the loop (break statement)
break is used to end the loop midway.
Example
<?php
$i = 1;
do {
if ($i == 5) {
break; //The loop will stop at 5.
}
echo “Number: $i<br>”;
$i++;
} while ($i<= 10);
?>
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Skipping iteration (continue statement)
continue is used to skip the current iteration and move to the next.
Example
<?php
$i = 1;
do {
if ($i == 3) {
$i++; //Increment $i before moving on to the next iteration.
continue; // skip 3
}
echo “Number: $i<br>”;
$i++;
} while ($i<= 5);
?>
Output:
Number: 1
Number: 2
Number: 4
Number: 5
Tips for using do-while Loop
- To run the code at least once:
When you want to ensure that a code block will run at least once.
- Avoid Infinite Loop:
Make sure the variable is updated correctly and the condition is returned to false at the time.
- Use break and continue in Complex Logic:
Use these to maintain control in large and complex code.
Conclusion
- The do-while loop is used when the code needs to be run at least once.
- This is useful in cases where the condition needs to be checked later.
- Use this carefully to avoid infinite loops.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining