Responsive Advertisement

Define break and continue.

 Define break and continue.




                             








In PHP, `break` and `continue` are control structures used within loops to alter the flow of execution.

1. `break`: - The `break` statement is used to terminate the execution of a loop prematurely. - When encountered, `break` immediately exits the current loop and resumes execution at the next statement after the loop. - It is commonly used with conditional statements to terminate a loop when a specific condition is met. - The `break` statement is applicable to `for`, `foreach`, `while`, and `do while` loops.

 2. `continue`: - The `continue` statement is used to skip the remaining iterations within a loop and continue with the next iteration. - When encountered, `continue` jumps to the next iteration of the loop, ignoring any code below it within the current iteration. - It allows you to bypass certain iterations based on specific conditions. - Like `break`, `continue` is also applicable to `for`, `foreach`, `while`, and

do-while` loops. 

Here's an example to illustrate their usage: 

```php for 

($i = 1; $i <= 10; $i++) 

 if ($i == 5) 

 break;

 // Terminates the loop when $i equals 5 }

 if ($i % 2 == 0) 

 continue;

// Skips the remaining code in the current iteration when $i is even } echo $i . ' '; } ``` Output: ``` 1 3 ```

Post a Comment

0 Comments