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.

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.

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
"; continue; } echo $l . "
"; } echo "
"; for($l=0; $l<10; $l++){ if($l == 5){ echo "Now It reach end The Value
"; break; } echo $l . "
"; }

PHP Break Statement Example

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

        echo "
"; } echo "End of for loop" ;

will output

0
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 "
"; } echo "End of your for loop" ;

will output

0
1
3
4
5
End of for loop

Example PHP

Leave a Comment