Difference between break and continue in PHP

The break and continue both are used to skip the iteration of a loop. These keywords are helpful in controlling the flow of the program.

Difference between break and continue:

  1. The break statement terminates the whole iteration of a loop whereas continue skips the current iteration.
  2. The break statement terminates the whole loop early whereas the continue brings the next iteration early.
  3. In a loop for switch, break acts as terminator for case only whereas continue 2 acts as terminator for case and skips the current iteration of loop.

Program 1: This program illustrates the continue statement inside a loop.



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
for ($i = 1; $i < 10; $i++) {
    if ($i % 2 == 0) {
        continue;
    }
    echo $i . " ";
}
?>

chevron_right


Output:
forContinue

Program 2: This program illustrates the break statement inside a loop.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
for ($i = 1; $i < 10; $i++) {
    if ($i == 5) {
        break;
    }
    echo $i . " ";
}
?>

chevron_right


Output:
forBreak

Program 3: Using switch inside a loop and continue 2 inside case of switch.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
for ($i = 10; $i <= 15; $i++) {
    switch ($i) {
        case 10:
            echo "Ten";
            break;
  
        case 11:
            continue 2;
  
        case 12:
            echo "Twelve";
            break;
  
        case 13:
            echo "Thirteen";
            break;
  
        case 14:
            continue 2;
  
        case 15:
            echo "Fifteen";
            break;
    }
  
    echo "<br> Below switch, and i = " . $i . ' <br><br> ';
}
?>

chevron_right


Output:
loopSwitchContinue



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.