Usage analysis of php for loop statements

Source: Internet
Author: User
Tags php code

The syntax of the for loop is:

 

The code is as follows: Copy code
For (expr1; expr2; expr3)
Statement

The following describes several useful variants of the for statement.

1. Infinite loop

This is also called an endless loop. It will continue like this without beginning or end.

The code is as follows: Copy code

<? Php
For (;;){
// Place the statements that require continuous execution
}
?>

If an infinite loop is used together with if else, break can also jump out of the loop.

The code is as follows: Copy code

<? Php
For (;;){
// If it is 2199 AD, it will jump out of the loop
If (date ('Y') = '000000 '){
Break;
}
}
?>

2. Use an empty expression

Next we will talk about the use of the null syntax in the initialization statement expr1. The most common function of leaving expr1 blank is to complete more complex initialization.

The code is as follows: Copy code

<? Php
If (isset ($ I )){
Unset ($ I );
If (int) date ('') <2008 ){
$ I = 0;
} Else {
$ I = 1;
}
} Else {
$ I = 3;
}

For (; $ I <10; $ I ++ ){
Echo $ I;
}
?>

In the same way, the iteration expression expr3 may also be left blank. You can also use this to write more complex iterations, such as calling different iterations based on different conditions.

The expr2 field in the for statement is an infinite loop mentioned above. Of course, you can add more complex conditions to determine when to jump out of the loop, which is not repeated here.

3. Multiple cycles

Using multiple loops to control multiple variables is also a feature that is often ignored in the for statement. In the following example, a double loop is usually used in a general task, and a triple or above loop is of little significance.

The code is as follows: Copy code

<? Php
For ($ I = 0, $ j = 10; $ I <= 10; $ I ++, $ j --){
Echo "$ I + $ j = 10rn ";
}
?>

The above code will be output:

0 + 10 = 10
1 + 9 = 10
2 + 8 = 10
3 + 7 = 10
4 + 6 = 10
5 + 5 = 10
6 + 4 = 10
7 + 3 = 10
8 + 2 = 10
9 + 1 = 10
10 + 0 = 10

If we want to skip the loop halfway, how can we operate the instance?

The following is an example of multi-loop nesting:

The code is as follows: Copy code
For ($ I = 1; $ I <= 10; $ I ++ ){
For ($ j = 1; $ j <= 10; $ j ++ ){
$ M = $ I * $ I + $ j * $ j;
Echo "$ m n <br/> ";
If ($ m <90 | $ m> 190 ){
Break 2;
}
}
}

Break 2 jumps out of the double loop. You can take a test and remove 2. The results are completely different. If you do not use the parameters, only this loop exists, and the first loop continues to run.

Note:
Break is used in various loops and switch statements mentioned above. Its role is to jump out of the current syntax structure and execute the following statements. The break statement can contain a parameter n, indicating the number of layers that exit the loop. To jump out of multiple loops, you can use n to represent the number of layers that exit, if the parameter is not included, the loop jumps out.

 

4. More complex expressions

If the three expressions of the for statement are more complex, they can be used to optimize the algorithm. You can even use a for statement without a loop body to complete some tasks. For example, calculation of accumulation or factorial:

The code is as follows: Copy code

<? Php
// Calculates the accumulate result from 1-5, and returns the value to $ j.
For ($ I = 1, $ j = 0; $ I <= 5; $ j + = $ I ++ );
Echo $ j;

// Calculate the factorial result from 1-5. The bin value is $ j.
For ($ I = 1, $ j = 1; $ I <= 5; $ j * = $ I ++ );
Echo $ j;

?>

If I want to execute it in one place, the current loop will be automatically called up and executed-instance

The code is as follows: Copy code

<? Php
For ($ I = 1; $ I <= 100; $ I ++ ){
If ($ I % 3 = 0 | $ I % 7 = 0 ){
Continue;
}
} Else {
Echo "$ I n <br/> ";
}
}
?>

The function of PHP code snippets is to output less than 100 of the natural numbers that cannot be divisible by 7 and cannot be divisible by 3. In a loop, use the if condition statement to determine the number that can be divisible, then execute the continue statement to directly enter the next loop. The following output statement is not executed.

Note:

Continue is used in the loop structure. The control program abandons the code after this loop continue statement and proceeds to the next loop. The continue itself does not jump out of the loop structure, but just gives up this loop. If you use continue in a non-cyclic structure (for example, in the if statement, switch statement), the program will fail.

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.