Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

C Control Flow if, else, switch, goto: Tutorial, Examples, FAQs & Interview Tips

if Statement

The if statement executes a block of code only if the condition is true (non-zero in C).

if (condition) {
    // executes if condition is true
}

if-else Statement

if (condition) {
    // executes if condition is true
} else {
    // executes if condition is false
}

if-else-if Ladder

Used to test multiple conditions in sequence. The first true condition executes and the rest are skipped.

if (condition1) {
    // ...
} else if (condition2) {
    // ...
} else if (condition3) {
    // ...
} else {
    // default
}

switch-case

The switch statement tests a variable against a list of values (cases). Each case must end with break to prevent fall-through. The default case runs if no case matches.

if-else-if Ladder - Grade Calculator
#include <stdio.h>

int main() {
    int marks;
    printf("Enter marks (0-100): ");
    scanf("%d", &marks);

    if (marks >= 90) {
        printf("Grade: A+ (Excellent)\n");
    } else if (marks >= 80) {
        printf("Grade: A (Very Good)\n");
    } else if (marks >= 70) {
        printf("Grade: B (Good)\n");
    } else if (marks >= 60) {
        printf("Grade: C (Average)\n");
    } else if (marks >= 50) {
        printf("Grade: D (Pass)\n");
    } else {
        printf("Grade: F (Fail)\n");
    }

    return 0;
}

/*
Enter marks (0-100): 85
Grade: A (Very Good)
*/
switch-case - Day of Week
#include <stdio.h>

int main() {
    int day;
    printf("Enter day number (1-7): ");
    scanf("%d", &day);

    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:
            printf("Saturday\n");
            break;
        case 7:
            printf("Sunday\n");
            break;
        default:
            printf("Invalid day number!\n");
    }

    // Fall-through example: weekend check
    switch (day) {
        case 6:
        case 7:
            printf("It's the weekend!\n");
            break;
        default:
            printf("It's a weekday.\n");
    }

    return 0;
}
Nested if - Largest of Three Numbers
#include <stdio.h>

int main() {
    int a, b, c;
    printf("Enter three numbers: ");
    scanf("%d %d %d", &a, &b, &c);

    if (a >= b) {
        if (a >= c) {
            printf("Largest: %d\n", a);
        } else {
            printf("Largest: %d\n", c);
        }
    } else {
        if (b >= c) {
            printf("Largest: %d\n", b);
        } else {
            printf("Largest: %d\n", c);
        }
    }

    return 0;
}

/*
Enter three numbers: 12 45 30
Largest: 45
*/
Key Takeaways
  • C supports if-else, switch, for, while, do-while, break, continue, and goto.
  • The switch statement uses strict equality - it does not support ranges or conditions.
  • Always add break at the end of each case in switch - without it, execution falls through to the next case.
  • goto is generally discouraged - it makes code harder to read and maintain.
  • Use break to exit a loop or switch; continue to skip to the next iteration.
  • Nested loops can be exited with a flag variable or goto (one of the few valid uses of goto in C).

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.