Difference between break and continue

Difference between break and continue

Today, We want to share with you Difference between break and continue.
In this post we will show you Difference between break and continue in PHP | Break and Continue, hear for Difference Between Break and Continue Statements we will give you demo and example for implement.
In this post, we will learn about What is difference between break, continue and return statements with an example.

BREAK:

-break statement will stop the current loop
-break statement ends execution of the current for loop, foreach loop, while loop, do-while loop or switch structure.

Read Also:  PHP MySQL ajax Autocomplete Live Search Box in jQuery

CONTINUE:

– continue statement will stop the each current iteration and start the next loop one.
– continue statement is used within any looping structures to skip this itself the rest of the current each loop iteration and continue statement execution at the condition all the evaluation and then the start beginning of the next iteration following.

Continue statement and break statement provide extra control over your loops by manipulating the all the data flow of the iterations.

Read Also:  10 little Importance PHP functions list with Example

The following code is an example; in this post you learn about the break statement and Continue statement.

Syntax of break and continue

while ($foo) {   <--------------------┐
    continue;    --- goes back here --┘
    break;       ----- jumps here ----┐
}                                     |
                 <--------------------┘

Example

$l = 10;
while (--$l)
{
    if ($l == 5)
    {
        continue;
    }
    if ($l == 3)
    {
        break;
    }
    echo $l . "\n";
}

will output:

9 8 7 6 4 

PHP break and continue Statement with Example

for($l=0; $l<10; $l++){
    if($l == 5){
        echo "Now It reach five<br>";
        continue;
    }
    echo $l . "<br>";
}

echo "<hr>";

for($l=0; $l<10; $l++){
    if($l == 5){
         echo "Now It reach end The Value<br>";
         break;
    }
    echo $l . "<br>";
}

PHP Break Statement Example

$number = 0;
for ($number = 0;$number <= 5;$number++)
{
        if ($number==2)
        {
                break;
        }
        echo $number;

        echo "<br />";

}
echo "End of for loop" ;

will output

Read Also:  How to Integrate PayPal Payment Gateway in PHP?


1
End of your for loop

PHP Continue Statement Example

$number = 0;
for ($number = 0;$number <= 5;$number++)
{
        if ($number==2)

        {
                continue;
        }

        echo $number;
        echo "<br />";
}
echo "End of your for loop" ;

will output


1
3
4
5
End of for loop

Example PHP


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  HTTP Session Management Using Laravel
  2. Read Also:  defer parsing of javascript htaccess
  3. Read Also:  Laravel Eloquent Queries Joins Example
  4. Read Also:  Vue IF Else Conditional Statement
  5. Read Also:  SQL Not Equal Operator introduction and examples
  6. Read Also:  How to print/display javascript variable in html?
  7. Read Also:  like in sql
  8. Read Also:  SQL Not Equal Operator introduction and examples
  9. Read Also:  10 little Importance PHP functions list with Example
  10. Read Also:  PHP MySQL ajax Autocomplete Live Search Box in jQuery