Bytebook Compiler

Control Flow

If, Else & Switch

Make decisions with if, else if, nested conditions and switch — with the classic grade, leap-year and calculator programs.

4 min read6 runnable examples4-question quiz

Programs become useful when they can decide. C runs a block of code only when a condition is true (any non-zero value) and skips it when the condition is false (zero).

if

C
#include <stdio.h>

int main(void) {
    int age;
    printf("Age: ");
    scanf("%d", &age);
    if (age >= 18) {
        printf("You can vote.\n");
    }
    printf("Thanks for checking!\n");
    return 0;
}
Output
Age: 17
Thanks for checking!
Sample input is includedTry it Yourself

The braces can be dropped when the block has exactly one statement, but keeping them avoids bugs when you later add a second line.

if...else

C
#include <stdio.h>

int main(void) {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    if (n % 2 == 0)
        printf("%d is even\n", n);
    else
        printf("%d is odd\n", n);
    return 0;
}
Output
Enter a number: -7
-7 is odd
Sample input is includedTry it Yourself

else if ladders

Check conditions top to bottom; the first true one wins and the rest are skipped.

C
#include <stdio.h>

int main(void) {
    int marks;
    printf("Marks out of 100: ");
    scanf("%d", &marks);

    if (marks < 0 || marks > 100)
        printf("Invalid marks\n");
    else if (marks >= 90)
        printf("Grade O (Outstanding)\n");
    else if (marks >= 80)
        printf("Grade A+\n");
    else if (marks >= 70)
        printf("Grade A\n");
    else if (marks >= 60)
        printf("Grade B+\n");
    else if (marks >= 40)
        printf("Grade P (Pass)\n");
    else
        printf("Grade F (Fail)\n");
    return 0;
}
Output
Marks out of 100: 83
Grade A+
Sample input is includedTry it Yourself

Nested if: the leap-year program

A year is a leap year if it is divisible by 4, except century years, which must be divisible by 400. This is one of the most-asked programs in CSE 101 exams.

C
#include <stdio.h>

int main(void) {
    int year;
    printf("Year: ");
    scanf("%d", &year);
    if (year % 4 == 0) {
        if (year % 100 == 0) {
            if (year % 400 == 0)
                printf("%d is a leap year\n", year);
            else
                printf("%d is not a leap year\n", year);
        } else {
            printf("%d is a leap year\n", year);
        }
    } else {
        printf("%d is not a leap year\n", year);
    }
    return 0;
}
Output
Year: 1900
1900 is not a leap year
Sample input is includedTry it Yourself

The same logic fits in one condition: (year % 4 == 0 && year % 100 != 0) || year % 400 == 0.

switch

When you compare one integer or character against many fixed values, switch is cleaner than a long else if ladder:

C
#include <stdio.h>

int main(void) {
    double a, b;
    char op;
    printf("Enter two numbers: ");
    scanf("%lf %lf", &a, &b);
    printf("Operator (+ - * /): ");
    scanf(" %c", &op);

    switch (op) {
        case '+': printf("%.2f\n", a + b); break;
        case '-': printf("%.2f\n", a - b); break;
        case '*': printf("%.2f\n", a * b); break;
        case '/':
            if (b != 0) printf("%.2f\n", a / b);
            else        printf("Cannot divide by zero\n");
            break;
        default:  printf("Unknown operator %c\n", op);
    }
    return 0;
}
Output
Enter two numbers: 12 4
Operator (+ - * /): /
3.00
Sample input is includedTry it Yourself

Rules to remember:

  • The switch expression must be an integer type (int, char, enum) — not float or a string.
  • Each case label must be a constant, and labels must be unique.
  • break exits the switch. Without it, execution falls through into the next case.
  • default runs when no case matches. It is optional and can appear anywhere.

Fall-through is occasionally useful — for grouping cases:

C
#include <stdio.h>

int main(void) {
    char ch;
    printf("Letter: ");
    scanf(" %c", &ch);
    switch (ch) {
        case 'a': case 'e': case 'i': case 'o': case 'u':
        case 'A': case 'E': case 'I': case 'O': case 'U':
            printf("%c is a vowel\n", ch);
            break;
        default:
            printf("%c is a consonant\n", ch);
    }
    return 0;
}
Output
Letter: e
e is a vowel
Sample input is includedTry it Yourself

Test yourself

4 questions · pick an answer to check it instantly

  1. 1

    What does this print?

    C
    int x = 0;
    if (x = 0) printf("A");
    else printf("B");
  2. 2

    What happens if you leave out break at the end of a case?

  3. 3

    Which of these cannot be used as a switch expression?

  4. 4

    Is 2000 a leap year?