abri



do...while


php128 apg

Full code fragment that perform REPEATED ACTIONS until a desired result is reached.


Behavior do...while loops are the similar to while loops, except the truth expression is checked at the end of each iteration instead of in the beginning.

Since PHP 5.3.0, it is possible to use the goto operator instead of this function.

With the use of goto it will be possible to code scripts with greater ease.

On the next page you will know how to use it.



FIRST SYNTAX TYPE


In the following, we will show the FIRST SYNTAX TYPE of -  do... while  - conditional structure:



<?php

// STRUCTURE #1

seed
    
do 
    
statement
    
    
while ( expr );
    
    
    
where,

seed to begin the loop

statement 
code to be executed

expr 
to control the loop
    
?>

SECOND SYNTAX TYPE


In the following, we will show the SECOND SYNTAX TYPE of -  do... while  - conditional structure:



<?php

// STRUCTURE #2
 
    
seed
        
do
        {
            
statement
        
}
        while ( 
expr );


where,

seed to begin the loop

statement 
code to be executed

expr 
to control the loop
             
?>

  1 EXERCISE   

<?php

$n01 
2;

do 
echo 
$n01++ . '&nbsp;&nbsp;';
while (
$n01 <= 9);

?>


 RESULT   

2 3 4 5 6 7 8 9


  2 EXERCISE   

<?php

$lang 
'en';
// $lang = 'pt';

$nr 5;
$ir 1
$dr $nr $ir 1;
$txt 'Maxima debetur puero reverentia!';
$en $txt "<br> $dr times -> from $ir 
                       to 
$nr -> interval [$ir$nr]<br><br>";
$pt $txt "<br> $dr vezes -> de $ir 
                        a 
$nr -> intervalo [$ir$nr]<br><br>";

echo $
$lang;
do {
print 
$ir;
echo 
") $txt<br><br>";
$ir++;
}
while ( 
$ir <= $nr );

?> 


 RESULT   

Maxima debetur puero reverentia!
5 times -> from 1 to 5 -> interval [1, 5]

1) Maxima debetur puero reverentia!
2) Maxima debetur puero reverentia!
3) Maxima debetur puero reverentia!
4) Maxima debetur puero reverentia!
5) Maxima debetur puero reverentia!



  3 EXERCISE   

<?php

$n3 
0;

do
echo ++
$n3 ' * ' . ($n3 1) . ' = ' $n3*($n3 1) . '<br>';
while(
$n3 <= 9)

?> 


 RESULT   

1 * 0 = 0
2 * 1 = 2
3 * 2 = 6
4 * 3 = 12
5 * 4 = 20
6 * 5 = 30
7 * 6 = 42
8 * 7 = 56
9 * 8 = 72
10 * 9 = 90