I am searching advantage of using Do While, most of blogs and tutorials say its depends on requirement, without that is there any advantage we get from Do While ?
<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
?>
I am searching advantage of using Do While, most of blogs and tutorials say its depends on requirement, without that is there any advantage we get from Do While ?
<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
?>
That's funny. Today was one of those days when I needed a do while loop. It was kind of like this: I wanted to generate a number, but it had to pass a test.
With a while loop it would look something like this:
$num = generateRandomNumber();
while (!numberMeetsRequirements($num)) {
$num = generateRandomNumber();
}
As you can see, one line is repeated, once inside and once outside the loop. In this particular example, you can avoid this by using a do-while loop:
do {
$num = generateRandomNumber();
} while (!numberMeetsRequirements($num));
But like the others already said: It really depends on what you are doing. The code above is only an example.
There's no advantage. while
is a pre-conditioned loop, and do..while
is post-conditioned loop. That means - in while
a condition is checked before iteration, and in do..while
- after iteration.
Therefore, in do..while
loop body will be executed at least once - so it can not be used if you're iterating by something, which can have zero repeating (for example, count of rows in select from database).
Note, that due to structured program theorem any program could be written only with if
and while
. So, in general, there's no need in do..while
statement. You always can replace it with while
.
A Do While will always run once as the condition is only checked after the first run
Where a While Do will only run if your condition matches as it is checked first
The main thing is that the statements inside the loop are executed at least once where in most other constructs they might never be executed.
do { >>command<< } while ()
<-- command will be exectued at least one time
while () { >>command<< }
<-- command will only be executed if while condition is true
You can implement any algorithm using a for
, while
and do-while
loop interchangeably. However, following the flow of logic may push you towards using one loop over the other.
A prime example of when using a do-while loop is more appropriate (based on the flow of logic) is in the implementation of a bubble sort. The description of the algorithm can be found here, in short you know that you will need to at least iterate through the array once (seeing as the best-case for bubble sort is O(n)
, where n
is the length of the array). The implementation is as follows:
$arr = array( 10, 5, 6, 1, 3, 2, 7, 8, 9, 4 );
$arr_len = 10;
$swapped;
do {
$swapped = false;
for ( $i = 0; i < ( $arr_len - 1 ); $i++ ) {
if ( $arr[ i ] > $arr[ i + 1 ] ) {
$temp = $arr[ i ];
$arr[ i ] = $arr[ i + 1];
$arr[ i + 1 ] = $temp;
$swapped = true;
}
}
} while ( $swapped == true );
My PHP is very rusty, if there is a syntax error do let me know.