Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

if — conditional statement

The `if` statement selects a branch based on a condition. An optional `else` handles the false case.

Version: C23; repository compiler flag, production runtime not attested

Syntax

if (expression) {
    /* statements */
}

if (expression) {
    /* statements */
} else {
    /* statements */
}

if executes its first branch when the parenthesized expression compares unequal to zero. When it compares equal to zero, it executes the else branch if present; otherwise, it skips the body.

The condition must have scalar type; it need not have type bool. In C23, scalar types include arithmetic types, pointer types, and nullptr_t. The expression nullptr is therefore a valid condition and is treated as false.

A branch can be a single statement; { ... } groups statements. Use braces for clarity: else binds to the nearest preceding if permitted by the syntax. else if is another if nested in the else branch.

Examples

#include <stdio.h>

int main(void)
{
    int score = 72;

    if (score >= 50) {
        puts("pass");
    } else {
        puts("fail");
    }

    return 0;
}

Result

pass

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources