Bifalgorithm post · Computer science one · For Loops
How are for loops structured in C?
How are for loops structured in C? (Computer science one · For Loops) — "17.2. For Loops\n\nFor loops in C use the familiar syntax of placing the initializatio…
Ideas in this flowchart
- For loops in C
- The for keyword combines initialization, continuation condition, and iteration on one line.
- The initialization and continuation condition are separated by semicolons; the iteration statement is not followed by one.
- Example: for(i=1; i<=10; i++) { /* perform some action */ }
- The opening curly bracket appears on the same line as the for keyword.
- Statements inside the loop body are indented at the same indentation level.
- In the example, the index variable i is declared before the for loop.
- Some languages allow the index variable to be declared in the initialization statement.
- Declaring the index variable in the loop scopes it to the loop, so it is unavailable before and after the loop body.
- C89 and earlier standards require the variable to be declared before the loop.
- C99 and newer standards allow declaring the variable in the initialization statement.
- Some compilers also support the newer syntax through non-standard C extensions.
- For maximum portability, the older declaration convention is followed.