> ## 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.

# C If Statement

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

```c theme={"dark"}
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:

```c theme={"dark"}
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.

```c theme={"dark"}
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.

```c theme={"dark"}
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.

```c theme={"dark"}
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;
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
