Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Looping statement

4,003 views

Published on

Published in: Education, Technology

Looping statement

  1. 1. LOOPING STATEMENT IN PHP<br />1<br />www.YouStudy.IN<br />
  2. 2. Looping statement<br />Loops execute a block of code a specified number of times, or while a specified condition is true.<br />In PHP, the following looping statements are used:<br />The While Loop <br />The Do...While Loop <br />The For Loop <br />The Foreach Loop <br />Break and Continue Statements <br />2<br />www.YouStudy.IN<br />
  3. 3. The while loop<br />While structure is another type of loop statements, where the condition is checked at first, the iteration will not stop even if the value changes while executing statements.<br />3<br />www.YouStudy.IN<br />
  4. 4. Syntax <br /> while (condition) {  code to be executed;}<br />4<br />www.YouStudy.IN<br />
  5. 5. Example <br /><?php$i=0;while ($i <= 10) { // Output values from 0 to 10   echo "The number is ".$i."<br />";   $i++;}?><br />5<br />www.YouStudy.IN<br />
  6. 6. The do while loop<br />Do While statement is same as the while statement, the only difference is that it evaluates the expression at the end.<br />6<br />www.YouStudy.IN<br />
  7. 7. Syntax <br />do {   code to be exected;}while (condition);<br />7<br />www.YouStudy.IN<br />
  8. 8. Example <br /><?php $i = 0;do {   echo "The number is ".$i."<br/>";   $i++;} while ($i <= 10);?><br />8<br />www.YouStudy.IN<br />
  9. 9. The For Loop<br />The for loop is used when you know in advance how many times the script should run.<br />9<br />www.YouStudy.IN<br />
  10. 10. Syntax <br />for (initialization; condition; increment) {   code to be executed;}<br />10<br />www.YouStudy.IN<br />
  11. 11. For loop cont..<br />he For statement takes three expressions inside its parentheses, separated by semi-colons. When the For loop executes, the following occurs:<br />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. <br />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. <br />The update expression increment executes. <br />The statements execute, and control returns to step 2.<br />11<br />www.YouStudy.IN<br />
  12. 12. Example <br /><?phpfor ($i=0; $i <= 10; $i++) {   echo "The number is ".$i."<br />";}?><br />12<br />www.YouStudy.IN<br />
  13. 13. The Foreach Loop<br />For Each structure is a loop structure used for arrays<br />13<br />www.YouStudy.IN<br />
  14. 14. Syntax <br />foreach (array as value){   code to be executed;}    foreach (array as key => value){   code to be executed;}<br />14<br />www.YouStudy.IN<br />
  15. 15. Example <br /><?php$email = array('john.smith@example.com', 'alex@example.com');foreach ($email as $value) {   echo "Processing ".$value."<br />";}?><br />15<br />www.YouStudy.IN<br />
  16. 16. The break statement<br />Break ends the execution of the for, for each, while, do-while or switch statement.<br />16<br />www.YouStudy.IN<br />
  17. 17. Syntax <br />Break (Optional numeric argument) <br />17<br />www.YouStudy.IN<br />
  18. 18. Example <br /><?php <br />$c = 0; <br />while (++$c) <br />{ <br />switch ($c) <br />{ <br />case 5: <br />echo "At 5; "; break 1; <br />case 10:<br />echo "At 10; quitting "; break 2;<br />default: <br />break; <br />} } ?> <br />18<br />www.YouStudy.IN<br />
  19. 19. The continue statement<br />"Continue" is used to skip the current loop iteration and continue with the next iteration of the loop. But "Break" is used to exit from the whole loop.<br />19<br />www.YouStudy.IN<br />
  20. 20. Syntax <br />Continue (Optional numeric argument); <br />20<br />www.YouStudy.IN<br />
  21. 21. Example <br /><?php<br />for ($c = 0; $c <=10; ++$c)<br /> {<br />if ($c == 5)<br /> {<br />continue;<br />}<br />print "$c "; <br />} <br />?> <br />21<br />www.YouStudy.IN<br />
  22. 22. The End<br />THANK U<br />22<br />www.YouStudy.IN<br />

×