for Three-part loop statement
for Three-part loop statement
for's sections are optional except semicolons; a missing condition is true, and continue runs the step. C99 allows initializer declarations; C89 does not.
| Category | Control flow |
|---|---|
| Kind | Keyword / Statement |
| C standard | C89 / C90 |
📝 Syntax
for (init; cond; step) statement;
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
sum = 10
🏷️ Related control-flow keywords
📚 Related Tutorials
❓ FAQ
QWhy can't for (int i = 0; ...) be compiled as C89?
ADeclaring a variable in the for initializer was introduced in C99. In C89, place int i; in the enclosing block's declaration section and write for (i = 0; ...), as this entry's example does.
QCan all three expressions in a for loop be omitted?
AYes, but the two semicolons cannot. In for (;;), the condition is treated as always true, so the body must provide a termination path through break, return, or another suitable mechanism.
QDoes continue skip i++ in a for loop?
ANo. After skipping the rest of the body, continue executes the third expression in the for header, then performs the next condition test.