diff options
Diffstat (limited to 'devdocs/c/language%2Fbreak.html')
| -rw-r--r-- | devdocs/c/language%2Fbreak.html | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/devdocs/c/language%2Fbreak.html b/devdocs/c/language%2Fbreak.html new file mode 100644 index 00000000..bf2423f9 --- /dev/null +++ b/devdocs/c/language%2Fbreak.html @@ -0,0 +1,62 @@ + <h1 id="firstHeading" class="firstHeading">break statement</h1> <p>Causes the enclosing <a href="for" title="c/language/for">for</a>, <a href="while" title="c/language/while">while</a> or <a href="do" title="c/language/do">do-while</a> loop or <a href="switch" title="c/language/switch">switch statement</a> to terminate.</p> +<p>Used when it is otherwise awkward to terminate the loop using the condition expression and conditional statements.</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>break</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">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 <code>break</code> statement </td> +</tr> +</table> <p>Appears only within the <span class="t-spar">statement</span> of a loop body (<a href="while" title="c/language/while"><code>while</code></a>, <a href="do" title="c/language/do"><code>do-while</code></a>, <a href="for" title="c/language/for"><code>for</code></a>) or within the <span class="t-spar">statement</span> of a <a href="switch" title="c/language/switch"><code>switch</code></a>.</p> +<h3 id="Explanation"> Explanation</h3> <p>After this statement the control is transferred to the statement or declaration immediately following the enclosing loop or switch, as if by <a href="goto" title="c/language/goto"><code>goto</code></a>.</p> +<h3 id="Keywords"> Keywords</h3> <p><a href="../keyword/break" title="c/keyword/break"><code>break</code></a></p> +<h3 id="Notes"> Notes</h3> <p>A break statement cannot be used to break out of multiple nested loops. The <a href="goto" title="c/language/goto"><code>goto</code> statement</a> may be used for this purpose.</p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h> + +int main(void) +{ + int i = 2; + switch (i) + { + case 1: printf("1"); + case 2: printf("2"); // i==2, so execution starts at this case label + case 3: printf("3"); + case 4: + case 5: printf("45"); + break; // execution of subsequent cases is terminated + case 6: printf("6"); + } + printf("\n"); + + // Compare outputs from these two nested for loops. + for (int j = 0; j < 2; j++) + for (int k = 0; k < 5; k++) + printf("%d%d ", j,k); + printf("\n"); + + for (int j = 0; j < 2; j++) + { + for (int k = 0; k < 5; k++) // only this loop is exited by break + { + if (k == 2) + break; + printf("%d%d ", j,k); + } + } +}</pre></div> <p>Possible output:</p> +<div class="text source-text"><pre data-language="c">2345 +00 01 02 03 04 10 11 12 13 14 +00 01 10 11</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul><li> 6.8.6.3 The break statement (p: 111) </li></ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 6.8.6.3 The break statement (p: 153) </li></ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 6.8.6.3 The break statement (p: 138) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 3.6.6.3 The break statement </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <code>[[<a href="attributes/fallthrough" title="c/language/attributes/fallthrough">fallthrough</a>]]</code><span class="t-mark-rev t-since-c23">(C23)</span> </td> <td> indicates that the fall through from the previous case label is intentional and should not be diagnosed by a compiler that warns on fall-through<br><span class="t-mark">(attribute specifier)</span> </td> +</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/break" title="cpp/language/break">C++ documentation</a></span> for <span class=""><span><code>break</code> statement</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/break" class="_attribution-link">https://en.cppreference.com/w/c/language/break</a> + </p> +</div> |
