summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Fif.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/c/language%2Fif.html')
-rw-r--r--devdocs/c/language%2Fif.html65
1 files changed, 65 insertions, 0 deletions
diff --git a/devdocs/c/language%2Fif.html b/devdocs/c/language%2Fif.html
new file mode 100644
index 00000000..b273c4e8
--- /dev/null
+++ b/devdocs/c/language%2Fif.html
@@ -0,0 +1,65 @@
+ <h1 id="firstHeading" class="firstHeading">if statement</h1> <p>Conditionally executes code.</p>
+<p>Used where code needs to be executed only if some condition is true.</p>
+<h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span> <code>if (</code> <span class="t-spar">expression</span> <code>)</code> <span class="t-spar">statement-true</span> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td>
+</tr> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span> <code>if (</code> <span class="t-spar">expression</span> <code>)</code> <span class="t-spar">statement-true</span> <code>else</code> <span class="t-spar">statement-false</span> </td> <td> (2) </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>if</code> statement </td>
+</tr> <tr class="t-par"> <td> <span class="t-spar">expression</span> </td> <td> - </td> <td> an <a href="expressions" title="c/language/expressions">expression</a> of any scalar type </td>
+</tr> <tr class="t-par"> <td> <span class="t-spar">statement-true</span> </td> <td> - </td> <td> any <a href="statements" title="c/language/statements">statement</a> (often a compound statement), which is executed if <span class="t-spar">expression</span> compares not equal to <code>​0​</code> </td>
+</tr> <tr class="t-par"> <td> <span class="t-spar">statement-false</span> </td> <td> - </td> <td> any <a href="statements" title="c/language/statements">statement</a> (often a compound statement), which is executed if <span class="t-spar">expression</span> compares equal to <code>​0​</code> </td>
+</tr>
+</table> <h3 id="Explanation"> Explanation</h3> <p><span class="t-spar">expression</span> must be an expression of any <a href="type#Type_groups" title="c/language/type">scalar type</a>.</p>
+<p>If <span class="t-spar">expression</span> compares not equal to the integer zero, <span class="t-spar">statement-true</span> is executed.</p>
+<p>In the form <span class="t-v">(2)</span>, if <span class="t-spar">expression</span> compares equal to the integer zero, <span class="t-spar">statement_false</span> is executed.</p>
+<table class="t-rev-begin"> <tr class="t-rev t-since-c99">
+<td> <p>As with all other selection and iteration statements, the entire if-statement has its own block scope:</p>
+<div class="c source-c"><pre data-language="c">enum {a, b};
+int different(void)
+{
+ if (sizeof(enum {b, a}) != sizeof(int))
+ return a; // a == 1
+ return b; // b == 0 in C89, b == 1 in C99
+}</pre></div> </td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td>
+</tr> </table> <h3 id="Notes"> Notes</h3> <p>The <code>else</code> is always associated with the closest preceding <code>if</code> (in other words, if <span class="t-spar">statement-true</span> is also an if statement, then that inner if statement must contain an <code>else</code> part as well):</p>
+<div class="c source-c"><pre data-language="c">int j = 1;
+if (i &gt; 1)
+ if(j &gt; 2)
+ printf("%d &gt; 1 and %d &gt; 2\n", i, j);
+ else // this else is part of if(j&gt;2), not part of if(i&gt;1)
+ printf("%d &gt; 1 and %d &lt;= 2\n", i, j);</pre></div> <p>If <span class="t-spar">statement-true</span> is entered through a <a href="goto" title="c/language/goto">goto</a>, <span class="t-spar">statement-false</span> is not executed.</p>
+<h3 id="Keywords"> Keywords</h3> <p><a href="../keyword/if" title="c/keyword/if"><code>if</code></a>, <a href="../keyword/else" title="c/keyword/else"><code>else</code></a></p>
+<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
+
+int main(void)
+{
+ int i = 2;
+ if (i &gt; 2) {
+ printf("first is true\n");
+ } else {
+ printf("first is false\n");
+ }
+
+ i = 3;
+ if (i == 3) printf("i == 3\n");
+
+ if (i != 3) printf("i != 3 is true\n");
+ else printf("i != 3 is false\n");
+}</pre></div> <p>Output:</p>
+<div class="text source-text"><pre data-language="c">first is false
+i == 3
+i != 3 is false</pre></div> </div> <h3 id="References"> References</h3> <ul>
+<li> C17 standard (ISO/IEC 9899:2018): </li>
+<ul><li> 6.8.4.1 The if statement (p: 108-109) </li></ul>
+<li> C11 standard (ISO/IEC 9899:2011): </li>
+<ul><li> 6.8.4.1 The if statement (p: 148-149) </li></ul>
+<li> C99 standard (ISO/IEC 9899:1999): </li>
+<ul><li> 6.8.4.1 The if statement (p: 133-134) </li></ul>
+<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
+<ul><li> 3.6.4.1 The if 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/if" title="cpp/language/if">C++ documentation</a></span> for <span class=""><span><code>if</code> statement</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/if" class="_attribution-link">https://en.cppreference.com/w/c/language/if</a>
+ </p>
+</div>