C Programming
About Lesson
Understanding the Switch-Case Structure

The switch-case structure allows us to execute different blocks of code based on the value of a variable or expression.

  • Syntax:

    switch (expression) {
    case value1:
    // Statements
    break;
    case value2:
    // Statements
    break;
    // More cases...
    default:
    // Default statements
    }
  • Example:

    int choice;

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

    switch (choice) {
    case 1:
    printf("You chose option 1.n");
    break;
    case 2:
    printf("You chose option 2.n");
    break;
    case 3:
    printf("You chose option 3.n");
    break;
    default:
    printf("Invalid choice.n");
    }

2: Implementing Switch-Case for Menu-Driven Programs

A common use case of switch-case in C is to implement menu-driven programs where users can choose different options.

  • Example:

    #include <stdio.h>

    int main() {
    int choice;

    while (1) {
    printf("Menu:n");
    printf("1. Option 1n");
    printf("2. Option 2n");
    printf("3. Option 3n");
    printf("4. Exitn");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
    case 1:
    printf("You chose Option 1.n");
    // Implement functionality for Option 1
    break;
    case 2:
    printf("You chose Option 2.n");
    // Implement functionality for Option 2
    break;
    case 3:
    printf("You chose Option 3.n");
    // Implement functionality for Option 3
    break;
    case 4:
    printf("Exiting...n");
    return 0;
    default:
    printf("Invalid choice. Please try again.n");
    }
    }

    return 0;
    }

  • Explanation:

    • The program displays a menu with options and prompts the user to enter a choice.
    • Based on the user’s input, the switch-case structure executes the corresponding block of code.
    • The default case handles invalid choices and prompts the user to try again.
3: Practice and Examples
  • Practice Exercise:
    • Write a menu-driven program that performs basic arithmetic operations (addition, subtraction, multiplication, division) based on user input.
    • Use switch-case to implement different cases for each operation.
#include <stdio.h>

int main() {
int choice;
float num1, num2;

while (1) {
printf("Menu:n");
printf("1. Additionn");
printf("2. Subtractionn");
printf("3. Multiplicationn");
printf("4. Divisionn");
printf("5. Exitn");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
printf("Result: %.2fn", num1 + num2);
break;
case 2:
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
printf("Result: %.2fn", num1 - num2);
break;
case 3:
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
printf("Result: %.2fn", num1 * num2);
break;
case 4:
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
if (num2 != 0) {
printf("Result: %.2fn", num1 / num2);
} else {
printf("Error: Division by zero.n");
}
break;
case 5:
printf("Exiting...n");
return 0;
default:
printf("Invalid choice. Please try again.n");
}
}

return 0;
}

Switch-case statements are powerful tools for implementing menu-driven applications and handling multiple scenarios based on user input. Practice using switch-case structures to enhance your problem-solving skills in C programming.