diff options
Diffstat (limited to 'devdocs/c/language%2Fdo.html')
| -rw-r--r-- | devdocs/c/language%2Fdo.html | 61 |
1 files changed, 0 insertions, 61 deletions
diff --git a/devdocs/c/language%2Fdo.html b/devdocs/c/language%2Fdo.html deleted file mode 100644 index 226bcf7c..00000000 --- a/devdocs/c/language%2Fdo.html +++ /dev/null @@ -1,61 +0,0 @@ - <h1 id="firstHeading" class="firstHeading">do-while loop</h1> <p>Executes a <span class="t-spar">statement</span> repeatedly until the value of the condition <span class="t-spar">expression</span> becomes false. The test takes place after 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>do</code> <span class="t-spar">statement</span> <code>while (</code> <span class="t-spar">expression</span> <code>)</code> <code>;</code> </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 after 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 is 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>do-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 0. 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 after each execution of <span class="t-spar">statement</span> (whether entered normally or by a goto). If the controlling expression needs to be evaluated before the loop body, the <a href="while" title="c/language/while">while loop</a> or the <a href="for" title="c/language/for">for 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 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>do {...} 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 do-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 statement.</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/do" title="c/keyword/do"><code>do</code></a>, <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> -enum { SIZE = 8 }; -int main(void) -{ - // trivial example - int array[SIZE], n = 0; - do array[n++] = rand() % 2; // the loop body is a single expression statement - while(n < SIZE); - puts("Array filled!"); - n = 0; - do { // the loop body is a compound statement - printf("%d ", array[n]); - ++n; - } while (n < SIZE); - printf("\n"); - - // the loop from K&R itoa(). The do-while loop is used - // because there is always at least one digit to generate - int num = 1234, i=0; - char s[10]; - do s[i++] = num % 10 + '0'; // get next digit in reverse order - while ((num /= 10) > 0); - s[i] = '\0'; - puts(s); -}</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 -4321</pre></div> </div> <h3 id="References"> References</h3> <ul> -<li> C17 standard (ISO/IEC 9899:2018): </li> -<ul><li> 6.8.5.2 The do statement (p: 109) </li></ul> -<li> C11 standard (ISO/IEC 9899:2011): </li> -<ul><li> 6.8.5.2 The do statement (p: 151) </li></ul> -<li> C99 standard (ISO/IEC 9899:1999): </li> -<ul><li> 6.8.5.2 The do statement (p: 136) </li></ul> -<li> C89/C90 standard (ISO/IEC 9899:1990): </li> -<ul><li> 3.6.5.2 The do 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/do" title="cpp/language/do">C++ documentation</a></span> for <span class=""><span><code>do</code>-<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/do" class="_attribution-link">https://en.cppreference.com/w/c/language/do</a> - </p> -</div> |
