The do-while loop executes a block of code first
and checks its condition afterward. Therefore, the loop body
executes at least once, even if the condition is initially false.
A do-while loop is useful when you want
the code to run at least once before checking
whether it should repeat.
Syntax:
do {
// Code to execute
} while (condition);
Notice the semicolon after the
while (condition) statement.
Example:
<?php
$i = 1;
do {
echo $i . "<br>";
$i++;
} while ($i <= 5);
?>
Output:
1
2
3
4
5
The loop displays numbers from 1 to 5 and stops when its condition becomes false.
A do-while loop follows these steps:
do block.while statement.<?php
$count = 1;
do {
echo "Hello PHP<br>";
$count++;
} while ($count <= 3);
?>
Output:
Hello PHP
Hello PHP
Hello PHP
The statement inside the loop executes three times
because the condition remains true until
$count becomes 4.
The main feature of a do-while loop is that its condition is checked after the loop body.
<?php
$i = 10;
do {
echo "Value: " . $i;
} while ($i < 5);
?>
Output:
Value: 10
Although $i < 5 is false, PHP
executes the block once before checking the condition.
You can count backward by decreasing a variable during every iteration.
<?php
$i = 5;
do {
echo $i . "<br>";
$i--;
} while ($i >= 1);
?>
Output:
5
4
3
2
1
The -- operator decreases the counter
by one after each number is displayed.
A do-while loop can calculate the sum of a sequence of numbers.
<?php
$i = 1;
$sum = 0;
do {
$sum += $i;
$i++;
} while ($i <= 5);
echo "Sum = " . $sum;
?>
Output:
Sum = 15
The loop adds 1, 2, 3, 4 and 5 to the
$sum variable.
You can print a multiplication table using a do-while loop.
<?php
$number = 7;
$i = 1;
do {
echo $number . " x " . $i . " = ";
echo ($number * $i) . "<br>";
$i++;
} while ($i <= 10);
?>
Output:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Change $number = 7 to another number
to generate a different table.
The modulus operator (%) helps identify
even numbers inside a do-while loop.
<?php
$i = 1;
do {
if ($i % 2 == 0) {
echo $i . "<br>";
}
$i++;
} while ($i <= 10);
?>
Output:
2
4
6
8
10
The condition $i % 2 == 0 is true
for even numbers.
| While Loop | Do While Loop |
|---|---|
| Checks the condition before executing the body. | Checks the condition after executing the body. |
| May execute zero times. | Executes at least once. |
Syntax starts with while. |
Syntax starts with do. |
| No semicolon after the condition's closing parenthesis is required. | A semicolon is required after while (condition). |
While loop example:
<?php
$i = 10;
while ($i < 5) {
echo $i;
}
?>
Output:
No output
Do-while example:
<?php
$i = 10;
do {
echo $i;
} while ($i < 5);
?>
Output:
10
The break statement terminates a loop.
The continue statement skips the rest
of the current iteration and proceeds to the next one.
Example using break:
<?php
$i = 1;
do {
if ($i == 4) {
break;
}
echo $i . "<br>";
$i++;
} while ($i <= 10);
?>
Output:
1
2
3
Example using continue:
<?php
$i = 0;
do {
$i++;
if ($i == 3) {
continue;
}
echo $i . "<br>";
} while ($i < 5);
?>
Output:
1
2
4
5
In the second example, the counter is increased
before continue, so the loop can
progress normally and eventually terminate.
Write PHP programs to complete these exercises:
Sample starting code:
<?php
$i = 1;
$sum = 0;
do {
$sum += $i;
$i++;
} while ($i <= 10);
echo "Total = " . $sum;
?>
Expected output:
Total = 55
1. When does a do-while loop check its condition?
2. How many times does a do-while loop execute when its condition is initially false?
3. Which symbol must appear after the while condition in a do-while loop?