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.

CategoryControl flow
KindKeyword / Statement
C standardC89 / 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 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏