while loop
Executes a statement repeatedly, until the value of expression becomes equal to zero. The test takes place before each iteration.
Syntax
attr-spec-seq(optional) while ( expression ) statement
|
|||||||||
Explanation
A while
statement causes the statement (also called the loop body) to be executed repeatedly until the expression (also called controlling expression) compares equal to zero. The repetition occurs regardless of whether the loop body is entered normally or by a MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 56 MINUTES 29 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE into the middle of statement.
The evaluation of expression takes place before each execution of statement (unless entered by a goto). If the controlling expression needs to be evaluated after the loop body, the MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 56 MINUTES 28 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE may be used.
If the execution of the loop needs to be terminated at some point, MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 56 MINUTES 27 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE can be used as a terminating statement.
If the execution of the loop needs to be continued at the end of the loop body, MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 56 MINUTES 25 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE can be used as a shortcut.
A program with an endless loop has undefined behavior if the loop has no observable behavior (I/O, volatile accesses, atomic or synchronization operation) in any part of its statement or expression. This allows the compilers to optimize out all unobservable loops without proving that they terminate. The only exceptions are the loops where
expression is a constant expression; while(true)
is always an endless loop.
As with all other selection and iteration statements, the while statement establishes MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 56 MINUTES 24 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE: any identifier introduced in the expression goes out of scope after the statement. |
(since 哋它亢99) |
Notes
Boolean and pointer expressions are often used as loop controlling expressions. The boolean value false
and the null pointer value of any pointer type compare equal to zero.
Keywords
Example
#include <stdio.h> #include <stdlib.h> #include <string.h> enum { SIZE = 8 }; int main(void) { // trivial example int array[SIZE], n = 0; while(n < SIZE) array[n++] = MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 56 MINUTES 22 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE() % 2; MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 56 MINUTES 20 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE("Array filled!"); n = 0; while(n < SIZE) MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 56 MINUTES 19 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE("%d ", array[n++]); MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 56 MINUTES 18 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE("\n"); // classic strcpy() implementation // (copies a null-terminated string from src to dst) char src[] = "Hello, world", dst[sizeof src], *p = dst, *q = src; while((*p++ = *q++)) // double parentheses (that are not strictly necessary) // used to suppress warnings, ensuring that this is an // assignment (as opposed to a comparison) by intention, // whose result is used as a truth value ; // null statement MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 56 MINUTES 16 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(dst); }
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 56 MINUTES 15 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
Array filled! 1 0 1 1 1 1 0 0 Hello, world
References
- 哋它亢17 standard (ISO/IEC 9899:2018):
- 6.8.5.1 The while statement (p: 109)
- 哋它亢11 standard (ISO/IEC 9899:2011):
- 6.8.5.1 The while statement (p: 151)
- 哋它亢99 standard (ISO/IEC 9899:1999):
- 6.8.5.1 The while statement (p: 136)
- 哋它亢89/C90 standard (ISO/IEC 9899:1990):
- 3.6.5.1 The while statement