PHP for loop notation and examples

Source: Internet
Author: User

The For loop is one of the most recent looping statements, and in any language, there is this looping statement, which is also a common loop method in our work.

Syntax rules:

for (EXPR1; expr2; expr3) {

The code to execute

}

Expr1: Indicates where the loop begins

EXPR2: The condition of the loop, if the value is TRUE, the loop continues, and the nested loop statement is executed. If the value is FALSE, the loop is terminated.

EXPR3: evaluated (and executed) after each loop.

Writing a bit obscure, we write the simplest for loop demo!

For loop Demo1:

<?phpfor ($n =1; $n <20; $n + +) {    echo ' for Loop statement ' executes ' $n. ' Times <br> ";}

Execution Result:

The FOR Loop statement executes the 1th time
The FOR Loop statement executes the 2nd time
The FOR Loop statement executes the 3rd time
The FOR Loop statement executes the 4th time
The FOR Loop statement executes the 5th time
The FOR Loop statement executes the 6th time
The FOR Loop statement executes the 7th time
The FOR Loop statement executes the 8th time
The FOR Loop statement executes the 9th time
The FOR Loop statement executes the 10th time
The FOR Loop statement executes the 11th time
The FOR Loop statement executes the 12th time
The FOR Loop statement executes the 13th time
The FOR Loop statement executes the 14th time
The FOR Loop statement executes the 15th time
The FOR Loop statement executes the 16th time
The FOR Loop statement executes the 17th time
The FOR Loop statement executes the 18th time
The FOR Loop statement executes the 19th time

It can be seen that when the $n<20 condition is not satisfied, it does not output $n.

For Loop statement Demo2, use break to jump out for the For loop:

<?phpfor ($n =1; $n <20; $n + +) {    if ($n ==10) {break        ;    }    The echo ' For Loop statement executes the '. $n. ' Times <br> ";}

Output Result:

The FOR Loop statement executes the 1th time
The FOR Loop statement executes the 2nd time
The FOR Loop statement executes the 3rd time
The FOR Loop statement executes the 4th time
The FOR Loop statement executes the 5th time
The FOR Loop statement executes the 6th time
The FOR Loop statement executes the 7th time
The FOR Loop statement executes the 8th time
The FOR Loop statement executes the 9th time

When n equals 10, jumps out of the loop and does not continue. If we just want to jump out of 10, the rest of the execution, can be written like this:

<?phpfor ($n =1; $n <20; $n + +) {    if ($n ==10) {        continue;    }    The echo ' For Loop statement executes the '. $n. ' Times <br> ";}

So we just jump out of a loop and the results are as follows:

The FOR Loop statement executes the 1th time
The FOR Loop statement executes the 2nd time
The FOR Loop statement executes the 3rd time
The FOR Loop statement executes the 4th time
The FOR Loop statement executes the 5th time
The FOR Loop statement executes the 6th time
The FOR Loop statement executes the 7th time
The FOR Loop statement executes the 8th time
The FOR Loop statement executes the 9th time
The FOR Loop statement executes the 11th time
The FOR Loop statement executes the 12th time
The FOR Loop statement executes the 13th time
The FOR Loop statement executes the 14th time
The FOR Loop statement executes the 15th time
The FOR Loop statement executes the 16th time
The FOR Loop statement executes the 17th time
The FOR Loop statement executes the 18th time
The FOR Loop statement executes the 19th time

I blog: PHP for loop Writing and example

PHP for loop notation and examples

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.