The continue statement ends the execution of th current iteration but doesn't end the loop as a whole. Instead, the next iteration begins immediately. Using the previous example as in the break statement, replacing the break statement with the continue statement, we can get rid of a divide-by-zero error without ending the loop completely. See the example below:
<?php
$i = -4;
for ($i ; $i <= 10; $i++ ) {
if ( $i == 0 )
continue;
$temp = 1000/$i;
print "1000 divided by $i is... $temp<br>";
}
?>
save it as continue.php and open it through your browser. The result should be:
1000 divided by -4 is... -250
1000 divided by -3 is... -333.33333333333
1000 divided by -2 is... -500
1000 divided by -1 is... -1000
1000 divided by 1 is... 1000
1000 divided by 2 is... 500
1000 divided by 3 is... 333.33333333333
1000 divided by 4 is... 250
1000 divided by 5 is... 200
1000 divided by 6 is... 166.66666666667
1000 divided by 7 is... 142.85714285714
1000 divided by 8 is... 125
1000 divided by 9 is... 111.11111111111
1000 divided by 10 is... 100
As the result says, we know that the iteration stop temporarily if $i equals to zero and then continue the loop until finish
No comments:
Post a Comment