Curriculum / C / Lesson 06

Conditions (if / else) in C

Conditional statements let your program make decisions — running different code depending on whether a condition is true or false.C offers if, else if, else, and the switch statement for choosing between many fixed options..

6 of 11
beginner 📖 5 min read 🎯 1 question

Conditions

Conditional statements let your program make decisions — running different code depending on whether a condition is true or false.

C offers if, else if, else, and the switch statement for choosing between many fixed options.

💡 Key insight In C, any non-zero value is treated as "true" and 0 is treated as "false" inside a condition.

Syntax

syntax
if (condition) {
    // code
} else if (condition2) {
    // code
} else {
    // code
}

Example

Conditions are checked top to bottom; the first one that's true runs, and the rest are skipped.

c
#include <stdio.h>

int main() {
    int marks = 72;
    if (marks >= 90) {
        printf("Grade: A\n");
    } else if (marks >= 60) {
        printf("Grade: B\n");
    } else {
        printf("Grade: C\n");
    }
    return 0;
}
outputGrade: B

Try it yourself

Copy this example into the C compiler and run it — try changing a value and see what happens.

Quick Quiz

1. What happens if you forget 'break' inside a switch case in C?

Back to curriculum