Learning notes for forwhile loop statements in php _ PHP Tutorial

Source: Internet
Author: User
Learning notes for forwhile loop statements in php. This article introduces how to use basic loop statements in php, including for loop, while loop, and dowhile loop. This article is suitable for beginners of php. This article introduces how to use basic loop statements in php, including for loop, while loop, and do while loop. This article is suitable for beginners of php.

You can use the for statement to control multiple variables to implement advanced applications with multiple loops.

The following shows a program that uses the for statement to output a 9*9 multiplication table:

The code is as follows:

For ($ I = 1; $ I <= 9; $ I ++ ){
For ($ j = 1; $ j <= $ I; $ j ++ ){
$ Sum = $ I * $ j;
Echo $ sum. "t ";
}
Echo"
";
}
?>

In addition to the while loop statements mentioned above, php also provides for loop statements to implement the same function. In addition, the for statement can implement a more complex and multi-function loop. Any while loop can be replaced by a for loop.

Basic structure:

For (expression 1; expression 2; Expression 3 ){
Execution statement body
}

The execution process is as follows:

1. execute expression 1 first;

2. judge whether expression 2 is true or false. if expression 2 is false, the php statement is executed out of the for loop. if expression 2 is true, the statement body is executed in the for loop;

3. execute expression 3;

4. Return to step 4 for cyclic operation;

5. jump out of the for statement until the end of the loop.

Flowchart:

Instance:

The code is as follows:

For ($ I = 0; $ I <= 9; $ I ++ ){
$ Sum + = $ I;
Echo $ sum. "t ";
}
?>


Do... The while loop statement is a variant of the while loop. its function is similar to that of the while loop. it only checks whether the expression is true after executing the loop. The basic structure is as follows:

Do {;
Statement body
} While (expression)
Do... The while loop statement first executes the body of the statement, and then judges the conditions of the expression. if the value is true, the system returns the loop once. if the value is false, the loop jumps out.

Instance:

The code is as follows:

$ I = 1;
$ Sum = 0;
Do {
$ Sum + = $ I;
Echo $ sum. "n ";
$ I ++;
} While ($ I <= 10)
?>

While loop statement
The while statement is used in php to implement loops. its basic structure is as follows:

While (judgment statement ){
Execution statement body;
}
Judgment statements generally use relational or logical operators as judgment conditions.

When the statement is true, the statement body is executed, and then the expression value is checked. if the statement is true, the statement is executed again. The loop is exited when the statement is false.

Instance:

The code is as follows:

$ I = 0;
While ($ I <9 ){
$ I ++;
Echo $ I ."
";
}
?>


While and do... While differences:

The main difference between the two is do... The first loop of the while statement must be executed.

If the execution statements of the two loop bodies are the same, their running results are generally the same, but when the expression is false at the beginning, the running results of the two loops are different.

Instance:

The code is as follows:

/* While loop */
$ A = 9;
While ($ a> 10 ){
Echo "entering the while loop statement body ";
}
/* Do... while loop */
Do {
Echo "go to do... while loop statement body ";
} While ($ a> 10)
?>


The methods for php to exit the loop include the break and continu statements, which are used to exit the loop program when the judgment conditions are met.

Break statement usage:
When the judgment condition value is true, the entire loop is terminated before execution.

Instance:

The code is as follows:

/* Circular area with an output area less than 100 */
For ($ r = 1; $ r ++ ){
$ A = 3.14 * $ r;
If ($ A> 50) break;/* an endless loop is formed if no break exists */
Echo $ ."
";
}
?>

Continue statement usage:
The function of the continue statement is to end this loop and enter the next loop, instead of exiting the entire loop program.

Instance:

The code is as follows:

/* Output a singular value less than 10 */
For ($ I = 1; $ I <= 10; $ I ++ ){
If ($ I % 2 = 0) continue;
Echo $ I. "t ";
}
?>

What is the difference between a break and a continue statement:

From the above example, we can see that the roles of break and continue statements in the exit loop are essentially different.

Continue only ends this loop, and then returns the loop body to continue executing the next loop;

Break immediately terminates the entire loop and does not repeat the execution.

When using the while loop, this article is very suitable for beginners of php ....

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.