Advanced loops
Written by Administrator   
Thursday, 18 March 2010
Article Index
Advanced loops
Mulitple exits
Break and continue
For and efficiency

Banner

break n

Now that we have the idea of nesting it is time to introduce the break n command. You can follow the break command by a number that indicates how many control structures should be skipped out of. If you don't specify a number then 1 is assumed. Also notice that break 0 has no effect. For example:

for($i=1;$i<=5;$i++){
for($j=1;$j<=3;$j++){
if($j==2)break 1;
echo($i ." ". $j ."<br/>";);
}
}

This is the same nested loop example given in the previous section but now when $j is 2 the break 1 instruction causes the inner loop to terminate. As a result the the program prints

1,1 2,1 3,1 4,1 5,1

If you now change the program to read

for($i=1;$i<=5;$i++){
for($j=1;$j<=3;$j++){
if($j==2)break 2;
echo($i ." ". $j ."<br/>");
}
}

The only change has been to modify break 1 to break 2. This means that now when the break is obeyed the inner and the outer loops exit. As a result the program prints 1,1 and then both loops terminate.

If you change the break to break 3 then you will see an error message to the effect that there aren't three nested control structures to jump out of.

The temptation to use the break n facility is high but there are surprisingly few places where it is worth the additional confusion. Just as the Goto instruction was derided for being an unstructured and uncontrolled jump into chaos there is a very big argument for the break being a similarly unconstrained transfer of control smashing its way through as many nested structures as the programmer cares to set.However there are situations where it seems like the natural expression of an idea. For example if you use a set of nested loops to search for a particular value then when you find it what could be more natural than breaking out of all of the nested loops in a single transfer of control. As in the case of the single loop and break the problem is that you now have multiple exits and you again don't know why the loop ended - but the nested loop situation is only marginally worse.

Continue

The break instruction has a companion in the form of the continue instruction. This doesn't break out of the entire loop - it simply terminates the current repeat. That is, if the loop is in the middle of repeating its list of instructions for the 6th time and it encounters a repeat it immediately treats the loop body as complete and moves to the next repeat. Notice that any tests for the loop to repeat are performed in the usual way - it really is as if the loop had finished the last repeat in the usual way.

For example

for($i=1;$i<=5;$i++){
for($j=1;$j<=3;$j++){
if($j==2)continue;
echo($i ." ". $j ."<br/>");
}
}

In this case the inner loop skips the repeat for $j equal to two but continues on to $j =3. The sequenced generated is:

1,1 1,3 2,1 2,3 3,1 3,3 .. 5,1 5,3

This is easy enough to understand but continue can also be used, like break, with a number that indicates how many nested loops should be "continued". That is:

continue n

skips to the end of each of n nested loops.If you think about this for a minute you will see that it is a very strange idea. How can you skip to the end of two nested loops? Skipping to the end of the outer loop automatically brings the inner loop to a close!? This is indeed what happens and a much better way of thinking about continue n is that it doesn't really do anything to the inner loop it simply skips to the next repeat of the loop that is specified by n. Of course this terminates all of the nested loops.

Perhaps an example will make this odd idea easier to follow:

for($i=1;$i<=5;$i++){
for($j=1;$j<=3;$j++){
if($j==2)continue 2;
echo($i ." ". $j ."<br/>";);
}
}

Now when $j is equal to 2 control skips to the end of the outer for loop and we immediately move on to the next value of $i. It is as if the inner loop reaches out and grabs the loop n levels up and makes it move on to the next repeat.

That is, when the outer loop starts, $i=1 and the inner loop initially repeats for $j=1. When the inner loop reaches $j=2 it triggers the continue 2 which causes the outer loop to move on to $i=2 and hence the inner loop restarts with $j=1 again.

The result is that the sequence generated is:

1,1 2,1 3,1 4,1 5,1

Although the continue, or equivalently the continue 1 instruction is occasionally useful in making clear what happens in a loop, it seems wise to avoid the continue n instruction at all costs. It is logical and you can see why it was added to PHP, but on the other hand, it is confusing, it is unclear and most of all it is unnecessary.

After all break and continue are little more than slightly constrained "goto" instructions and are capable of making your code just as difficult to follow. In conclusion you should try to minimise their use and only use them when they make what you are doing clear.

The break and the continue can be used within other PHP control statements to break out or to move to the next iteration, not just loops. However, it is in loops that they find their main use and hence are more liable to misuse. The break is particularly important in the case of the switch instruction where its use is uncontroversial and standard/recommended.

Banner

<ASIN:1430210117>

<ASIN:0596157134>



Last Updated ( Monday, 22 March 2010 )