An if-else statement chooses between two paths. Awarding a grade, however, needs a choice among five or six. C offers two statements for selecting one path out of many: the else-if ladder and the switch statement.

The else-if Ladder

Definition: else-if ladder. A chain of if-else statements in which each else part contains the next if, so that the conditions are tested in order and only the block of the first true condition is carried out.

if (condition1)
{
    block 1
}
else if (condition2)
{
    block 2
}
else
{
    default block
}

The conditions are tested from the top. As soon as one is true, its block runs and the rest of the ladder is skipped. If none is true, the final else block runs.

Order of the Conditions

Because only the first true condition is acted upon, the order matters. For example, in a grade ladder that tests mark >= 80 first, the test mark >= 60 is reached only when mark >= 80 is false, so it really means "from 60 to 79". If the ladder began with mark >= 40, a mark of 95 would satisfy it at once and receive grade C. Range tests must therefore run from the highest band down, or from the lowest band up.

The switch Statement

Definition: switch statement. A multiple-way selection statement that compares the value of an integer or character expression with a list of constant case labels and transfers control to the matching case.

switch (expression)
{
    case constant1:
        statements;
        break;
    case constant2:
        statements;
        break;
    default:
        statements;
}

The expression is evaluated once. Control jumps to the case whose constant equals its value. The break statement then leaves the switch. If no case matches, the default part runs.

Fall-Through

If break is omitted, execution continues into the next case, whether or not it matches. This is called fall-through. It is usually a mistake, but it can be used deliberately to let several case labels share the same statements.

Choosing Between the Two

Table 3.8 compares the two statements.

Table 3.8: Comparison of the else-if ladder and the switch statement

Point else-if ladder switch
Tests Any conditions, including ranges Equality with constants only
Type of value Any type Integer or character
Typical use Grade bands, ranges of values Menu choices, day numbers
No match Final else default

Example 3.8: Grade and remark

Problem: Read a mark from 0 to 100. Use an else-if ladder to find the grade and a switch statement to display a remark for that grade.

Solution:

  1. Validate the mark first.
  2. Test the grade bands from the highest downwards.
  3. Use the grade character as the switch expression.
#include <stdio.h>

int main(void)
{
    int mark;
    char grade;

    printf("Enter mark (0-100): ");
    scanf("%d", &mark);
    if (mark < 0 || mark > 100)
    {
        printf("\nInvalid mark\n");
        return 0;
    }
    if (mark >= 80)
    {
        grade = 'A';
    }
    else if (mark >= 60)
    {
        grade = 'B';
    }
    else if (mark >= 40)
    {
        grade = 'C';
    }
    else
    {
        grade = 'F';
    }
    printf("\nGrade: %c\n", grade);
    switch (grade)
    {
        case 'A':
            printf("Remark: Excellent\n");
            break;
        case 'B':
            printf("Remark: Good\n");
            break;
        case 'C':
            printf("Remark: Pass\n");
            break;
        default:
            printf("Remark: Needs improvement\n");
    }
    return 0;
}

Input:

72

Output:

Enter mark (0-100):
Grade: B
Remark: Good

Result: 72 is not 80 or more, but it is 60 or more, so the grade is B. The switch matches case 'B', displays "Good" and leaves at the break.

Common mistakes

  • Arranging range conditions in an order that lets a lower band capture higher values.
  • Omitting break in a switch, causing unintended fall-through.
  • Using a range or a variable as a case label; case labels must be constants.

Key takeaways

  • The else-if ladder tests conditions in order and runs only the first true block.
  • The order of range conditions decides the correctness of the ladder.
  • switch compares an integer or character expression with constant case labels.
  • break ends each case; default handles values that match no case.

Practice questions

  1. In an else-if ladder, how many blocks are carried out?
  2. What happens when break is omitted at the end of a case?
  3. Which statement is more suitable for checking whether a mark lies in a range?

Answers to the practice questions

  1. Exactly one: the block of the first true condition, or the final else block if none is true.
  2. Execution falls through into the statements of the next case.
  3. The else-if ladder, because switch can test only equality with constants.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit III