Home  >  Article  >  When to use for loop

When to use for loop

卡哇伊
卡哇伊Original
2020-07-14 10:30:148272browse

For loop execution method: first perform the initialization operation; then determine whether the termination condition is met, and if so, execute the statements in the loop body; finally execute the iteration part, and after completing a loop, re-judge the termination condition.

When to use for loop

while statement

The while statement implements a "when type" loop, and its general format is:

[Initialization]

while ([Boolean expression]){

body;

[Iteration]

}

1. When the value of the Boolean expression is true, the statement in the curly brackets is executed in a loop. And the initialization part and the iteration part are optional.

2. The while statement first calculates the termination condition, and only executes the statements in the loop when the condition is met. This is the characteristic of the "when type" cycle.

do-while statement

The do-while statement implements a "until" loop. Its general format is:

[Initialization]

do{

body;

[Iteration]

}while ([Boolean expression])

1.do-while statement First the loop body is executed, and then the termination condition is calculated. If the result is true, the statement in the curly brackets is executed in a loop until the result of the Boolean expression is false.

2. Different from the while statement, the loop body of the do-while statement is executed at least once, which is a characteristic of the "until" loop.

for statement

The for statement is also used to implement "when type" loops. Its general format is:

for ([Initialization]; [Boolean expression];[Iteration]){

body;

}

1. When the for statement is executed, the initialization operation is first performed, and then it is determined whether the termination condition is met. , if satisfied, execute the statements in the loop body, and finally execute the iteration part. After completing a cycle, re-judge the termination condition.

2. You can declare a variable in the initialization part of the for statement, and its scope is a for statement.

3. The for statement is usually used to execute situations where the number of loops is determined (such as operating on array elements). It can also be used to execute situations where the number of loops is uncertain based on the loop end condition.

4. You can use comma statements in the initialization part and the iteration part to perform multiple actions. A comma statement is a sequence of statements separated by commas. For example:

for(i=0,j=10;i

……

}

5. The initialization, termination and iteration parts can all be empty statements (but the semicolon cannot). When all three are empty, it is equivalent to an infinite loop.

The above is the detailed content of When to use for loop. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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