13. Loops

In computer programming, a loop is a sequence of instruction that is continually repeated until a certain condition is reached.

  1. for
  2. foreach
  3. while
  4. do….while
for: Example

<?php
    
    for($i=1; $i<=10; $i++){
    //echo $i,”<br>”;
    }
    //echo “Reverse LOOP<br>”;
    for($i=10; $i>=1; $i––){
    //echo $i,”<br>”;
    }
    for($i=1; $i<=10; $i++){
    //echo $i*$i,”<br>”;
    }
    for($k=1; $k<6; $k++){
		static $r=1; // 1,2,6,24,120
		//$r=$r*$k;
    }
    //echo $r;
    for($a=6; $a>0; $a--){
		static $k = 1; //6,30,120,360,720
		$k = $a*$k; 
	}
	echo $k;
	
	
?>

while Example:

<?php
	$i=1;
	while($i<=10){
	//echo $i;
	$i++;
	}
	$i=10;
	while($i>=1){
	//echo $i,”<br>”;
	$i–-;
	}
?>

do….while example:

<?php
	$i=5;
	do {
		echo $i 
		$i++;  
	}
	while($i>6);
?>
// if while loop & for loop don’t meet any condition, don’t give output
// do….while loop uses for at least 1 output.
foreach Example:
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s