Solution for Exercise 8-19

ex0819

#include <stdio.h>

int main()
{
    char choice;

    puts("Meal Plans:");
    puts("A - Breakfast, Lunch, and Dinner");
    puts("B - Lunch and Dinner only");
    puts("C - Dinner only");
    printf("Your choice: ");
    scanf("%c",&choice);

    printf("You've opted for ");
    switch(choice)
    {
        case 'A':
            printf("Breakfast, ");
        case 'B':
            printf("Lunch and ");
        case 'C':
            printf("Dinner ");
        default:
            printf("as your meal plan.\n");
    }
    return(0);
}

Output

Meal Plans:
A - Breakfast, Lunch, and Dinner
B - Lunch and Dinner only
C - Dinner only
Your choice: B
You've opted for Lunch and Dinner as your meal plan.

Notes

* If you don't type your response in uppercase, the output looks like this:

You've opted for as your meal plan.

* Try various options for input.

* See how the default part of the structure is executed no matter what?

* Without any break statements, execution always falls through this switch-case structure.