search
HomeBackend DevelopmentPHP ProblemSelected 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! ! !

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;
    }
}
?>

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;;
}
?>

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
}
?>

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;;
}
?>

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!

Statement
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
continue是跳出当前循环还是所有循环continue是跳出当前循环还是所有循环Feb 02, 2023 pm 04:20 PM

continue是跳出当前循环。continue语句用于跳过本次循环,执行下次循环;当遇到continue语句时,程序会立即重新检测条件表达式,如果表达式结果为真则开始下次循环,如果表达式结果为假则退出循环。

PHP中continue关键字的作用和使用方法PHP中continue关键字的作用和使用方法Jun 28, 2023 pm 08:07 PM

PHP中continue关键字的作用和使用方法在PHP编程中,continue是一个非常有用的关键字。它用于控制循环语句的执行流程,允许跳过当前循环中的剩余代码,并直接进入下一次循环的执行。continue的作用是在循环语句中跳过当前迭代中的代码,并直接开始下一次迭代。当执行到continue语句时,循环控制会立即转到循环体的开始处,而不会执行continu

Go语言break停止语句有什么用Go语言break停止语句有什么用Jan 18, 2023 pm 03:46 PM

在Go语言中,break停止语句用于循环语句中跳出循环,并开始执行循环之后的语句。break语句可以结束for、switch和select的代码块,另外break语句还可以在语句后面添加标签,表示退出某个标签对应的代码块,标签要求必须定义在对应的 for、switch和select的代码块上。

JS循环学习:跳出循环语句break和continueJS循环学习:跳出循环语句break和continueAug 03, 2022 pm 07:08 PM

在之前的文章中,我们带大家学习了JS中的几种循环控制结构(while和do-while循环、for循环​),下面聊聊跳出循环语句break和continue,希望对大家有所帮助!

php里面break的用法是什么php里面break的用法是什么Jan 31, 2023 pm 07:33 PM

在php中,break用于跳出当前的语法结构,执行下面的语句;可以在switch、for、while和do while等语句中使用,可以终止循环体的代码并立即跳出当前的循环,执行循环之后的代码。break语句可以带一个参数n,表示跳出循环的层数,如果要跳出多重循环的话,可以用n来表示跳出的层数,如果不带参数默认是跳出本重循环。

聊聊PHP switch语句中不使用break的情况聊聊PHP switch语句中不使用break的情况Mar 20, 2023 pm 04:55 PM

在PHP中使用switch语句来进行多个分支的选择是很常见的,通常在每个分支结束后会使用break语句来退出switch语句。然而,有些情况下我们不想使用break语句,本文将介绍在PHP switch语句中不使用break的情况。

Java中的break关键字的作用是什么?Java中的break关键字的作用是什么?Apr 23, 2023 am 10:13 AM

说明1、break的作用是跳出现在的循环块(for、while、dowhile)或程序块(switch)。2、循环块的作用是跳出现在循环中的循环体。程序块中的作用是中断和下一个case条件的比较。在switch语句中使用break,终止switch语句。当break用于循环时,跳出循环。在其他地方使用break是没有意义的。实例intsum=0;inti;for(i=1;i

PHP中break语句的用法详解PHP中break语句的用法详解Mar 20, 2024 pm 03:51 PM

PHP中break语句的用法详解在PHP编程中,break语句是一个非常常用的控制语句,它通常用于中断循环或者switch语句的执行。在本文中,我们将详细解释break语句的用法,并给出具体的代码示例。1.中断循环在循环中使用break语句可以提前中断循环的执行,跳出循环体。这在某些情况下非常有用,比如在满足某个条件时就不再需要继续循环。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!