The break and continue statements are frequently used in combination with conditional statements for and if - else. Following rules apply to these statements:
- The continue statement is used to skip the rest of the code execution in a loop.
- The break statement is used to terminate the current loop structure just as used in the switch statement.
- Both statements take arguments in a numeric form. A continue takes argument to find out how many steps are to skip, while on the other hand, a break takes argument to find how many loops and conditions to be skipped (typically in case of a nested loop).
Syntax for BREAK and CONTINUE statements
<?php
// break
for (initial_value; counter; initial_value_change) {
if (initial_value == x) {
break;
}
}
// continue
for (initial_value; counter; initial_value_change) {
if (initial_value == y) {
continue;
}
}
?>
Example with a BREAK and a CONTINUE statements
Comments
No comments have been made yet.
Please login to leave a comment. Login now