Lesson 15 of 50 – PHP Do While Loop
30%

PHP Do While Loop

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.

1. What Is a Do While Loop?

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.

2. How Does a Do While Loop Work?

A do-while loop follows these steps:

  1. Execute the statements inside the do block.
  2. Evaluate the condition in the while statement.
  3. If the condition is true, execute the block again.
  4. If the condition is false, stop the loop.
<?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.

3. Do While Executes at Least Once

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.

4. Printing Numbers in Reverse

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.

5. Calculate the Sum of Numbers

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.

6. Multiplication Table Using Do While

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.

7. Display Even Numbers

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.

8. Do While vs. While Loop
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
9. Using break and continue

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.

📝 Practical Assignment

Write PHP programs to complete these exercises:

  1. Print numbers from 1 to 10 using a do-while loop.
  2. Print numbers from 10 down to 1.
  3. Calculate the sum of numbers from 1 to 50.
  4. Print the multiplication table of 8.
  5. Display even numbers from 1 to 20.
  6. Display odd numbers from 1 to 20.
  7. Calculate the factorial of a positive integer.
  8. Use a do-while loop to display a message at least once when its condition is false.
  9. Use break to stop the loop when the counter reaches 6.
  10. Use continue to skip the number 4.

Sample starting code:

<?php
$i = 1;
$sum = 0;

do {
    $sum += $i;
    $i++;
} while ($i <= 10);

echo "Total = " . $sum;
?>

Expected output:

Total = 55

🧠 Quick Quiz

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?