Several usage analysis of phpfor loop statements

Source: Internet
Author: User
Several usage analysis of phpfor loop statements. The syntax of the for loop is as follows: Copy the code for (expr1; expr2; expr3) statement below to talk about several useful variants of the for statement. 1. the infinite loop is also called an endless loop. The syntax for not opening a for loop is:

The code is as follows:
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.


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:

For (;;){
// If it is 2199 AD, then jump out of the loop http://www.hzhuti.com/nokia/n93/
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:

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.

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:

For ($ I = 1; $ I <= 10; $ I ++ ){
For ($ j = 1; $ j <= 10; $ j ++ ){
$ M = $ I * $ I + $ j * $ j;
Echo "$ m n
";
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:

// 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

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

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.

From php development

The syntax of the http://www.bkjia.com/PHPjc/478302.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478302.htmlTechArticlefor loop is: the code is as follows: for (expr1; expr2; expr3) statement below to talk about several useful variants of the for statement. 1. infinite loop is also called an endless loop...

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.