简体   繁体   中英

Continue or break a specific loop

I have several loops and would like to specify a particular loop when I use the break or continue instruction

In the PHP doc , it is written that I can use the number numeric argument which tells it how many nested enclosing structures are to be broken out of.

$i = 0;
while (++$i) {
    switch ($i) {
    case 5:
        echo "At 5<br />\n";
        break 1;  /* Exit only the switch. */
    case 10:
        echo "At 10; quitting<br />\n";
        break 2;  /* Exit the switch and the while. */
    default:
        break;
    }
}

But I'd like something more friendly like this:

$i = 0;
loop1: // Label/tag of loop1
while (++$i) {
    loop2: // Label/tag of loop2
    switch ($i) {
    case 5:
        echo "At 5<br />\n";
        break loop2;  /* Exit only the switch. */
    case 10:
        echo "At 10; quitting<br />\n";
        break loop1;  /* Exit the switch and the while. */
    default:
        break;
    }
}

Is it possible?

It is not possible. Furthermore, too many break -Statements can make your code very unreadable and hard to understand. You should restructure your code to avoid breaks and use other control flow statements .

do {
    $i += 1;
    if($i === 5) {
        echo "At 5<br />\n";
    } else if ($i === 10) {
        echo "At 10; quitting<br />\n";
    }
} while($i < 10);

You can also try to split your code up in different functions or methods.

function doSomething($i) {
    if($i === 5) {
        echo "At 5<br />\n";
    } else if ($i === 10) {
        echo "At 10; quitting<br />\n";
    }
}

function run() {
    $i = 0;
    while($i < 10) doSomething(++$i);    
}

In the second case you have to be aware of the strong dependency between the two functions. If you would change run() so that it counts to 15 the function doSomething would misbehave if $i = 10 . Therefore you should encapsulate them in a class or try to introduce another variable which both functions can use to determine when the loop will end. That will avoid future mistakes and magic things to happen.

function doSomething($i, $max) {
    if($i === 5) {
        echo "At 5<br />\n";
    } else if ($i === $max) {
        echo "At 10; quitting<br />\n";
    }
}

function run($max) {
    $i = 0;
    while($i < $max) doSomething(++$i);    
}

Of course it is no problem to use one, two or even more breaks if your code remains readable. But you should consider if there are other more elegant ways to solve your problem. And mostly there are better ways.

My personal recommendation is extract nested loop code into the separate methods and/or use the exceptions. In general "break to label" feels like "goto". I don't want you to be eaten by dinosaur ;)

I do not believe this is possible, but if you break down your code and put the nested loops in a function you can use return to quit all loops. That might improve readability.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM