
While and do-while loops in PHP
Unlike the for loop in PHP, while and do-while loops are used for problems where the number of iterations is not known in advance. The for loop, on the other hand, is used when the number of iterations is known. However, there are problems where all three loops (for, while, and do-while) can be used to solve them. But to do that, we need to understand the concepts of while and do-while loops in PHP.
- Structure of a while loop
- Structure of a do-while loop
- When to use for, while, and do-while loops
- While and do-while loops in array traversal
Contents
1. Structure of a while loop
Syntax:
<?php while (condition) { // code to be executed } ?>
Where $condition
is the condition to stop the loop. If $condition
evaluates to false, the loop will end; otherwise, the loop will continue iterating. The while loop will loop indefinitely if the condition expression you provide always evaluates to true.
Example: Using a while loop in PHP to list numbers from 1 to 10.
To solve this problem, you can use a for loop in PHP to solve it easily.
<?php for ($i = 1; $i <= 10; $i++){ echo $i . ' - '; } ?>
But the problem requires the use of a while loop, so let’s see the solution below:
<?php $i = 1; // Variable for iteration while ($i <= 10){ // Only loop if $i <= 10 echo $i . ' - '; // Output to the screen $i++; // Increment $i by 1 } ?>
Explanation:
$i = 1
is the variable used for iteration.while ($i <= 10)
is the line that starts the loop, where the loop condition is$i <= 10
.echo $i . ' - '
outputs the variable$i
and the character ‘-‘ to the screen.$i++
increases the value of$i
by 1. For example, if currently$i = 1
, after each iteration,$i
will be equal to 2. This line is crucial because without it, the variable$i
will always be 1 after each loop, and the loop condition will always be true (($i <= 10) <=> (1 <= 10) => true
), causing an infinite loop.
Iteration 1: Variable $i = 1
, the condition is checked (1 <= 10)
=> true, so the code inside the loop will be executed and output “1 – ” to the screen. Then, the line $i++
will increase $i
by 1 => $i = 2
, and it goes back to the loop for the next iteration.
Iteration 2: Variable $i = 2
, the condition is checked (2 <= 10)
=> true, so the code inside the loop will be executed and output “2 – ” to the screen. Then, the line $i++
will increase $i
by 1 => $i = 3
, and it goes back to the loop for the next iteration.
Similarly, for iterations 3, 4, 5, 6, 7, 8, and 9: After the 9th iteration, the variable $i
will have the value $i = 10
.
Iteration 10: Variable $i = 10
, the condition is checked (10 <= 10)
=> true, so the code inside the loop will be executed and output “10 – ” to the screen. Then, the line $i++
will increase $i
by 1 => $i = 11
, and it goes back to the loop for the next iteration.
Iteration 11: Variable $i = 11
, the condition is checked (11 <= 10)
=> false. It doesn’t satisfy the condition, so the loop ends (the code inside the loop is not executed, and the string “11 – ” doesn’t appear on the screen).
End result: Combining all the outputs together, the screen will display “1 – 2 – 3 – 4 – 5 – 6 – 7 – 8 – 9 – 10 – “.
With a for loop, the iterations follow a regular increment or decrement pattern. However, with a while loop, you can loop based on any expression, not just a specific pattern.
Example:
<?php $i = 0; $j = 10; while ($i < 100 && $j > 5){ $i++; $j -= 2; } ?>
This loop will execute 3 times.
Iteration 1: $i = 0, $j = 10, the condition is checked (0 < 100 && 10 > 5) => true, so the loop executes by incrementing $i and decrementing $j by 2. At this point, $i = 1, $j = 8.
Iteration 2: $i = 1, $j = 8, the condition is checked (1 < 100 && 8 > 5) => true, so the loop executes by incrementing $i by 1 and decrementing $j by 2. At this point, $i = 2, $j = 6.
Iteration 3: $i = 2, $j = 6, the condition is checked (2 < 100 && 6 > 5) => true, so the loop executes by incrementing $i by 1 and decrementing $j by 2. At this point, $i = 3, $j = 4.
Iteration 4: $i = 3, $j = 4, the condition is checked (3 < 100 && 4 > 5) => false, as the condition is false, the loop terminates (the 4th iteration is not executed).
2. Do-while loop structure
A while loop checks the condition first and then executes the code inside the loop. On the other hand, a do-while loop executes the code inside the loop first and then checks the condition. If the condition is true, it will continue to the next iteration of the loop. If the condition is false, the loop will terminate. The do-while loop in PHP always executes at least one iteration because it performs the code execution before checking the condition.
Syntax:
<?php do { // Code to be executed } while (condition); ?>
Don’t forget to put a semicolon (;) after the while statement.
Example:
<?php $i = 1; do{ echo $i; $i++; }while ($i <= 10); ?>
This program outputs the numbers from 1 to 10 to the screen. The explanation is similar to the while loop. At each iteration, it prints the value of the variable $i to the screen, then increases $i by 1, and finally checks the condition. If ($i <= 10) is true, it continues to the next iteration, otherwise, it terminates the loop.
Example:
<?php $i = 1; do{ echo $i; $i++; }while ($i < 1); ?>
This loop will execute once because it performs the “do” part first and then checks the while condition. This is why I mentioned earlier that the while loop always executes at least once.
Note: Just like the caution with the while loop, the do-while loop in PHP is prone to infinite looping, so be careful when using it.
3. Can a problem be solved using all three loops?
The answer is yes and no. As with the example above, using the for loop may not be feasible.
<?php $i = 0; $j = 10; while ($i < 100 && $j > 5){ $i++; $j -= 2; } ?>
Example: Print numbers from 100 to 200;
Using a for loop:
<?php for ($i = 100; $i <= 200; $i++){ echo $i; } ?>
Using a while loop:
<?php $i = 100; while ($i <= 200){ echo $i; $i++; // Increase $i by 1 } ?>
Using a do-while loop:
<?php $i = 100; do { echo $i; $i++; } while ($i <= 200); ?>
4. When to use for, while, and do-while loops
This depends on the experience of each individual, but there are some common guidelines that can help us determine:
For loops are used for problems that involve iterating over a specific sequence of steps, with evenly spaced increments (e.g., 1, 2, 3, 4), and when the total number of iterations is known.
On the other hand, while and do-while loops are used for other types of problems that don’t follow a specific sequence and don’t require knowing the total number of iterations in advance.
5. Nested while and do-while loops
Similar to for loops and if statements, while and do-while loops can be nested to solve more complex problems.
For example:
<?php $i = 1; while ($i < 10){ $j = $i; while ($j < 10){ echo $j; $j++; } echo ''; $i++; } ?>
This loop will output a triangle pattern with numbers:
123456789
23456789
3456789
456789
56789
6789
789
89
9
The total number of iterations is equal to the product of the iteration count of the two inner loops, plus the iteration count of the outer loop. For example, if the outer loop iterates 10 times and the inner loop iterates 10 times, the total number of iterations will be 10 x 10 + 10 = 110 times. Therefore, the cost of nested loops is high.
6. Using while and do-while loops for array traversal
Similar to the for loop, while and do-while loops can be used to access elements in an indexed array.
For example:
<?php // List of Years $years = array( 1990, 1991, 1992, 1993, 1994, 1995 ); // Output in the regular way echo $years[0]; echo $years[1]; echo $years[2]; echo $years[3]; echo $years[4]; echo $years[5]; // Using while $i = 0; while ($i <= 5){ echo $year[$i]; $i++; // Increase the variable $i } // Using do..while $i = 0; do { echo $year[$i]; $i++; } while ($i <= 5); ?>
7. Conclusion
In this lesson, you have learned about the while and do while loops. In total, we have covered three loops: if, while, and do while. In the next lesson, we will explore a loop specifically designed for handling arrays in PHP, which is the foreach loop.
Related Articales
Comments
-
Download and Install Vertrigo Server
May 25, 2023.84 views -
Declaring variables in PHP, common types of variables encountered
May 25, 2023.84 views -
Data types in PHP and their corresponding variable types
May 25, 2023.84 views -
Operators and expressions in PHP
May 25, 2023.84 views -
The if else statement in PHP
May 25, 2023.84 views -
The switch case statement in PHP
May 25, 2023.84 views -
The for loop in PHP
May 25, 2023.84 views -
While and do-while loops in PHP
May 25, 2023.84 views -
The foreach Loop in PHP
May 25, 2023.84 views -
The break, continue, goto, die, exit statements in PHP
May 25, 2023.84 views -
Building Functions in PHP
May 25, 2023.84 views -
Recursive Algorithm in PHP
May 25, 2023.84 views -
Bubble Sort Algorithm in PHP
May 25, 2023.84 views -
Linear Search Algorithm in PHP
May 25, 2023.84 views -
The technique of sentry placement in PHP
May 25, 2023.84 views -
Flagging technique in PHP
May 25, 2023.84 views -
Selection Sort Algorithm in PHP
May 25, 2023.84 views
Tags
© copyright 2021 Courseplus