Selected flow control statements--break statement and continue statement (with detailed explanation)

慕斯
Release: 2023-03-10 09:34:01
Original
1549 people have browsed it

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! ! !

Selected flow control statements--break statement and continue statement (with detailed explanation)

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. &#39;<hr/>&#39;;
    if($hr == 4){
    break;
    }
}
?>
Copy after login

The code running result is as follows:

Selected flow control statements--break statement and continue statement (with detailed explanation)

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. &#39;<hr/>&#39;;
    if($hr == 4){
    break;
    }
}
for($i =0;$i <10; $i ++){
    for ($j=0;$j<10;$j++){
        echo$j. &#39;&#39;;
    if($j== 4){
    break 2;
    }
    }
    echo &#39;<br/>&#39;;
}
?>
Copy after login

The code running result is as follows:

Selected flow control statements--break statement and continue statement (with detailed explanation)

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
}
?>
Copy after login

The code running result is as follows:

Selected flow control statements--break statement and continue statement (with detailed explanation)

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. &#39;&#39;;
    }
    echo &#39;<br/>&#39;;
}
?>
Copy after login

The code running results are as follows:

Selected flow control statements--break statement and continue statement (with detailed explanation)

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!