Home > Article > Backend Development > Selected flow control statements--break statement and continue statement (with detailed explanation)
The previous article introduced you to "Detailed explanation and examples - for loop (and the difference between while loop)". This article continues to introduce to you the selected flow control statements--break statement and continue statement. (With detailed explanation), don’t hesitate to come in and learn! You will definitely gain something! ! !
1: break statement
Function:
You can use break in switch to terminate the execution of the branch structure;
You can use break in any loop structure to terminate the loop operation;
We will explain the specific structure by code operation. The code is as follows:
<?php /******break 语句******/ //break测试 输出10个hr for($hr =0;$hr <10; $hr ++){ echo $hr. '<hr/>'; if($hr == 4){ break; } } ?>
The code running result is as follows:
Note:
The break statement can be followed by parameters. The meaning of break1 is the same as break. If the break2 statement is set in the reloop to terminate the two-layer loop (nested loop)
For the specific structure, we use the code Operation explanation, the code is as follows:
<?php /******break 语句******/ //break测试 输出10个hr for($hr =0;$hr <10; $hr ++){ echo $hr. '<hr/>'; if($hr == 4){ break; } } for($i =0;$i <10; $i ++){ for ($j=0;$j<10;$j++){ echo$j. ''; if($j== 4){ break 2; } } echo '<br/>'; } ?>
The code running result is as follows:
After understanding the break statement, we then understand the continue statement:
Continue function: Recycle the loop structure to terminate this cycle and start the next cycle;
We will explain the specific structure with code operations. The code is as follows:
<?php //continue for($i=0;$i<10;$i ++){ if($i == 4){ continue; } echo $i;//0 1 2 3 5 } ?>
The code running result is as follows:
continue statement
Note:continue can be followed by numerical parameters. continue1 means the same as continue. If continue2 is set in a reloop, it means jumping to the outer layer to continue the loop (nested loop)
We will explain the specific structure with code operations. The code is as follows :
<?php //continue for($i=0;$i<10;$i ++){ if($i == 4){ continue; } echo $i;//0 1 2 3 5 } for($i =0;$i <10; $i ++){ for ($j=0;$j<10;$j++){ if($j== 4){ continue 2; } echo$j. ''; } echo '<br/>'; } ?>
The code running results are as follows:
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of Selected flow control statements--break statement and continue statement (with detailed explanation). For more information, please follow other related articles on the PHP Chinese website!