PHP break and Continue jump loop example

Source: Internet
Author: User

Sometimes, you might want to let the unconditional loop begin and allow the statements in parentheses to decide when to exit the loop.

There are two special statements that can be used for recycling: interrupts and continuation.

When a break statement terminates or a for loop, the following loop of the current code, if any, continues to execute. Or

You can put a number after the discount keyword, explaining how the loop structure is multi-layered to get rid of. So, Bury

Hidden in a statement deep nested loops can break the outermost loop.

<?php
echo "<p><b>example of using the" Break statement:</b></p> ";

For ($i =0 $i <=10; $i + +) {
if ($i ==3) {break;}
echo "The number is". $i;
echo "<br/>";
}

echo "<p><b>one More example of using the" Break statement:</b><p> ";

$i = 0;
$j = 0;

while ($i < 10) {
while ($j < 10) {
if ($j = = 5) {break 2;}//Breaks out of two while loops tutorial
$j + +;
}
$i + +;
}

echo "The" "the". $i. " <br/> ";
echo "The second number is". $j. " <br/> ";
?>

The continue statement terminates the execution of the next iteration loop while the statement block executes one or a for loop

<?php
echo "<p><b>example of using the Continue statement:</b><p>";

For ($i =0 $i <=10; $i + +) {
if (i==3) {continue;}
echo "The number is". $i;
echo "<br/>";
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.