Php program to print the number using do-while loop

A Php program to print the number using do-while loop. In this program we can print the number using do while loop. The do while loop is an exit controlled loop in which the loop body is run at least once even if the test condition is false. When you want to exit your software based on user input, this is an example of such a scenario.

<?php 
$x = 10;

do {
echo "$x <br>";
$x++;
} while ($x <= 15);
?>

 

Output:

10
11
12
13
14
15

What is do while loop in php

A do while loop is identical to a while loop, except that it executes the statements within the body of the do-while loop before verifying the condition. In the while loop, on the other hand, the condition is verified first, and then the statements in the while loop are executed.

In all instances where the loop body must be executed at least once, the do while loop should be used. When activities are to be repeated based on user input in a menu-driven software, we often require a do while loop to figure out which action the user wants to do.

Difference between while loop and do while loop

Only when the supplied condition is true is the while loop executed. Do-while loops, on the other hand, are executed for the first time regardless of the condition. The condition is tested after the first execution of the while loop.