Control Statements in C

By default, a C program executes statements from top to bottom, one by one. But real-world problems often require making decisions — doing one thing if a condition is true and something else if it is false. Control statements allow a program to change its flow based on conditions.

The two primary decision-making tools in C are:

  1. The if / else family of statements
  2. The switch statement

The if Statement

The if statement checks a condition. If the condition is true (non-zero), the block of code inside it is executed. If the condition is false (zero), the block is skipped.

Syntax


if (condition)
{
    // code to execute if condition is true
}

Example — Check if a Number is Positive


#include <stdio.h>

int main()
{
    int num = 10;

    if (num > 0)
    {
        printf("The number is positive.\n");
    }

    printf("Program ended.\n");

    return 0;
}

Output


The number is positive.
Program ended.

The if-else Statement

The if-else statement executes one block if the condition is true and a different block if the condition is false.

Syntax


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

Example — Check Even or Odd


#include <stdio.h>

int main()
{
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    if (num % 2 == 0)
    {
        printf("%d is Even.\n", num);
    }
    else
    {
        printf("%d is Odd.\n", num);
    }

    return 0;
}

Sample Output


Enter a number: 7
7 is Odd.

The if-else if-else (Ladder) Statement

When there are more than two possible outcomes, the if-else if-else ladder is used. It checks conditions one by one from top to bottom. The first condition that is true gets executed, and the rest are skipped. The final else acts as a default case.

Syntax


if (condition1)
{
    // executed if condition1 is true
}
else if (condition2)
{
    // executed if condition2 is true
}
else if (condition3)
{
    // executed if condition3 is true
}
else
{
    // executed if none of the above are true
}

Example — Grade Classification


#include <stdio.h>

int main()
{
    int marks;

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

    if (marks >= 90)
    {
        printf("Grade: A+\n");
    }
    else if (marks >= 75)
    {
        printf("Grade: A\n");
    }
    else if (marks >= 60)
    {
        printf("Grade: B\n");
    }
    else if (marks >= 50)
    {
        printf("Grade: C\n");
    }
    else
    {
        printf("Grade: Fail\n");
    }

    return 0;
}

Sample Output


Enter marks (0-100): 78
Grade: A

Nested if Statements

A nested if means placing one if statement inside another. This is used when a decision depends on multiple layers of conditions.


#include <stdio.h>

int main()
{
    int age;
    int hasVoterID;

    printf("Enter age: ");
    scanf("%d", &age);

    printf("Do you have a voter ID? (1 = Yes, 0 = No): ");
    scanf("%d", &hasVoterID);

    if (age >= 18)
    {
        if (hasVoterID == 1)
        {
            printf("You are eligible to vote.\n");
        }
        else
        {
            printf("Age is valid but Voter ID is missing.\n");
        }
    }
    else
    {
        printf("You are not old enough to vote.\n");
    }

    return 0;
}

Sample Output


Enter age: 20
Do you have a voter ID? (1 = Yes, 0 = No): 1
You are eligible to vote.

Short-Form if (Without Braces)

If the if or else block contains only one statement, the curly braces can be omitted. However, using braces is always recommended for clarity.


int x = 10;
if (x > 5)
    printf("x is greater than 5\n");  // single statement, no braces needed

The switch Statement

The switch statement is used when a variable needs to be compared against multiple specific values. It is cleaner and more readable than a long chain of if-else if statements for fixed value comparisons.

Syntax


switch (expression)
{
    case value1:
        // code for value1
        break;

    case value2:
        // code for value2
        break;

    case value3:
        // code for value3
        break;

    default:
        // code if no case matches
}

How switch Works

  1. The expression is evaluated once.
  2. Its value is compared with each case label.
  3. When a match is found, the code inside that case is executed.
  4. The break statement exits the switch block.
  5. If no case matches, the default block executes.

Example — Day of the 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");
    }

    return 0;
}

Sample Output


Enter day number (1-7): 5
Friday

Example — Simple Calculator Using switch


#include <stdio.h>

int main()
{
    float a, b;
    char op;

    printf("Enter: number operator number (e.g. 5 + 3): ");
    scanf("%f %c %f", &a, &op, &b);

    switch (op)
    {
        case '+':
            printf("Result: %.2f\n", a + b);
            break;
        case '-':
            printf("Result: %.2f\n", a - b);
            break;
        case '*':
            printf("Result: %.2f\n", a * b);
            break;
        case '/':
            if (b != 0)
                printf("Result: %.2f\n", a / b);
            else
                printf("Error: Division by zero!\n");
            break;
        default:
            printf("Unknown operator!\n");
    }

    return 0;
}

Sample Output


Enter: number operator number (e.g. 5 + 3): 10 / 2
Result: 5.00

Fall-Through in switch (Without break)

If a break statement is missing after a case, the program continues executing the next case's code regardless of whether it matches. This is called fall-through.


#include <stdio.h>

int main()
{
    int num = 2;

    switch (num)
    {
        case 1:
            printf("One\n");
        case 2:
            printf("Two\n");  // this runs
        case 3:
            printf("Three\n"); // this also runs (fall-through!)
            break;
        case 4:
            printf("Four\n");
    }

    return 0;
}

Output (Unexpected without break)


Two
Three

Tip: Always include break at the end of each case to avoid unintended fall-through, unless fall-through is the intended behavior.

Multiple Cases Sharing the Same Code

Multiple case labels can share the same block of code by stacking them together:


#include <stdio.h>

int main()
{
    int month;
    printf("Enter month number: ");
    scanf("%d", &month);

    switch (month)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            printf("This month has 31 days.\n");
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            printf("This month has 30 days.\n");
            break;
        case 2:
            printf("February has 28 or 29 days.\n");
            break;
        default:
            printf("Invalid month!\n");
    }

    return 0;
}

if-else vs switch — When to Use Which

ScenarioRecommended
Checking ranges (e.g., marks >= 90)if-else
Checking exact values (e.g., day == 1)switch
Checking float conditionsif-else (switch only works with int/char)
Multiple fixed options (e.g., menu choices)switch
Complex conditions with logical operatorsif-else

Summary

Control statements give a C program the ability to make decisions and change its execution path. The if statement handles simple yes/no decisions. The if-else handles two outcomes. The if-else if-else ladder handles multiple conditions in a chain. The switch statement elegantly handles multiple specific value comparisons with clean, readable syntax. Together, these tools form the decision-making foundation of every C program.

Leave a Comment

Your email address will not be published. Required fields are marked *