Home > Backend Development > PHP Tutorial > Detailed graphic tutorials on while loop statements, do-while loop statements, and for loop statements

Detailed graphic tutorials on while loop statements, do-while loop statements, and for loop statements

伊谢尔伦
Release: 2023-03-11 08:28:01
Original
9609 people have browsed it

1. while loop Statement format:

1. Format:

while(循环的条件){
    循环语句;
}
Copy after login

2 , Things to note when using while loop statements:
1) While loop statements generally control the number of times they loop through a variable.
2) If the loop body code of the while loop statement has only one statement, the curly braces can be omitted. But like ifjudgment statement, it is not recommended that you omit it.
3) The judgment condition of the while loop statement cannot be followed by a semicolon, otherwise it will affect the execution effect.

Leave you with two entertainment programs:
1. Implement a guessing number game. If you don’t guess correctly, you can continue to enter the number you guessed. If you guess correctly, stop the program.

2. The upgraded version of the guessing game can only guess three times at most. If there is still the last chance left, the user will be reminded.

2. do while loop statement (less used, while is more close to people’s thinking):
1. Format:

do{
    循环语句;
}while(判断条件);
Copy after login

2. while loop The difference between statement and do-while loop statement:
while loop statement is judged first and then executed, while do-while loop statement is executed first and then judged. It will be executed at least once regardless of whether the condition is met or not.

3. Example:

1) Analysis reason: In java, the java compiler does not allow writing nonsense. Because false is a constant, jvm will recognize the constant value, while(false) is nonsense, so an error is reported.
2) How to solve: Using variables will not be recognized by jvm, so no more errors will be reported.

4. Comparison between while and do while:

3. for loop statement:
1. Format:

for(初始化语句;判断语句;循环后的语句){    循环语句;}
Copy after login

2. The execution flow of the for loop statement:

3. Things to pay attention to in the for loop statement:
1) The writing method of for(;;) is an infinite loop statement, equivalent to while(true); there can be no three statements in the for loop statement, but there must be no less semicolons, otherwise an error will be reported
2) The initialization statement of the for loop statement will only be executed once, only in the first It is only executed during the second cycle.
3) When the loop body statement of the for loop statement has only one sentence, the curly braces can be omitted. But it is not recommended to omit it.

The above is the detailed content of Detailed graphic tutorials on while loop statements, do-while loop statements, and for loop statements. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template