C Switch Case – How to use

shape
shape
shape
shape
shape
shape
shape
shape

You will learn how to use the switch command in C to execute a block of code based on a multiple choice selection.

Below is an example of a MENU using the switch command:

switch (menu) {
    case 1:
        printf("Voce escolheu a opcao 1");
        break; 
    case 2:
        printf("Voce escolheu a opcao 2");
        break;
    default:
        printf("Voce nao escolheu uma opcao valida");
        break;
}

If the menu variable is worth 1, the following command line will be executed:

printf("Voce escolheu a opcao 1");

If the menu variable is worth 2, the following command line will be executed:

printf("Voce escolheu a opcao 2");

If MENU is not equal to any of the above values, the following command line will be executed:

printf("Voce nao escolheu uma opcao valida");

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest news

Latest news directly from our blog.