The usage of the PHP loop statement for while doing while

Source: Internet
Author: User
Tags logical operators php tutorial

Usage of the PHP tutorial loop statement for while doing while
<?php
Loop structure
One, while loop
while (expression)
{
loop body;//Repeat until the expression is false
}

<?php
$num = 1;

while ($num <= 10) {
Print "number is $num <br/>";
$num + +;
}

print ' Done. '
?>

There is a certain difference between a Do While loop statement and a while, and the difference is that a does while a while must be true will execute first, regardless of whether the condition is true.

do {
echo "Mmmmm ... I Love cookies! *munch Munch munch* ";
while ($cookies > 1);

The output is.

Mmmmm ... I Love cookies! *munch Munch Munch


Third, for Loop

Depending on the cycle conditions, there are two types of loops

One: Counting loops (generally used for)
Another: Conditional loops (general use while do-while)

For
(EXPR1; expr2; expr3) {
Statement
}

Where the EXPR1 is the initial value of the condition. EXPR2 is the condition of judgment, which is usually judged by the logic operation symbol (logical operators). EXPR3 the part to be executed after the execution of statement to change the condition for the next round of judgment, such as add one. Wait a minute. and statement for the implementation of the conditions of the program, if the program has only one line, you can omit the curly braces {}.

The following example, written with a For loop, "dare not later", can be taken and compared with a while loop.

<?php
For ($i =1 $i <=10; $i + +) {
echo "$i. <br>n";
}
?>


Output table

<HEAD>
<title>value of Shares</title>
<BODY>
<table border=1>
<?php
for ($shares = 1; $shares <= $shares + +) {
$cost = $shares * 20;
echo "<tr><td>the cost of $shares share/s is $cost #x0024s </TD>", "N";
$dividend = $cost * 0.10;
echo "<td>the dividend is $dividend #x0024s </TD></TR>", "N";
}
?>
</TABLE>
</BODY>
</HTML>


Cumulative calculation

<?php
$count = 1;
while ($count < 5) {
echo "$count squared =". Pow ($count, 2). " <br/> ";
$count + +;
}
?>


Do While loop

<title>the Do...while statement</title>
<body>
<div>
<?php
$num = 1;
do {
Print "Execution number: $num <br/>n";
$num + +;
while ($num > && $num < 400);
?>
</div>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.