Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The if statement is a fundamental control flow construct in C that conditionally executes a statement or a compound statement (block) based on the runtime evaluation of a scalar expression.

Syntax and Branching

int expression = 1;

if (expression) {
    // statement(s)
}
To create mutually exclusive execution branches, the if statement can be followed by an else clause. While commonly formatted as else if for readability, C grammar does not contain a dedicated else if construct. It is strictly an else clause where the subsequent statement happens to be another independent if statement:
int expression_1 = 0;
int expression_2 = 1;

if (expression_1) {
    // statement(s)
} else if (expression_2) {
    // statement(s)
} else {
    // statement(s)
}

Evaluation Mechanics

  1. Expression Evaluation: At runtime, the executing program evaluates the scalar expression enclosed in parentheses.
  2. Truth Condition: C does not require a strict boolean data type for control flow. Instead, it evaluates the expression against zero:
    • If the expression evaluates to a non-zero value, the condition is considered true, and the subsequent statement is executed.
    • If the expression evaluates to zero (0, 0.0, or NULL), the condition is considered false, and the execution pointer bypasses the statement.

Type Constraints

The expression within the parentheses must evaluate to a scalar type. This includes:
  • Arithmetic types: Integers (int, char, short, long) and floating-point numbers (float, double).
  • Pointer types: Memory addresses (where a NULL pointer evaluates to 0).
Aggregate types, such as struct or union, cannot be evaluated directly within an if condition unless a specific scalar member is accessed.

Scope and Compound Statements

The if statement strictly binds to the single statement immediately following it.
int x = 1;
int y = 0;
int z = 0;

if (x > 0)
    y = 1; // Bound to the if statement
z = 2;     // Unconditionally executed; NOT bound to the if statement
To bind multiple statements to a single if condition, they must be enclosed in braces {} to form a compound statement. A compound statement introduces a new lexical block scope. Variables declared within this block have automatic storage duration; their lifetime ends and they go out of scope once the block terminates.
int x = 1;
int z = 0;

if (x > 0) {
    int y = 1; // y has automatic storage duration scoped to this block
    z = 2;
}
// The lifetime of y has ended here; accessing y results in a compilation error

The Dangling Else Rule

In nested if statements without explicit braces, C resolves ambiguity by binding an else clause to the closest preceding, unbound if statement within the same block.
int condition_a = 1;
int condition_b = 0;
int result = 0;

if (condition_a)
    if (condition_b)
        result = 1; // Bound to 'if (condition_b)'
    else            // Binds to 'if (condition_b)', not 'if (condition_a)'
        result = 2;
Master C with Deep Grasping Methodology!Learn More