Leaving Parts Blank in a for loop. (#30479)
This commit is contained in:
committed by
Christopher McCormack
parent
8ac6600a23
commit
4fc41ac133
@ -16,7 +16,6 @@ for ( initialization; condition; update ) {
|
|||||||
statement(s);
|
statement(s);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The `for` loop consists of 3 sections:
|
The `for` loop consists of 3 sections:
|
||||||
1. The initialization section (start value)
|
1. The initialization section (start value)
|
||||||
2. Condition (stop value)
|
2. Condition (stop value)
|
||||||
@ -24,6 +23,11 @@ The `for` loop consists of 3 sections:
|
|||||||
|
|
||||||
The initialization statement is executed only once. Then, the condition is evaluated. If the condition is false (0), the `for` loop is terminated. But if the condition is true (nonzero), code inside the block of the `for` loop is executed. Finally, the update expression is executed. This process repeats until the condition is false.
|
The initialization statement is executed only once. Then, the condition is evaluated. If the condition is false (0), the `for` loop is terminated. But if the condition is true (nonzero), code inside the block of the `for` loop is executed. Finally, the update expression is executed. This process repeats until the condition is false.
|
||||||
|
|
||||||
|
You can leave any of the three field blank:
|
||||||
|
- If you leave initialization blank, then there is no initialization phase
|
||||||
|
- If you leave update blank, then there is no update phase
|
||||||
|
- If you leave the condition blank, then practically it becomes an infinite loop unless you break inside loop
|
||||||
|
|
||||||
The `for` loop is commonly used when the number of iterations is known.
|
The `for` loop is commonly used when the number of iterations is known.
|
||||||
|
|
||||||
#### Example
|
#### Example
|
||||||
|
Reference in New Issue
Block a user