1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
<h1 id="firstHeading" class="firstHeading">while loop</h1> <p>Executes a <span class="t-spar">statement</span> repeatedly, until the value of <span class="t-spar">expression</span> becomes equal to zero. The test takes place before each iteration.</p>
<h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin"> <tr class="t-sdsc"> <td class="t-sdsc-nopad"> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span> <code>while (</code> <span class="t-spar">expression</span> <code>)</code> <span class="t-spar">statement</span> </td> <td class="t-sdsc-nopad"> </td> <td class="t-sdsc-nopad"> </td>
</tr>
</table> <table class="t-par-begin"> <tr class="t-par"> <td> <span class="t-spar">expression</span> </td> <td> - </td> <td> any <a href="expressions" title="c/language/expressions">expression</a> of <a href="type#Type_groups" title="c/language/type">scalar type</a>. This expression is evaluated before each iteration, and if it compares equal to zero, the loop is exited. </td>
</tr> <tr class="t-par"> <td> <span class="t-spar">statement</span> </td> <td> - </td> <td> any <a href="statements" title="c/language/statements">statement</a>, typically a compound statement, which serves as the body of the loop </td>
</tr> <tr class="t-par"> <td> <span class="t-spar">attr-spec-seq</span> </td> <td> - </td> <td> <span class="t-mark-rev t-since-c23">(C23)</span>optional list of <a href="attributes" title="c/language/attributes">attributes</a>, applied to the loop statement </td>
</tr>
</table> <h3 id="Explanation"> Explanation</h3> <p>A <code>while</code> statement causes the <span class="t-spar">statement</span> (also called <i>the loop body</i>) to be executed repeatedly until the <span class="t-spar">expression</span> (also called <i>controlling expression</i>) compares equal to zero. The repetition occurs regardless of whether the loop body is entered normally or by a <a href="goto" title="c/language/goto">goto</a> into the middle of <span class="t-spar">statement</span>.</p>
<p>The evaluation of <span class="t-spar">expression</span> takes place before each execution of <span class="t-spar">statement</span> (unless entered by a goto). If the controlling expression needs to be evaluated after the loop body, the <a href="do" title="c/language/do">do-while loop</a> may be used.</p>
<p>If the execution of the loop needs to be terminated at some point, <a href="break" title="c/language/break"> break statement</a> can be used as a terminating statement.</p>
<p>If the execution of the loop needs to be continued at the end of the loop body, <a href="continue" title="c/language/continue"> continue statement</a> can be used as a shortcut.</p>
<p>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 <span class="t-spar">statement</span> or <span class="t-spar">expression</span>. This allows the compilers to optimize out all unobservable loops without proving that they terminate. The only exceptions are the loops where <span class="t-spar">expression</span> is a constant expression; <code>while(true)</code> is always an endless loop.</p>
<table class="t-rev-begin"> <tr class="t-rev t-since-c99">
<td> <p>As with all other selection and iteration statements, the while statement establishes <a href="scope" title="c/language/scope">block scope</a>: any identifier introduced in the <span class="t-spar">expression</span> goes out of scope after the <span class="t-spar">statement</span>.</p>
</td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td>
</tr> </table> <h3 id="Notes"> Notes</h3> <p>Boolean and pointer expressions are often used as loop controlling expressions. The boolean value <code>false</code> and the null pointer value of any pointer type compare equal to zero.</p>
<h3 id="Keywords"> Keywords</h3> <p><a href="../keyword/while" title="c/keyword/while"><code>while</code></a></p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#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++] = rand() % 2;
puts("Array filled!");
n = 0;
while(n < SIZE) printf("%d ", array[n++]);
printf("\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
puts(dst);
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">Array filled!
1 0 1 1 1 1 0 0
Hello, world</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 6.8.5.1 The while statement (p: 109) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 6.8.5.1 The while statement (p: 151) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 6.8.5.1 The while statement (p: 136) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 3.6.5.1 The while statement </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/while" title="cpp/language/while">C++ documentation</a></span> for <span class=""><span><code>while</code> loop</span></span> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/language/while" class="_attribution-link">https://en.cppreference.com/w/c/language/while</a>
</p>
</div>
|