Skipping an Iteration with the CONTINUE Statement

Chapter 6 #123 - Martha's CIS 52

ASSIGNMENT/EXPLANATION

"The continue statement ends execution of the current iteration but doesn't cause the loops as a whole to end. Instead, the next iteration begins immediately." (123) In this example, "if the value of the $counter variable is eqivalent to zero, the iteration is skipped and the next one starts immediately."

ANALYSIS: "Using the break and continue statements can make code more difficult to read because they often add layers of complexity to the logic of the loop statements that contain them." (124) To aid others (and to remind yourself of your intent!), use comments...

Exercise

4000 divided by -4 is...-1000
4000 divided by -3 is...-1333.3333333333
4000 divided by -2 is...-2000
4000 divided by -1 is...-4000
4000 divided by 1 is...4000
4000 divided by 2 is...2000
4000 divided by 3 is...1333.3333333333
4000 divided by 4 is...1000
4000 divided by 5 is...800
4000 divided by 6 is...666.66666666667
4000 divided by 7 is...571.42857142857
4000 divided by 8 is...500
4000 divided by 9 is...444.44444444444
4000 divided by 10 is...400

CODE

<?php 
$counter = -4;
for (; $counter <= 10; $counter++) {
if ($counter == 0) {
continue;
}
$temp = 4000/$counter;
echo "4000 divided by ".$counter." is...".$temp."<br />";
}
?>