while Pretest loop

while Pretest loop

while tests a scalar condition before each pass; false initially means zero iterations. Ensure the condition changes; break exits and continue retests.

CategoryControl flow
KindKeyword / Statement
C standardC89 / C90

📝 Syntax

while (expr) statement;

▶ Example

Edit the C code below and click Run; the server compiles and executes it:

Output:

3
2
1

📚 Related Tutorials

❓ FAQ

QHow do while and do while differ in execution count?
Awhile tests first and may execute zero times; do while executes the body first and therefore runs at least once. Both continue subsequent iterations while their conditions remain true, not while they are false.
QHow can a while (1) loop exit normally?
AUse break when a condition inside the body is met, or return to leave the function. Long-running tasks such as servers may intentionally continue. Examples and terminating programs should avoid waits with no exit path.
QDoes continue in a while loop retest the condition?
AYes. continue jumps directly to the next evaluation of the while control expression. If the statement that updates the condition comes after continue, the same condition may repeat indefinitely.
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%

🙏 帮我们做得更好

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

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