Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use for Loop

Shulou Source: shulou.com Published: 2022-06-03 03:52:45 09月27日 Update

This article will explain in detail how to use the for cycle, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

The for loop execution method: first execute the initialization operation; then judge whether the termination condition is satisfied; if so, execute the statement in the loop body; finally execute the iterative part, and re-judge the termination condition after completing a cycle.

While statement

The while statement implements a "equivalent" loop, which has the following general format:

[initialization]

While ([Boolean expression]) {

Body

[iteration]

}

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

The 2.while statement first evaluates the termination condition, and then executes the statement in the loop when the condition is met. This is the characteristic of the "current type" cycle.

Do-while statement

The do-while statement implements a straight-to-type loop, which has the following general format:

[initialization]

Do {

Body

[iteration]

} while ([Boolean expression])

The 1.do-while statement first executes the loop body, then evaluates the termination condition, and if the result is true, the loop executes the statement in curly braces until the result of the Boolean expression is false.

two。 Unlike while statements, the loop body of do-while statements is executed at least once, which is characteristic of a "straight-to-type" loop.

For statement

The for statement is also used to implement the "equivalent" loop, which has the following general format:

For ([initialization]; [Boolean expression]; [iteration]) {

Body

}

When the 1.for statement is executed, the initialization operation is performed first, and then the termination condition is determined. If so, the statement in the loop body is executed, and finally the iterative part is executed. After completing a cycle, re-determine the termination conditions.

two。 You can declare a variable in the initialization section of a for statement whose scope is a for statement.

3.for statements are usually used to perform situations where the number of loops is determined (such as manipulating array elements), or to perform cases where the number of loops is uncertain based on the condition at the end of the loop.

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

For (iMagnum 0, job10, witi)

Tags: Statement loop condition part iteration Boolean expression loop body format comma content situation parenthesis article more times characteristics knowledge article result Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Tech Info OPPO Reno MySQL Redmi