April 6, 2010

Iteration and Loops in PHP


A loop is a sequence of instruction s that is continually repeated until a certain condition is reached. Loops are basically types of control construct. Loops can be combined so that they are executed at the same time, thus improving the run-time efficiency. Loops save the time of the computers by setting a single code for recurring tasks. They are good because once they are compiled they run over and over again until they reach their limit. Hence resulting in improved efficiency. PHP also supports the following loops:

The while Structure
A while statement executes a code block until a condition is set:


















This prints the table of 2. The instructions written inside the While Loop continues to execute until the value of $a exceeds 10.

Infinite loops are usually not a good thing, but, because PHP provides the proper mechanism for interrupting the loop at any point, they can also be useful.























In this block of code, the (true) condition is always satisfied and, therefore, the compiler will be more than happy to go on repeating the code block forever. Inside the code block itself, we perform two if-then conditions, and the second one is dependent on the first so that the $b > 50 will only be evaluated after $a > 100, and, if both are true, the break statement will cause the execution point to exit from the loop into the preceding scope. Naturally, we could have written this loop just by using the condition ($a <=100 && $b <= 50) in the while loop, but it would have been less efficient because we’d have to perform the check twice. (Remember, $b doesn’t increment unless $a is greater than 100.) If the second condition were a complex expression, our script’s performance might have suffered.


The do-while Structure
The major problem with the while() loop is that, if the condition never evaluates to True, the statements inside the code block are never executed. In some cases, it might be preferable that the code be executed at least once, and then the condition evaluated to determine whether it will be necessary to execute it again.
This can be achieved in one of two ways: either by copying the code outside of the while loop into a separate code block, which is inefficient and makes your scripts more difficult to maintain, or by using a do-while loop:











In this above block of code, $a will be printed once even after the condition is false ($a is not less that 10) because we are using do-while loop which executes the statements written inside the do while loop at least one time even if the condition is false.


The For Loop
The statements in the for loop repeat continuously for a specific number of times. The while and do-while loops repeat until a certain condition is met. The for loop repeats until a specific count is met. You can use a for loop when the number of repetition is know, or can be supplied by the user.









As you can see, the declaration of a for loop is broken in to three parts:
The first expression is known as start Expression. It is evaluated before the loop begins. This start Expression is evaluated only once at the beginning of the loop.
The second represents the condition that must be satisfied for the loop to continue. Finally, the third contains a set of instructions that are executed once at the end of every iteration of the loop before the condition is tested.

Naturally, you can also create a for loop that is infinite in a number of ways, in fact. You could omit the second expression from the declaration, which would cause the interpreter to always evaluate the condition to true. You could omit the third expression and never perform any actions in the code block associated with the loop that will cause the condition in the second expression to be evaluated as true. You can even omit all three expressions using the form for(;;) and end up with the equivalent of while(true).

You can also use while loop inside the For Loop. This can be illustrated more clearly from the following example:






















By executing the above block of code it will print the table from 1 to 10.
 


Share/Bookmark

0 comments:

Post a Comment