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

  1. do Block:

It contains the code that runs first.

  1. 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

  1. First, the do block of code is run.
  2. After that, the condition is checked.
  3. If the condition is true, the loop runs again.
  4. 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

  1. To run the code at least once:

When you want to ensure that a code block will run at least once.

  1. Avoid Infinite Loop:

Make sure the variable is updated correctly and the condition is returned to false at the time.

  1. 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’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *