Switch Case Default In C Programming Language

In this video tutorial lets learn how switch case decision control statement works and its syntax in C Programming Language.

Related Read:
Simple Calculator Program using Switch Case: C
break Statement In C Programming Language
Continue Statement In C Programming Language

Note: The condition in switch must be a integer constant or an expression which evaluates to an integer constant.

Video Tutorial: Switch Case Default In C Programming Language


[youtube https://www.youtube.com/watch?v=XklMeJ5ha1A]

YouTube Link: https://www.youtube.com/watch?v=XklMeJ5ha1A [Watch the Video In Full Screen.]

Source Code: Switch Case Default In C Programming Language

#include<stdio.h>

int main()
{
    char choice;

    printf("Enter your choice\n");
    scanf("%c", &choice);

    switch(choice)
    {
        case 'a':
        case 'A': printf("You typed A or a\n");
                  break;

        case 'b':
        case 'B': {
                   printf("You typed B or b\n");
                   break;
                  }

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
B
You typed B or b

Output 2
Enter your choice
b
You typed B or b

Here we’ve not specified keyword break after case ‘a’ and case ‘b’, that means if user enters a or A, for both whatever is present in case ‘A’ gets executed and then control exits switch case when it encounters break statement. Similarly for ‘b’ and ‘B’.

Also note that curly braces are optional here. Infact its unnecessary to use curly braces in switch case statements.

#include<stdio.h>

int main()
{
    int choice;

    printf("Enter your choice\n");
    scanf("%d", &choice);

    switch(choice)
    {
        case 1: printf("This is first statement\n");

        case 2: printf("This is second statement\n");

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
1
This is first statement
This is second statement
Your selection is wrong!

Output 2
Enter your choice
2
This is second statement
Your selection is wrong!

Here we’re not using break after any case, so it’s printing statements present in all the cases.

#include<stdio.h>
int main()
{
    int choice;

    printf("Enter your choice\n");
    scanf("%d", &choice);

    switch(choice)
    {
        case 1: printf("This is first statement\n");
                break;

        case 2: printf("This is second statement\n");
                break;

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
1
This is first statement

Output 2
Enter your choice
2
This is second statement

Output 3
Enter your choice
a
Your selection is wrong!

#include<stdio.h>
int main()
{
    int choice;

    printf("Enter your choice\n");
    scanf("%d", &choice);

    switch(choice)
    {
        case 2: printf("Apple\n");
                break;

        case 1: printf("IBM\n");
                break;

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
1
IBM

Here order doesn’t matter. As you can see from above example, case 2 comes before case 1.

#include<stdio.h>
int main()
{
    int choice;

    printf("Enter your choice\n");
    scanf("%d", &choice);

    switch(choice)
    {
        printf("List of companies\n");
        case 2: printf("Apple\n");
                break;

        case 1: printf("IBM\n");
                break;

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
1
IBM

All the code must go inside any of the cases orelse those code will not have any effect. In above program the printf statement with “List of companies” doesn’t get printed and it doesn’t even throw any error.

Note:
1. default case is optional.
2. Value of cases in switch must always be unique.
3. switch can not be used with float, double.
4. Switch, case, default are all builtin keywords.
5. continue does not take the control back to the start of switch.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert