Understanding Looping Structures in PHP

 
LOOPING STRUCTURE
 
Chapter - 7
 
MOHAMED FAKRUDEEN, PGT-COMPUTER SCIENCE
MOHAMED FAKRUDEEN, PGT-COMPUTER SCIENCE
SHAMROCK MATRIC. HR. SEC. SCHOOL, MOGAPPAIR, CHENNAI-107
SHAMROCK MATRIC. HR. SEC. SCHOOL, MOGAPPAIR, CHENNAI-107
 
Padasalai
 
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.
 
Looping structure
 
Looping structure are useful for writing iteration
logics.
It is most feature of many programming languages ,
including PHP.
 
Categories of “loop”
 
They are implemented using the following categories.
  for loop
  While loop
  for each loop
  Do while loop
 
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.
 
SYNTAX
 
 for( init counter; test counter; increment counter)
{
         code to be executed;
 }
 
P
P
A
A
R
R
A
A
M
M
E
E
T
T
E
E
R
R
S
S
 
init counter: 
Initialize the loop initial value.
   
Test counter: 
Evaluated for every iteration of the loop.
If it  
evaluates
 to TRUE, the loop continues. If it
evaluates to FALSE, the loop ends.
  
Increment counter: 
Increases the loop counter value.
 
EXAMPLE
 
<?php
   for( $i = 0; $i<= 10; $i++)
{
          echo “ The numbers is: $i<br>”;
  }
?>
 
For loop Structure and Flow chart
 
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.
 
Foreach loop Structure and Flow
 
SYNTAX:
 
foreach ($array as $value)
{
    code to be executed;
  }
 
 
 
 
 
EXAMPLE:
 
<?php
    $Student_name=array (“Magilan”, “Iniyan”, “Nilani”,
“Sibi”, “Shini”) ;
    foreach ($Student _name as $value)
 
{
         
  
 echo “ $value <br>” ;
     
 
}
     ?>
 
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.
 
While loop Structure and Flow
 
SYNTAX:
 
 while ( condition is true)
{
          code to be executed;
 }
 
EXAMPLE:
 
<?php
      $Student_count = 10;
      $Student_number = 1;
      while ($Student_number <=$Student _count)
{
         echo “ The student number is: $Student_number <br>” ;
         $Student_number ++ ;
       }
       ?>
 
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.
 
SYNTAX:
 
do
{
          code to be executed;
} while (condition
 
 
 
 
DoWhile
loop
Structure
and Flow
Chart
 
EXAMPLE
 
<?php
      $Student_count = 10;
      $Student_number = 1;
      do
      {
             echo “ The student number is: $Student_number <br>” ;
             $Student_number ++ ;
      }
      while ( $Student_number <= $Student_count)
      ?>
 
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
 
Next topic
 
Forms and Files
Slide Note
Embed
Share

Looping structures are essential in programming for writing iteration logics. This article delves into the importance of looping structures, different types in PHP, and creating basic logic using loops like for, while, for each, and do-while. Learn the syntax, parameters, examples, and flow charts to enhance your knowledge on looping structures.


Uploaded on Sep 12, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

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 evaluates to 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 Forms and Files

Related


More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#