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.
| Category | Control flow |
|---|---|
| Kind | Keyword / Statement |
| C standard | C89 / 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 control-flow keywords
📚 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.