Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 1 of 8

if, else if, else - Conditional Execution

The `if` statement is C's primary mechanism for conditional execution. Unlike Python, the condition is always in parentheses. In C, **any non-zero value is true** and **zero is false** - there is no separate boolean type in C89 (C99 adds `<stdbool.h>`). Understanding this 0/non-zero duality is critical because many C idioms use it directly.

11 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

if/else - Syntax, Truthiness & the Dangling-else Problem

if/else - Syntax, Truthiness & the Dangling-else Problem
#include <stdio.h>
#include <stdbool.h>   /* C99: provides bool, true, false */

int main(void) {
    int x = 5;

    /* Basic if - any non-zero value is truthy */
    if (x)              printf("x is non-zero
");
    if (x != 0)         printf("same thing, explicit
");

    /* if/else */
    if (x > 10) {
        printf("x > 10
");
    } else {
        printf("x <= 10
");
    }

    /* if / else if / else chain */
    int score = 82;
    if      (score >= 90) printf("A
");
    else if (score >= 80) printf("B
");   /* ← matches */
    else if (score >= 70) printf("C
");
    else                  printf("F
");

    /* ── DANGLING-ELSE TRAP ──────────────────────────────
       Which 'if' does this 'else' belong to?              */
    int a = 1, b = 2;
    if (a > 0)
        if (b > 10)
            printf("both
");
    else               /* BELONGS TO INNER if (b > 10) - not outer! */
        printf("a>0 but b<=10? NO - compiler binds to nearest if
");

    /* Fix: always use braces to make intent explicit */
    if (a > 0) {
        if (b > 10) {
            printf("both
");
        }
    } else {
        printf("a <= 0
");  /* now clearly belongs to outer if */
    }

    /* ── Pointer validity check - real pattern in C ───── */
    char *ptr = "hello";
    if (ptr != NULL && ptr[0] != '') {  /* short-circuit: safe */
        printf("ptr points to: %s
", ptr);
    }

    /* ── bool (C99) */
    bool flag = true;
    if (flag) printf("flag is set
");

    return 0;
}

Common Mistakes

  • The dangling-else - `if (a) if (b) X; else Y;` - the `else` belongs to the INNER `if (b)`. Always use braces `{}` around if/else bodies to avoid this ambiguity completely.
  • Using `=` instead of `==` - `if (x = 5)` assigns 5 to x and evaluates to 5 (truthy). Always succeeds. Enable `-Wall` to get a warning. Some programmers write `if (5 == x)` (Yoda condition) to make this a compile error.
  • Comparing pointers with `== NULL` vs truthy check - `if (ptr)` is idiomatic for non-NULL check. But for clarity in production code, `if (ptr != NULL)` is preferred because it makes the intent explicit.

Tip

Tip

Practice if else if else Conditional Execution in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Conditions evaluate non-zero as true and zero as false to branch execution flow

Practice Task

Note

Practice Task - (1) Write a working example of if else if else Conditional Execution from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with if else if else Conditional Execution is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • The `if` statement is C's primary mechanism for conditional execution.
  • The dangling-else - `if (a) if (b) X; else Y;` - the `else` belongs to the INNER `if (b)`. Always use braces `{}` around if/else bodies to avoid this ambiguity completely.
  • Using `=` instead of `==` - `if (x = 5)` assigns 5 to x and evaluates to 5 (truthy). Always succeeds. Enable `-Wall` to get a warning. Some programmers write `if (5 == x)` (Yoda condition) to make this a compile error.
  • Comparing pointers with `== NULL` vs truthy check - `if (ptr)` is idiomatic for non-NULL check. But for clarity in production code, `if (ptr != NULL)` is preferred because it makes the intent explicit.

Finished this lesson?

0 of 8 in Module 2
Sign in to save progress

Topics in This Module

0/8 done