Tuesday 13 October 2015

PHP : Looping Statements


PHP: Looping Statements



In programming it is often necessary to repeat the same block of code a given number of times, or until a certain condition is met. This can be accomplished using looping statements. PHP has two major groups of looping statements: for and while. The For statements are best used when you want to perform a loop a specific number of times. The While statements are best used to perform a loop an undetermined number of times. In addition, you can use the Break and Continue statements within looping statements.


The While statement executes a block of code if and as long as a specified condition evaluates to true. If the condition becomes false, the statements within the loop stop executing and control passes to the statement following the loop. The While loop syntax is as follows:



The block of code associated with the While statement is always enclosed within the { opening and } closing brace symbols to tell PHP clearly which lines of code should be looped through.
While loops are most often used to increment a list where there is no known limit to the number of iterations(lelaran) of the loop. For example:


Let's have a look at the examples. The first example defines a loop that starts with i=0. The loop will continue to run as long as the variable i is less than, or equal to 10. i will increase by 1 each time the loop runs:





Now let's consider a more useful example which creates drop-down lists of days, months and years. You can use this code for registration form, for example.



Hasil paparan kod di atas. 


Note: Make sure the condition in a loop eventually becomes false; otherwise, the loop will never terminate.
Pastikan syarat loop menjadi nilai false, jika tidak penyataan loop akan berterusan tanpa henti.

The Do...While Loop
The Do...While statements are similar to While statements, except that the condition is tested at the end of each iteration, rather than at the beginning. This means that the Do...While loop is guaranteed to run at least once. The Do...While loop syntax is as follows:

The example below will increment the value of i at least once, and it will continue incrementing the variable i as long as it has a value of less than or equal to 10:



The For Loop
The For statement loop is used when you know how many times you want to execute a statement or a list of statements. For this reason, the For loop is known as a definite loop. The syntax of For loops is a bit more complex, though for loops are often more convenient than While loops. The For loop syntax is as follows:


The For statement takes three expressions inside its parentheses, separated by semi-colons. When the For loop executes, the following occurs:
  • The initializing expression is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity.
  • The condition expression is evaluated. If the value of condition is true, the loop statements execute. If the value of condition is false, the For loop terminates.
  • The update expression increment executes.
  • The statements execute, and control returns to step 2.
Have a look at the very simple example that prints out numbers from 0 to 10:

Next example generates a multiplication table 2 through 9. Outer loop is responsible for generating a list of dividends, and inner loop will be responsible for generating lists of dividers for each individual number:
At last let's consider the example that uses 2 variables. One to add all the numbers from 1 to 10. The other to add only the even numbers.

The Foreach Loop
The Foreach loop is a variation of the For loop and allows you to iterate over elements in an array. There are two different versions of the Foreach loop. The Foreach loop syntaxes are as follows:
The example below demonstrates the Foreach loop that will print the values of the given array:
PHP executes the body of the loop once for each element of $email in turn, with $value set to the current element. Elements are processed by their internal order. Looping continues until the Foreach loop reaches the last element or upper bound of the given array.
An alternative form of Foreach loop gives you access to the current key:

In this case, the key for each element is placed in $key and the corresponding value is placed in $value.
The Foreach construct does not operate on the array itself, but rather on a copy of it. During each loop, the value of the variable $value can be manipulated but the original value of the array remains the same.

Break and Continue Statements
Sometimes you may want to let the loops start without any condition, and allow the statements inside the brackets to decide when to exit the loop. There are two special statements that can be used inside loops: Break and Continue.
The Break statement terminates the current While or For loop and continues executing the code that follows after the loop (if any). Optionally, you can put a number after the Break keyword indicating how many levels of loop structures to break out of. In this way, a statement buried deep in nested loops can break out of the outermost loop.
Examples below show how to use the Break statement:
The Continue statement terminates execution of the block of statements in a While or For loop and continues execution of the loop with the next iteration:


Disediakan Oleh : Abd Rahman Bin Sirat
Rujukan http://webcheatsheet.com/php/loops.php









0 comments:

Post a Comment