Module 4 of 13 · C Programming Fundamentals · Beginner

Control Flow

Duration: 45 min

Control flow statements allow you to make decisions in your program. They determine which code blocks execute based on conditions. The main control flow structures are if/else, switch, and the ternary operator.

if Statement

#include <stdio.h>

int main() {
    int age = 18;
    
    if (age >= 18) {
        printf("You are an adult\n");
    }
    
    return 0;
}

if-else Statement

#include <stdio.h>

int main() {
    int score = 45;
    
    if (score >= 50) {
        printf("Pass\n");
    } else {
        printf("Fail\n");
    }
    
    return 0;
}

if-else if-else Chain

#include <stdio.h>

int main() {
    int grade = 75;
    
    if (grade >= 90) {
        printf("A\n");
    } else if (grade >= 80) {
        printf("B\n");
    } else if (grade >= 70) {
        printf("C\n");
    } else if (grade >= 60) {
        printf("D\n");
    } else {
        printf("F\n");
    }
    
    return 0;
}

Nested if Statements

#include <stdio.h>

int main() {
    int age = 25;
    int has_license = 1;
    
    if (age >= 18) {
        if (has_license) {
            printf("Can drive\n");
        } else {
            printf("Too young to drive\n");
        }
    } else {
        printf("Must be 18 to drive\n");
    }
    
    return 0;
}

switch Statement

#include <stdio.h>

int main() {
    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;
        default:
            printf("Weekend\n");
    }
    
    return 0;
}

Ternary Operator

#include <stdio.h>

int main() {
    int age = 20;
    
    // Syntax: condition ? value_if_true : value_if_false
    char *status = (age >= 18) ? "Adult" : "Minor";
    printf("Status: %s\n", status);
    
    // Nested ternary
    int score = 75;
    char grade = (score >= 90) ? 'A' : 
                 (score >= 80) ? 'B' : 
                 (score >= 70) ? 'C' : 'F';
    printf("Grade: %c\n", grade);
    
    return 0;
}

Practical Example: Calculator

#include <stdio.h>

int main() {
    int a = 10, b = 5;
    char op = '+';
    
    switch (op) {
        case '+':
            printf("%d + %d = %d\n", a, b, a + b);
            break;
        case '-':
            printf("%d - %d = %d\n", a, b, a - b);
            break;
        case '*':
            printf("%d * %d = %d\n", a, b, a * b);
            break;
        case '/':
            if (b != 0) {
                printf("%d / %d = %d\n", a, b, a / b);
            } else {
                printf("Cannot divide by zero\n");
            }
            break;
        default:
            printf("Invalid operator\n");
    }
    
    return 0;
}

Quiz 1: if-else

❓ What will print if x = 5?

Quiz 2: switch Statement

❓ What is the purpose of `break;` in a switch statement?

Quiz 3: Ternary Operator

❓ What does this ternary operator return? (x > 5) ? 10 : 20 when x = 3

Quiz 4: Nested if

❓ What will print if age = 20 and has_license = 1?

Quiz 5: default Case

❓ When does the `default` case execute in a switch?

← Previous Continue interactively → Next →

Related Courses