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
64
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 > 1)
if(j > 2)
printf("%d > 1 and %d > 2\n", i, j);
else // this else is part of if(j>2), not part of if(i>1)
printf("%d > 1 and %d <= 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 <stdio.h>
int main(void)
{
int i = 2;
if (i > 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">
© 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>
|