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:
- The break statement terminates the whole iteration of a loop whereas continue skips the current iteration.
- The break statement terminates the whole loop early whereas the continue brings the next iteration early.
- 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.Â
php
<?php for ( $i = 1; $i < 10; $i ++) { Â Â Â Â if ( $i % 2 == 0) { Â Â Â Â Â Â Â Â continue ; Â Â Â Â } Â Â Â Â echo $i . " " ; } ?> |
Output: Program 2: This program illustrates the break statement inside a loop.Â
php
<?php for ( $i = 1; $i < 10; $i ++) { Â Â Â Â if ( $i == 5) { Â Â Â Â Â Â Â Â break ; Â Â Â Â } Â Â Â Â echo $i . " " ; } ?> |
Output: Program 3: Using switch inside a loop and continue 2 inside case of switch.Â
php
<?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> ' ; } ?> |
Output:
Let us see the differences in a tabular form -:
 | Break | Continue |
1. | The break statement is used to jump out of a loop. | The continue statement is used to skip an iteration from a loop |
2. |
Its syntax is -: break; |
Its syntax is -: continue; |
3. | The break is a keyword present in the language | continue is a keyword present in the language |
4. | It can be used with loops ex-: for loop, while loop. | It can be used with loops ex-: for loop, while loop. |
5. | The break statement is also used in switch statements | We can use continue with a switch statement to skip a case. |