Break & Continue Statement
Break Statement
The break statement is used to stop the execution of a loop or switch statement immediately. When break is used, the program exits the current loop or switch block.
- Loops (for, while, do...while, foreach)
- switch statement
Example
for ($i = 1; $i <= 5; $i++) {
Β Β if ($i == 3) {
Β Β Β Β break;
Β Β }
Β Β echo $i . " ";
}
?>
Continue Statement
The continue statement is used inside loops to skip the current iteration and move to the next iteration of the loop.
The continue statement is usedΒ
- for loop
- while loop
- do...while loop
- foreach loop
Example
for ($i = 1; $i <= 5; $i++) {
Β Β if ($i == 3) {
Β Β Β Β continue;
Β Β }
Β Β echo $i . " ";
}
?>