Switch Case

C Programming Tutorial: Switch Statement

Welcome to the Codes With Pankaj "Switch Statement in C Programming" tutorial! This tutorial will guide you through the usage of the switch statement in C for multi-way decision-making.

Table of Contents

1. Introduction to Switch Statement

The switch statement in C allows the program to perform multi-way branching based on the value of an expression. It provides an alternative to the if-else if-else ladder for decision-making.

2. Syntax of Switch Statement

switch (expression) {
    case constant1:
        // Code to execute if expression matches constant1
    case constant2:
        // Code to execute if expression matches constant2
    // Add more case statements as needed
        // Code to execute if expression does not match any constant

3. Example: Simple Switch Statement

#include <stdio.h>

int main() {
    int choice = 2;
    switch (choice) {
        case 1:
            printf("Choice is 1\n");
        case 2:
            printf("Choice is 2\n");
        case 3:
            printf("Choice is 3\n");
            printf("Invalid choice\n");
    return 0;

4. Case Ranges in Switch Statement

Switch cases can specify a range of values using the comma operator.

switch (score) {
    case 0:
    case 1:
    case 2:
        printf("Low score\n");
    case 3:
    case 4:
    case 5:
        printf("Medium score\n");
        printf("High score\n");

5. Default Case

The default case is executed if none of the case constants match the value of the expression.

6. Break Statement

The break statement is used to exit the switch statement and prevent fallthrough behavior.

7. Fallthrough Behavior

Without a break statement, control will fall through to the next case, executing all subsequent case statements until a break or the end of the switch statement is reached.

8. Nested Switch Statements

Switch statements can be nested inside other switch statements for complex decision-making.

9. Best Practices

  • Always include a default case to handle unexpected values.

  • Use break statements to prevent fallthrough behavior and improve code clarity.

  • Consider using if-else statements instead of switch statements for more complex conditions.

10. Exercises

Try these exercises to practice switch statements in C:

  1. Exercise 1: Write a program to display the name of a month based on its number (1 for January, 2 for February, etc.).

  2. Exercise 2: Implement a program to classify a character as a vowel or consonant.

  3. Exercise 3: Create a program to calculate the number of days in a month based on the month number (1 for January, 2 for February, etc.).

  4. Exercise 4: Write a program to determine the type of a triangle based on the lengths of its sides.

  5. Exercise 5: Implement a program to calculate the cost of a product based on its category (1 for electronics, 2 for clothing, etc.).

We hope this tutorial has helped you understand the switch statement in C programming. Practice with the exercises provided to reinforce your understanding. Happy coding!

For more tutorials, visit www.codeswithpankaj.com.

Last updated