In this section, we will explore the switch statement in C, which provides a way to execute different parts of code based on the value of an expression. The switch statement is a more readable alternative to using multiple if-else statements when dealing with multiple conditions based on a single variable.

Key Concepts

  1. Syntax of switch Statement
  2. Using case Labels
  3. The default Case
  4. The break Statement
  5. Nested switch Statements
  6. Common Mistakes and Tips

  1. Syntax of switch Statement

The basic syntax of a switch statement is as follows:

switch (expression) {
    case constant1:
        // code to be executed if expression equals constant1
        break;
    case constant2:
        // code to be executed if expression equals constant2
        break;
    // you can have any number of case statements
    default:
        // code to be executed if expression doesn't match any case
}

Example

#include <stdio.h>

int main() {
    int day = 3;

    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\n");
    }

    return 0;
}

In this example, the value of day is 3, so the output will be "Wednesday".

  1. Using case Labels

Each case label must be followed by a constant expression. When the value of the switch expression matches a case label, the code associated with that case is executed.

Example

#include <stdio.h>

int main() {
    char grade = 'B';

    switch (grade) {
        case 'A':
            printf("Excellent!\n");
            break;
        case 'B':
        case 'C':
            printf("Well done\n");
            break;
        case 'D':
            printf("You passed\n");
            break;
        case 'F':
            printf("Better try again\n");
            break;
        default:
            printf("Invalid grade\n");
    }

    return 0;
}

In this example, the value of grade is 'B', so the output will be "Well done".

  1. The default Case

The default case is optional and is executed if none of the case labels match the switch expression. It is similar to the else part of an if-else statement.

Example

#include <stdio.h>

int main() {
    int number = 10;

    switch (number) {
        case 1:
            printf("Number is 1\n");
            break;
        case 2:
            printf("Number is 2\n");
            break;
        default:
            printf("Number is neither 1 nor 2\n");
    }

    return 0;
}

In this example, the value of number is 10, so the output will be "Number is neither 1 nor 2".

  1. The break Statement

The break statement is used to terminate a case in the switch statement. If break is omitted, the program continues to execute the next case regardless of the switch expression.

Example

#include <stdio.h>

int main() {
    int number = 2;

    switch (number) {
        case 1:
            printf("Number is 1\n");
        case 2:
            printf("Number is 2\n");
        case 3:
            printf("Number is 3\n");
        default:
            printf("Default case\n");
    }

    return 0;
}

In this example, the value of number is 2, but since there are no break statements, the output will be:

Number is 2
Number is 3
Default case

  1. Nested switch Statements

You can nest switch statements, but it is generally not recommended due to readability concerns.

Example

#include <stdio.h>

int main() {
    int outer = 1, inner = 2;

    switch (outer) {
        case 1:
            switch (inner) {
                case 1:
                    printf("Inner is 1\n");
                    break;
                case 2:
                    printf("Inner is 2\n");
                    break;
            }
            break;
        case 2:
            printf("Outer is 2\n");
            break;
    }

    return 0;
}

In this example, the output will be "Inner is 2".

  1. Common Mistakes and Tips

  • Forgetting break Statements: Omitting break can lead to fall-through behavior, which might not be intended.
  • Using Non-Constant Expressions: case labels must be constant expressions.
  • Overusing Nested switch Statements: This can make the code hard to read and maintain.

Practical Exercise

Exercise

Write a program that takes a number from the user and prints the corresponding month name. If the number is not between 1 and 12, print "Invalid month".

Solution

#include <stdio.h>

int main() {
    int month;

    printf("Enter a month number (1-12): ");
    scanf("%d", &month);

    switch (month) {
        case 1:
            printf("January\n");
            break;
        case 2:
            printf("February\n");
            break;
        case 3:
            printf("March\n");
            break;
        case 4:
            printf("April\n");
            break;
        case 5:
            printf("May\n");
            break;
        case 6:
            printf("June\n");
            break;
        case 7:
            printf("July\n");
            break;
        case 8:
            printf("August\n");
            break;
        case 9:
            printf("September\n");
            break;
        case 10:
            printf("October\n");
            break;
        case 11:
            printf("November\n");
            break;
        case 12:
            printf("December\n");
            break;
        default:
            printf("Invalid month\n");
    }

    return 0;
}

Conclusion

In this section, we learned about the switch statement in C, which allows for more readable and organized code when dealing with multiple conditions based on a single variable. We covered the syntax, usage of case labels, the default case, the importance of break statements, and nested switch statements. We also provided a practical exercise to reinforce the concepts learned. In the next section, we will explore the break and continue statements in more detail.

© Copyright 2024. All rights reserved