Learn › C Programming

Decision Making

Learn conditional execution with if-else statements, switch cases, and the ternary operator.

If-Else Statements

The if statement evaluates a condition and executes a block of code only when the condition is true. You can chain multiple conditions using else if to test alternative cases, and provide a default else block that runs when no conditions match. Curly braces are required for multi-statement blocks, though optional for single statements. Good practice is to always use braces to avoid bugs when adding statements later.

#include <stdio.h>

int main(void) {
    int temperature = 28;

    if (temperature > 35) {
        printf("It's extremely hot.\n");
    } else if (temperature > 25) {
        printf("It's warm and pleasant.\n");
    } else if (temperature > 15) {
        printf("It's cool outside.\n");
    } else {
        printf("It's cold, wear a jacket.\n");
    }

    return 0;
}

Switch Statements

The switch statement selects one of many code blocks to execute based on the value of an integer or character expression. Each case label specifies a value to match, and the break statement prevents fall-through to subsequent cases. The default case handles values that do not match any case label. Switch statements are often cleaner than long chains of if-else when comparing a single variable against multiple discrete values.

#include <stdio.h>

int main(void) {
    int day = 3;

    switch (day) {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        case 4:
            printf("Thursday\n");
            break;
        case 5:
            printf("Friday\n");
            break;
        case 6:
        case 7:
            printf("Weekend\n");
            break;
        default:
            printf("Invalid day\n");
            break;
    }

    return 0;
}

The Ternary Operator

The ternary operator is a concise way to write simple if-else expressions in a single line. It evaluates a condition and returns one of two values depending on whether the condition is true or false. The syntax is condition, question mark, value if true, colon, value if false. While it can make code more compact, overusing nested ternary operators reduces readability significantly.

#include <stdio.h>

int main(void) {
    int age = 20;
    const char *status = (age >= 18) ? "adult" : "minor";
    printf("Status: %s\n", status);

    int a = 15, b = 22;
    int max = (a > b) ? a : b;
    printf("The larger value is: %d\n", max);

    int score = 72;
    printf("Result: %s\n", (score >= 60) ? "Pass" : "Fail");

    return 0;
}

Nested Conditions

Conditions can be nested inside each other to create more complex decision trees. An if statement inside another if statement allows you to check additional criteria only when the outer condition is true. While nesting is sometimes necessary, deeply nested conditions are hard to read and maintain. Refactoring deeply nested conditions into separate functions or using early returns can significantly improve clarity.

#include <stdio.h>

int main(void) {
    int age = 25;
    int hasLicense = 1;
    int hasInsurance = 1;

    if (age >= 16) {
        if (hasLicense) {
            if (hasInsurance) {
                printf("You are eligible to drive.\n");
            } else {
                printf("You need insurance to drive.\n");
            }
        } else {
            printf("You need a driver's license.\n");
        }
    } else {
        printf("You are too young to drive.\n");
    }

    return 0;
}

← Data Types · Loops →