C Reference ยท Keywords and Statements
The if and else statements
Reference for the C if and else statements: syntax, how conditions are evaluated, the dangling else, else-if chains, and the C23 declaration form.
if runs a statement only when a condition is true. An optional else supplies the statement to run when it is false. The condition is any expression of scalar type (an integer, a floating-point number or a pointer): zero, 0.0 and a null pointer count as false, everything else counts as true.
The controlled statement is usually a block in braces, but it can be any single statement. Chaining else if gives you a sequence of tests that stops at the first one that succeeds.
if (condition) { /* runs when condition != 0 */ } else if (other) { /* runs when condition == 0 and other != 0 */ } else { /* runs when both are 0 */ }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
if ( expression ) statement if ( expression ) statement else statement
| Part | Description |
|---|---|
expression |
Controlling expression of scalar type. It is compared against zero. |
statement |
Any statement. A compound statement { ... } is the norm. |
Returns: nothing. if is a statement, not an expression; use the conditional operator when you need a value.
Undefined behavior: none caused by if itself, but the condition can contain undefined expressions such as signed overflow or reading an uninitialized variable.
Usage notes
- Always use braces. Without them, adding a second line under the
ifsilently runs it unconditionally. Many style guides ban brace-less bodies for that reason. - Dangling else. An
elsepairs with the closest unmatchedif. Indentation does not change that. Braces do. =instead of==.if (x = 5)assigns and is always true. Compilers warn with-Wall; wrap an intentional assignment in extra parentheses:if ((c = getchar()) != EOF).- Floating-point equality.
if (x == 0.1)is rarely what you want; see comparing floating-point numbers. - Pointers and integers in conditions work directly:
if (p)meansp != NULL, andif (n)meansn != 0. Both are idiomatic. - Short-circuit operators in the condition stop evaluating early, so
if (p && p->ok)is safe. See short-circuit evaluation. - Scope. Variables declared inside the braces vanish at the closing brace. C has no
if (init; condition)form like C++17; declare the variable before theif, optionally in a surrounding block. - Many
else ifbranches on one integer are often clearer as aswitch. - Lookup tables can replace long chains when the branches differ only by data.