Control Statements in C (if and switch)

Control statements determine the flow of execution in a C program. They allow the program to make decisions based on conditions.

The two most commonly used decision-making statements are:

  • if statement
  • switch statement

1. if Statement

The if statement executes a block of code only if the specified condition is true.

Syntax

if (condition)
{
    // statements
}

Example

#include <stdio.h>

int main()
{
    int age = 20;

    if (age >= 18)
    {
        printf("Eligible to vote");
    }

    return 0;
}

Output

Eligible to vote

2. if...else Statement

The if...else statement executes one block of code if the condition is true and another block if the condition is false.

Syntax

if (condition)
{
    // True block
}
else
{
    // False block
}

Example

 #include <stdio.h>

int main()
{
    int number = 15;

    if (number % 2 == 0)
    {
        printf("Even Number");
    }
    else
    {
        printf("Odd Number");
    }

    return 0;
}

Output

Odd Number

3. if...else if...else Ladder

This statement is used to test multiple conditions.

Syntax

if (condition1)
{
    // Statements
}
else if (condition2)
{
    // Statements
}
else
{
    // Statements
}

Example

 #include <stdio.h>

int main()
{
    int marks = 82;

    if (marks >= 90)
        printf("Grade A+");
    else if (marks >= 80)
        printf("Grade A");
    else if (marks >= 70)
        printf("Grade B");
    else if (marks >= 60)
        printf("Grade C");
    else
        printf("Fail");

    return 0;
}

Output

Grade A

4. Nested if Statement

A nested if statement means placing one if statement inside another.

Example

#include <stdio.h>

int main()
{
    int age = 22;
    int citizen = 1;

    if (age >= 18)
    {
        if (citizen == 1)
            printf("Eligible to vote");
        else
            printf("Not a citizen");
    }
    else
    {
        printf("Not eligible");
    }

    return 0;
}

Output

Eligible to vote

5. switch Statement

The switch statement selects one block of code from multiple alternatives based on the value of an expression.

Syntax

switch (expression)
{
    case value1:
        // statements
        break;

    case value2:
        // statements
        break;

    default:
        // statements
}

Example

#include <stdio.h>

int main()
{
    int day = 3;

    switch (day)
    {
        case 1:
            printf("Monday");
            break;

        case 2:
            printf("Tuesday");
            break;

        case 3:
            printf("Wednesday");
            break;

        case 4:
            printf("Thursday");
            break;

        default:
            printf("Invalid Day");
    }

    return 0;
}

Output

Wednesday

Example: Calculator Using switch

#include <stdio.h>

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

    printf("Enter two numbers: ");
    scanf("%d%d", &a, &b);

    printf("Enter operator (+, -, *, /): ");
    scanf(" %c", &op);

    switch (op)
    {
        case '+':
            printf("Result = %d", a + b);
            break;

        case '-':
            printf("Result = %d", a - b);
            break;

        case '*':
            printf("Result = %d", a * b);
            break;

        case '/':
            if (b != 0)
                printf("Result = %d", a / b);
            else
                printf("Division by zero is not allowed");
            break;

        default:
            printf("Invalid Operator");
    }

    return 0;
}

Difference Between if and switch

if Statementswitch Statement
Tests one or more conditionsTests a single expression against multiple constant values
Can use relational and logical operatorsWorks with int, char, and enum values (constant case labels)
Suitable for complex conditionsBest for menu-driven and multiple-choice programs
Can evaluate ranges of valuesCannot directly evaluate ranges of values

Summary

  • if executes code when a condition is true.
  • if...else chooses between two alternatives.
  • if...else if...else handles multiple conditions.
  • Nested if places one if statement inside another.
  • switch selects one block of code from multiple options using case labels.
  • Use if for complex decision-making and switch for selecting among multiple fixed choices.

Related Posts