summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Ffor.html
blob: 010d826f384611bd932a7b2079fa2cb2f7c4d3c0 (plain)
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
60
61
62
63
    <h1 id="firstHeading" class="firstHeading">for loop</h1>            <p>Executes a loop.</p>
<p>Used as a shorter equivalent of <a href="while" title="c/language/while">while loop</a>.</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-rev t-since-c23">(since C23)</span><span class="t-mark">(optional)</span> <code>for</code> <code>(</code> <span class="t-spar">init-clause</span> <code>;</code> <span class="t-spar">cond-expression</span> <code>;</code> <span class="t-spar">iteration-expression</span> <code>)</code> <span class="t-spar">loop-statement</span> </td> <td class="t-sdsc-nopad"> </td> <td class="t-sdsc-nopad"> </td>
</tr> 
</table> <h3 id="Explanation"> Explanation</h3> <p>Behaves as follows:</p>
<ul>
<li> <span class="t-spar">init-clause</span> may be an expression <span class="t-rev-inl t-since-c99"><span>or a declaration</span><span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span>. </li>
<ul><li> An <span class="t-spar">init-clause</span>, which is an expression, is evaluated once, before the first evaluation of <span class="t-spar">cond-expression</span> and its result is discarded. </li></ul>
</ul>  <table class="t-rev-begin"> <tr class="t-rev t-since-c99">
<td> <ul><li> An <span class="t-spar">init-clause</span>, which is a declaration, is in scope in the entire loop body, including the remainder of <span class="t-spar">init-clause</span>, the entire <span class="t-spar">cond-expression</span>, the entire <span class="t-spar">iteration-expression</span> and the entire <span class="t-spar">loop-statement</span>. Only <code>auto</code> and <code>register</code> <a href="storage_duration" title="c/language/storage duration">storage class specifiers</a> are allowed for the variables declared in this declaration. </li></ul> </td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td>
</tr> </table> <ul>
<li> <span class="t-spar">cond-expression</span> is evaluated before the loop body. If the result of the expression is zero, the loop statement is exited immediately. </li>
<li> <span class="t-spar">iteration-expression</span> is evaluated after the loop body and its result is discarded. After evaluating <span class="t-spar">iteration-expression</span>, control is transferred to <span class="t-spar">cond-expression</span>. </li>
</ul> <p><span class="t-spar">init-clause</span>, <span class="t-spar">cond-expression</span>, and <span class="t-spar">iteration-expression</span> are all optional. If <span class="t-spar">cond-expression</span> is omitted, it is replaced with a non-zero integer constant, which makes the loop endless:</p>
<div class="c source-c"><pre data-language="c">for(;;) {
   printf("endless loop!");
}</pre></div> <p><span class="t-spar">loop-statement</span> is not optional, but it may be a null statement:</p>
<div class="c source-c"><pre data-language="c">for(int n = 0; n &lt; 10; ++n, printf("%d\n", n))
    ; // null statement</pre></div> <p>If the execution of the loop needs to be terminated at some point, a <a href="break" title="c/language/break"> break statement</a> can be used anywhere within the <span class="t-spar">loop-statement</span>.</p>
<p>The <a href="continue" title="c/language/continue"> continue statement</a> used anywhere within the <span class="t-spar">loop-statement</span> transfers control to <span class="t-spar">iteration-expression</span>.</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">cond-expression</span>, <span class="t-spar">iteration-expression</span> or <span class="t-spar">loop-statement</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">cond-expression</span> is omitted or is a constant expression; <code>for(;;)</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 for statement establishes <a href="scope" title="c/language/scope">block scope</a>: any identifier introduced in the <span class="t-spar">init-clause</span>, <span class="t-spar">cond-expression</span>, or <span class="t-spar">iteration-expression</span> goes out of scope after the <span class="t-spar">loop-statement</span>.</p>
</td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td>
</tr> </table> <table class="t-rev-begin"> <tr class="t-rev t-since-c23">
<td> <p><span class="t-spar">attr-spec-seq</span> is an optional list of <a href="attributes" title="c/language/attributes">attributes</a>, applied to the <code>for</code> statement.</p>
</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td>
</tr> </table> <h3 id="Keywords"> Keywords</h3> <p><a href="../keyword/for" title="c/keyword/for"><code>for</code></a></p>
<h3 id="Notes"> Notes</h3> <p>The expression statement used as <span class="t-spar">loop-statement</span> establishes its own block scope, distinct from the scope of <span class="t-spar">init-clause</span>, unlike in C++:</p>
<div class="c source-c"><pre data-language="c">for (int i = 0; ; ) {
    long i = 1;   // valid C, invalid C++
    // ...
}</pre></div> <p>It is possible to enter the body of a loop using <a href="goto" title="c/language/goto">goto</a>. When entering a loop in this manner, <span class="t-spar">init-clause</span> and <span class="t-spar">cond-expression</span> are not executed. (If control then reaches the end of the loop body, repetition may occur including execution of <span class="t-spar">cond-expression</span>.)</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
enum { SIZE = 8 };
int main(void)
{
    int array[SIZE];
    for(size_t i = 0 ; i &lt; SIZE; ++i)
        array [i] = rand() % 2;
    printf("Array filled!\n");
    for (size_t i = 0; i &lt; SIZE; ++i)
        printf("%d ", array[i]);
    putchar('\n');
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Array filled!
1 0 1 1 1 1 0 0</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 6.8.5.3 The for statement (p: 110) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 6.8.5.3 The for statement (p: 151) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 6.8.5.3 The for statement (p: 136) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 3.6.5.3 The for 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/for" title="cpp/language/for">C++ documentation</a></span> for <span class=""><span><code>for</code> loop</span></span> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/language/for" class="_attribution-link">https://en.cppreference.com/w/c/language/for</a>
  </p>
</div>