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

Conditional loops come in two main flavours - the while with exit point at the start and do while with exit point at the end. If you want a primer on the basics see PHP Loops.

Banner

There is a great deal to be said for keeping the number of different types of control structures that you use in a program limited. It's hard enough to write one type of loop accurately without diversifying into multiple loop forms. However, PHP does provide a great deal of flexibility in the way that you can create loops. In particular you can bring a loop to a close in two different ways - break and continue. Let’s see how these work in detail.

In the early days of programing how to write good code was something that was enforced by rules and languages restrictions. Today we are more sophisticated but with sophistication comes responsibility. You have to have a commitment to evaluating the code you write in terms of how clear an expression of the idea it is. You don't code to save a few key presses or to impress others with just how clever you are. Good code is clear, readable and very, very predictable.

The infinite loop

Before we look at moving the exit point let's find out how to remove it completely! A loop with no exit point is called an infinite loop because it never ends and, believe it or not, infinite loops are sometimes useful. Any of the three standard PHP loops can be converted into infinite loops.

For example:

while(true){
   instructions;
}

This while loop never terminates because the condition is always true and hence the instructions are obeyed repeatedly. Now that you have seen this example casting the same thing as a do-while loop should be simple:

do{
instructions;
}while(true)

but the infinite form of the for loop is just a little more tricky:

for(;true;){
 instructions;
}

Again the loop continues forever because the condition is that brings it to an end is true forever. The only slightly odd thing about the infinite for loop is the way that you can leave out the first and final expressions. You can even leave out the middle expression and the result is an infinite loop even if it does look a little odd:

for(;;){
 instructions;
}

Infinite loops are often the result of a programming error. You simply write a loop that never ends because of a mistake in the exit condition.

Generally the first you know of an infinite loop is that either the program goes into overdrive never stopping but producing some output or other but most often it simply all goes quiet while the loop just loops. In many languages you can stop or "break into" a program that is stuck in an infinite loop but in the case of PHP the program is running on a server usually remote from where you are writing and all you can do is wait. In all cases an infinite loop isn't really infinite - it has a time limit applied which automatically stops it. There is no easy way to abort a program before the time limit even using debugging environments such as Eclipse.

Break

The break command allows you to create an exit point in a loop anywhere you care to place one - i.e. not just at the start or the end.

Within a loop a break transfers control out of the loop to the instruction following the loop. For example:

while(true){
instructions1;
if(condition)break:
instructions2;
}

This while loop initially looks like an infinite loop (see the previous section) but the if statement and the break bring it to a conclusion. When the condition is true the if statement executes the break which transfers control to the instruction following the loop. This is neither a while with an exit point at the start nor is it a do-while with an exit point at the end but something intermediate as it has its exit point in the middle. Notice that instructions1 are obeyed at least once but instructions2 might be completely skipped. Also notice that instructions1 are always executed once more than instructions2.

loops3

At one time such hybrid loops were, and in some quarters still are, regarded as evil inventions. Loops should always exit at the start or the end so that programs are easier to understand and easier to debug. This is true but today we tend to hope that programmers are smarter and able to interpret the meaning of something with sensitivity.

There are loops where you need to do something before the condition is ready to test and condition is met then nothing more needs to be done. If the condition isn't met then some post condition work needs to be done and then we get ready to test again. It is a bit like a recipe where the instructions naturally say

 "repeat-whisk, test - stop if ready, add some more stuff"

There is very clearly and naturally something you do before the test and something you do after the test.

Notice that you can always transform a break loop into a standard loop by moving things around. The instruction

 "repeat-whisk, test - stop if ready, add some more stuff"

is usually equivalent to

 "repeat- test - stop if ready, add some more stuff whisk,"

The only disadvantage of this alternative is that you have to perform a possibly necessary test before the first whisk operation. You can dispense with this initial test and move the test to the end of the loop if it is ok to "add some more stuff" before the first whisk and test.

 "repeat-, add some more stuff,whisk, test - stop if ready"

Exactly what you can convert a loop with a middle exit point into depends on the nature of the operations involved but the important point is that all loops can be converted to while loops. However, it can still be easier to write the loop using a break.

Notice also that the break will transfer control out of any loop and so you can write:

do{
instructions1;
if(condition)break:
instructions2;
}while(true)

or

for(;;){
instructions1;
if(condition)break:
instructions2;

}

<ASIN:0470563125>
<ASIN:0596157134>



Last Updated ( Monday, 22 March 2010 )