A detailed example of the "Do...while" loop statement of the PHP loop control statement

Source: Internet
Author: User
In front of you to explain the PHP loop statement in the while statement loop, in fact, while the statement has another form of expression, which is what we are going to explain to you today, "Do...while" loop statement

Do...while The concept of cyclic statements

Do...while loop Statements And while loop statements are very similar, the difference between the two is that the Do...while Loop statement is more than a while loop, while the loop statement, when the expression is false, will directly jump out of the current loop, and do ... The While Loop statement executes the PHP statement first, judging the conditional expression. Like, we usually go to the water dispenser, there are two kinds of people, a person will first look at the bucket there is no water, if there is, then press the button to connect the water, this is the while loop ; another person whether there is no water, the first to press the button, water is directly connected, if there is no effluent, Again to see if there is water in the bucket, and then silently leave, this is the do...while cycle.

Do...while syntax for a looping statement

Do {    statement} while (expr);

Syntax Explanation:

As you can see from the syntax, our conditional expression is placed behind the PHP statement statement, which means that no matter whether the expr expression is true,do...while, the loop executes at least once.

Now, let's do...while the flowchart of the loop statement.

Do...while Loop Statement Instance

This example compares the difference between a do...while loop and a while loop by running two statements. The code is as follows

<?phpheader ("Content-type:text/html;charset=utf-8");    Set the encoding $num=1;                             Declares an integer variable $sumwhile ($num!=1) {                     //using the while loop output    echo "will not see";                This statement will not output} do{                                  //Use do...while loop output    echo "will see";                   This sentence will output}while ($num!=1); >

Code Run Result:

As can be seen from the above example, our conditional expression is false, while the loop is not output, and the Do...while loop executes once regardless of whether the expression is false.

In the next section, we will explain the "for" loop in the PHP looping statement.

Related Article

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.