1 / 23

LOOPING STRUCTURE

Padasalai. Chapter - 7. LOOPING STRUCTURE. MOHAMED FAKRUDEEN, PGT-COMPUTER SCIENCE SHAMROCK MATRIC. HR. SEC. SCHOOL, MOGAPPAIR, CHENNAI-107. Learning objectives. To understand the importance of looping structure. To know different types of looping structure in PHP.

mjesus
Télécharger la présentation

LOOPING STRUCTURE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Padasalai Chapter - 7 LOOPING STRUCTURE MOHAMED FAKRUDEEN, PGT-COMPUTER SCIENCE SHAMROCK MATRIC. HR. SEC. SCHOOL, MOGAPPAIR, CHENNAI-107

  2. Learning objectives • To understand the importance of looping structure. • To know different types of looping structure in PHP. • To know the basic fundamental logic creation using looping structure.

  3. Looping structure • Looping structure are useful for writing iteration logics. • It is most feature of many programming languages , including PHP.

  4. Categories of “loop” • They are implemented using the following categories. • for loop • While loop • for each loop • Do while loop

  5. For loop: • For loop is an important functional looping system which is used for iteration logics when the programmer know in advance how many times the loop should run.

  6. SYNTAX for( init counter; test counter; increment counter) { code to be executed; }

  7. PARAMETERS • init counter: Initialize the loop initial value. • Test counter: Evaluated for every iteration of the loop. • If it evaluatesto TRUE, the loop continues. If it evaluates to FALSE, the loop ends. • Increment counter: Increases the loop counter value.

  8. EXAMPLE <?php for( $i = 0; $i<= 10; $i++) { echo “ The numbers is: $i<br>”; } ?>

  9. For loop Structure and Flow chart

  10. Foreach loop: • Foreach loop is exclusively available in PHP. It works only with arrays. • The loop iteration deepens on on each KEY value pair In the array. • For each,loop iteration the value of the current array element is assigned to $ value variable and the array pointer is shifted by one, until it reaches the end of the array element.

  11. Foreach loop Structure and Flow

  12. SYNTAX: foreach ($array as $value) { code to be executed; }

  13. EXAMPLE: • <?php $Student_name=array (“Magilan”, “Iniyan”, “Nilani”, “Sibi”, “Shini”) ; foreach ($Student _name as $value) { echo “ $value <br>” ; } ?>

  14. While loop: • While loop is an important feature which is used for simple iteration logics. It is checking the condition whether true or false. It executes the loop if specified condition is true. Refer figure 7.4.

  15. While loop Structure and Flow

  16. SYNTAX: while ( condition is true) { code to be executed; }

  17. EXAMPLE: • <?php $Student_count = 10; $Student_number = 1; while ($Student_number <=$Student _count) { echo “ The student number is: $Student_number <br>” ; $Student_number ++ ; } ?>

  18. Do while loop: • Do while loop always run the statement inside of the loop block at the first time execution. Then it is checking the condition whether true or false. It executes the loop if specified condition is true . Refer figure 7.4.

  19. SYNTAX: do { code to be executed; } while (condition

  20. DoWhile loop Structure and Flow Chart

  21. EXAMPLE • <?php $Student_count = 10; $Student_number = 1; do { echo “ The student number is: $Student_number <br>” ; $Student_number ++ ; } while ( $Student_number <= $Student_count) ?>

  22. Points to remember • PHP while loops execute a block of code while the specified condition is true. • The for loop is used when you know in advance how many times the script should run. • The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. • do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true

  23. Next topic • Forms and Files

More Related