PHP do-while loop

PHP do-while loop can be used to traverse set of code like php while loop. The PHP do-while loop is guaranteed to run at least once.

The PHP do-while loop is used to execute a set of code of the program several times. If you have to execute the loop at least once and the number of iterations is not even fixed, it is recommended to use the do-while loop.

It executes the code at least one time always because the condition is checked after executing the code.

The do-while loop is very much similar to the while loop except the condition check. The main difference between both loops is that while loop checks the condition at the beginning, whereas do-while loop checks the condition at the end of the loop.

Syntax

do{  
//code to be executed  
}while(condition); 

Flowchart

flowchart of php do while loop

Example

<?php    
$n=1;    
do{    
echo "$n<br/>";    
$n++;    
}while($n<=10);    
?>    

Output:

1
2
3
4
5
6
7
8
9
10

Example

A semicolon is used to terminate the do-while loop. If you don’t use a semicolon after the do-while loop, it is must that the program should not contain any other statements after the do-while loop. In this case, it will not generate any error.

 <?php  
    $x = 5;  
    do {  
        echo "Welcome to protutorials! </br>";  
        $x++;  
    } while ($x < 10);  
?>  

Output:

Welcome to protutorials!
Welcome to protutorials!
Welcome to protutorials!
Welcome to protutorials!
Welcome to protutorials!

Example

The following example will increment the value of $x at least once. Because the given condition is false.

 <?php  
    $x = 1;  
    do {  
        echo "1 is not greater than 10.";  
        echo "</br>";  
        $x++;  
    } while ($x > 10);  
    echo $x;  
?>

Output:

1 is not greater than 10.
2

Difference between while and do-while loop

while Loopdo-while loop
The while loop is also named as entry control loop.The do-while loop is also named as exit control loop.
The body of the loop does not execute if the condition is false.The body of the loop executes at least once, even if the condition is false.
Condition checks first, and then block of statements executes.Block of statements executes first and then condition checks.
This loop does not use a semicolon to terminate the loop.Do-while loop use semicolon to terminate the loop.

JavaScript while Loop and do-while Loop

Whenever you want to execute a certain statement over and over again you can use the JavaScript while loop to ease up your work. JavaScript while loop lets us iterate the code block as long as the specified condition is true. Just like for Loop, the while and do...while loop are also used to execute code statements multiple times based on a condition.

The only difference between the while loop and do...while loop is that the do...while loop will execute at least once because in do...while loop the condition is checked after execution of code block.

JavaScript while Loop: Syntax and Use

Below is the syntax for the while loop:

while(condition)
{
    // code statements
}

Let’s take an example to see the while loop in action.

JavaScript while Loop: Example

In this example, we are using while loop, and the loop executes only if the specified condition is true.

let a = 9;
            // while loop will be executed two times
            while (a <= 10)
            {
                document.write("while executed <br>");
                a++;
            }

JavaScript do…while Loop: Syntax and Use

Just like the while loop, the do...while loop lets you iterate the code block as long as the specified condition is true. In the do-while loop, the condition is checked after executing the loop. So, even if the condition is true or false, the code block will be executed for at least one time.

Below we have the syntax of the do...while loop,

do
{
    // code statements
}
while(condition)

JavaScript do...while Loop: Example

Let’s take an example and see the do...while loop in action.

let a = 10;
            // will execute twice
            do 
            {
                document.write("do executed <br>");
                a++;
            }
            while(a < 12)

In this tutorial, we explained the while and do...while loops used in the JavaScript.