scriptygoddess

25 Oct, 2004

Continue vs. Break (in while loops)

Posted by: Jennifer In: Lessons learned

(note to self type post)
Using "continue" in the middle of a while loop will skip the current iteration and go back to the beginning of the loop (checking the while value again).

Using "break" in the middle of a while loop will break the loop entirely. (no more looping)

So this code

<?php
$i = 0;
while ($i < 5) {
$i++;
if ($i%2) {
echo "value = " .$i . " – inside if<br>";
continue;
}
echo "value = " .$i . " – outside if<br>";
}
?>

Will produce this:
value = 1 – inside if
value = 2 – outside if
value = 3 – inside if
value = 4 – outside if
value = 5 – inside if

Where as this

<?php
$i = 0;
while ($i < 5) {
$i++;
if ($i%2) {
echo "value = " .$i . " – inside if<br>";
break;
}
echo "value = " .$i . " – outside if<br>";
}
?>

Will produce this:
value = 1 – inside if

See more about while loops here.

No Responses to "Continue vs. Break (in while loops)"

Comments are closed.

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements