"Welcome to our PHP 101 blog, where we demystify the world of web development. "

Saturday, January 20, 2024

PHP Break and Continue

 

PHP break and continue  Statement

Continue and break provide extra control over your loops by manipulating the flow of the iterations. 
Continue is the nicer one of the two as it prevents the current iteration, or flow through one pass of the loop, and just tells PHP to go to the next round of the loop. 
Break provides more aggressive control by completing skipping out of the loop and onto the code following the loop. 
BreakStatement:
Break statement is used to terminate the loop whenever require.
The keyword break ends execution of the current for, foreach, while, do while or switch structure. When the keyword break executed inside a loop the control automatically passes to the first statement outside the loop.
break is usually associated with the if.
Break - ends execution of the current structure. Break accepts an optional numeric argument which tells it how many execution of nested structures to be interrupted.
Break takes you out from a loop and switch case. 
The break statement can also be used to jump out of a loop. 
The break statement is frequently used to terminate the processing of a particular case within a switch statement. 
Within nested statements, the break statement terminates only the do, for, foreach, switch, or while statement that immediately encloses it. You can use a return or goto statement to transfer control elsewhere out of the nested structure.
Suppose we need to terminate the loop then we can use break statement. In PHP break statement is used for the termination of the loop statement and switch case statement.
Syntax:
{
statements;
break;
}

Flow diagram :




Example :
Inside the if statement
<?php
for($i=1;$i<=10;$i++){
echo “$i<br>”;
If($i==5){
break;
}
}
?>
Output:1 2 3 4 5

PHP Break inside inner loop

<?php
for($i=1;$i<=3;$i++){
for($j=1;$j=3;$j++){
echo “$i $j<br>”;
if($i==2 && $j==2)
{
break;
}
}
}
?>
Output: 1 1 1 2 1 3 2 1 2 2 3 1 3 2 3 3

PHP Break: inside switch statement

<?php
$num=3;
Switch($num){
Case 1:echo(“Monday”);break;
Case 2:echo(“Tuesday”);break;
Case 3:echo(“Wednesday”);break;
Case 4:echo(“Thursday”);break;
Case 5:echo(“Friday”);break;
Case 6:echo(“Saturday”);break;
Case 7:echo(“Sunday”);break;
}
?>


Continue Statement:

we can use continue statement to skip the part of the loop.
The continue statement forces the next iteration of the loop skipping the remaining code of the loop.
Continue - is used to stop processing the current block of code in the loop and goes to the next iteration.

"Continue is used within looping structures to skip the rest of the current loop iteration" and continue execution at the condition evaluation and proceed to the beginning of the next iteration. The current functionality treats switch structures as looping in regards to continue. It has the same effect as a break.
The continue Statement can be used in for ,while and do…while loop.
  1. In the case of the for loop as soon as after the execution of continue statement, increment/decrement statement of the loop gets executed. After the execution of increment statement, condition will be checked.
  2. In case of the while loop, continue statement will take control to the condition statement.
  3. In case of the do..while loop, continue statement will take control to the condition statement specified in the while loop.
Syntax:
<?php
Loop (While, do-while, for,)
{
conditions
{
continue;//continue statement
}
code executed;
}
?>
Flow Diagram:

Example:
<?php
$array=array(1,2,3,4);
foreach($array as $value)
{
if($value== 3)
 Continue;
echo “Value is $value <br>”;
}
?>
Output:
Value is 1 2 3 4

No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template