PHP - Do While loop

PHP - Do While loop

PHP Do-While Loop

The do-while loop in PHP is similar to the while loop, except that the condition is checked after the code block is executed. This ensures that the code inside the loop runs at least once, regardless of whether the condition is true or false initially.

Syntax of do-while loop:

do { // Code to be executed } while (condition);
  • condition: The condition is evaluated after the code inside the loop has executed. If the condition is true, the loop will run again. If the condition is false, the loop will stop.

How it works:

  1. The code inside the loop is executed first.
  2. The condition is checked after the code block has been executed.
  3. If the condition is true, the loop will continue and execute the code block again.
  4. If the condition is false, the loop will stop.

Example of do-while loop in PHP:

$count = 1; do { echo "Count: $count<br>"; $count++; } while ($count <= 5);

Output:

Count: 1 Count: 2 Count: 3 Count: 4 Count: 5

Explanation:

  • The loop executes the code inside the do block first and then checks if $count <= 5.
  • Since $count starts at 1, the condition is true and the loop continues.
  • After each iteration, $count is incremented, and the loop runs again until $count becomes 6, at which point the condition is false, and the loop stops.

Flowchart of the do-while loop:

Below is a diagram illustrating the flow of control inside a do-while loop.

+--------------------------+ | Start of the loop | +--------------------------+ | v +--------------------------+ | Execute the code block | +--------------------------+ | v +--------------------------+ | Check the condition | +--------------------------+ | condition is TRUE ? | Yes | No | v +--------------------------+ | End of the loop | +--------------------------+ | v +--------------------------+ | Go back to execute code| +--------------------------+

Explanation of the Flowchart:

  1. Start of the loop: The loop begins and the code inside the do block is executed once, regardless of the condition.
  2. Execute the code block: The code inside the loop is executed at least once before the condition is checked.
  3. Check the condition: After executing the code block, the condition is checked. If it's true, the loop will continue; if it's false, the loop will end.
  4. Repeat or Exit: The loop continues repeating the steps until the condition becomes false.

Key Differences Between while and do-while Loop:

  1. Condition Check:
    • while loop: The condition is checked before executing the code block.
    • do-while loop: The code block is executed at least once before checking the condition.
  2. Guaranteed Execution:
    • while loop: If the condition is false at the start, the code inside the loop may never run.
    • do-while loop: The code inside the loop will always execute at least once, even if the condition is false initially.

When to Use a do-while Loop:

  • When you need to ensure that the code inside the loop executes at least once, regardless of the condition.
  • When you want to perform an action and then check the condition after executing it, like accepting user input or performing a task before checking for a specific condition.

The do-while loop is useful when the action you need to take must happen at least once before a check is made.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close