Home > Backend Development > PHP Tutorial > Study notes on for while loop statement in php_PHP tutorial

Study notes on for while loop statement in php_PHP tutorial

WBOY
Release: 2016-07-13 17:15:23
Original
1166 people have browsed it

This article will introduce you to the use of basic loop statements in PHP, including the use of for loops, while loops and do while loops. This article is very suitable for beginners of PHP.

Use the for statement to control multiple variables, thereby realizing advanced applications of multiple loops.

Let’s look at a program that uses a for statement to output a 9*9 multiplication table:

The code is as follows Copy code
 代码如下 复制代码

for($i=1;$i<=9;$i++){
for($j=1;$j<=$i;$j++){
$sum=$i*$j;
echo $sum ."t";
}
echo "
";
  } 
?>

for($i=1;$i<=9;$i++){

for($j=1;$j<=$i;$j++){

$sum=$i*$j;

echo $sum ."t";

}
echo "
";
} ?>

In addition to the while loop statement mentioned earlier, PHP also provides a for loop statement to achieve the same function. Moreover, the for statement can implement more complex and functional loops. Any while loop can be replaced by a for loop.

Basic structural form:

for(expression1;expression2;expression3){

Execution statement body

}

The execution process is as follows:

1. Execute expression 1 first;

2. Then judge whether expression 2 is true or false. If it is false, jump out of the for loop and execute the next php statement. If it is true, enter the for loop to execute the statement body;

3. Then execute expression 3;
 代码如下 复制代码

for($i=0;$i<=9;$i++){
$sum+=$i;
echo $sum ."t";
}
?>

4. Return to step 2 for loop operation;

5. Jump out of the for statement until the end of the loop.


Flow chart:

Example:

The code is as follows Copy code

for($i=0;$i<=9;$i++){

代码如下 复制代码

$i=1;
$sum=0;
do{
$sum +=$i;
echo $sum ."n";
$i++;
}while($i<=10)
?>

 

$sum+=$i;

echo $sum ."t";
}

?>



The do...while loop statement is a variant of the while loop. Its function is similar to while, except that it checks whether the expression is true after executing the loop. The basic structure is:

do{;

Statement body

}while(expression)

The do...while loop statement first executes the statement body once, and then judges the condition of the expression. If the value is true, it returns and loops again. If it is false, it jumps out of the loop.

 代码如下 复制代码

$i=0;
while($i<9){
$i++;
echo $i ."
";
 }
?>

Example:
The code is as follows Copy code
$i=1;<🎜> $sum=0;<🎜> do{<🎜> $sum +=$i;<🎜> echo $sum ."n";<🎜> $i++;<🎜> }while($i<=10)<🎜> ?>
while loop statement The while statement is a statement used to implement loops in PHP programs. Its basic structure is as follows: while(judgment statement){ Execution statement body; } Judgment statements generally use relational operators or logical operators as judgment conditions. When the judgment statement is true, the statement body is executed, and then the value of the expression is checked. If it is still true, the execution statement is executed again. Exit the loop until the judgment statement is false. Example:
The code is as follows Copy code
$i=0;<🎜> while($i<9){<🎜> $i++;<🎜> echo $i ."
"; } ?>


The difference between while and do...while:

The main difference between the two is that the first loop of the do...while statement must be executed.

If the execution statements of the two loop bodies are the same, their running results are generally the same, but when the expression is false at the beginning, the running results of the two loops are different.

Example:

Example:
The code is as follows
 代码如下 复制代码

/* while循环 */
$a=9;
while($a>10){
  echo "进入while循环语句体";
 }
 /* do...while循环 */
 do{   
  echo "进入do...while循环语句体";
 }while($a>10) 
?>

Copy code


/* while loop */
$a=9;

while($a>10){

echo "Enter the while loop statement body";

}
 代码如下 复制代码

/* 输出面积在100以内的圆形面积 */
for($r=1;;$r++){
$A=3.14*$r*$r;
if($A>50) break; /* 如果没有break则形成死循环 */
  echo $A ."
";
 }
?>
/* do...while loop */

do{
echo "Enter do...while loop statement body";

}while($a>10)

?>

 代码如下 复制代码

/* 输出10以内的单数 */
for($i=1;$i<=10;$i++){
if($i%2==0) continue;
echo $i ."t";
}
?>

The methods for php to exit the loop include break and continu statements. Their function is to jump out of the loop program when the judgment conditions are met.

break statement usage:

When the value of the judgment condition is true, the entire loop ends early, and then the statements outside the loop are executed.

The code is as follows
Copy code

/* Output the circular area with an area within 100 */ for($r=1;;$r++){ $A=3.14*$r*$r; if($A>50) break; /* If there is no break, an infinite loop will be formed */ echo $A ."
";
} ?> continue statement usage:
The function of the continue statement is to end this loop and enter the next loop, not to exit the entire loop program. Example:
The code is as follows Copy code
/* Output an odd number within 10 */<🎜> for($i=1;$i<=10;$i++){<🎜> if($i%2==0) continue;<🎜> echo $i ."t";<🎜> }<🎜> ?> The difference between break and continue statements: From the above example, we can see that the break and continue statements have essentially different functions in exiting the loop. continue just ends this loop, and then returns to the loop body to continue executing the next loop; break terminates the entire loop immediately and does not execute it repeatedly. http://www.bkjia.com/PHPjc/628843.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628843.htmlTechArticleThis article will introduce you to the use of basic loop statements in php, including for loop, while loop and do How to use while loop, this article is very suitable for beginners of PHP. ...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template