Switch is conditional control statement, which helps to make choice and decision. In switch statement, we use switch, case and default keywords. In switch case first of all switch is evaluated and then match it one by one with all case_value. When a match is found, then related programming statement will be executed and it will check again next case and all case will be checked. Therefore, we have to use break keyword to stop the program if case matched.

Syntax of Switch


Switch(expression)
{
case case_value:
-----Tasks----
break;
case case_value1:
-------Tasks-----
break;
Default:
--------Taksk-----
---------------------
}

		


Example of Switch

In the following example, we will take input from the user and according to the value, we will print day. For example, if user will enter one it would be Monday. So accordingly, we will print days. If number is not between 1 to 7 then it would print wrong day and we have mentioned this in default. In switch, default is like else. If not condition matches then default statement will be called.

		
		#include<stdio.h>
void main()
{
    int x;
    printf("Enter numeric value ");
    scanf("%d",&x);

    switch(x)
    {
    case 1:
        printf("Monday");
        break;
    case 2:
        printf("Tuesday");
        break;
    case 3:
        printf("Wednesday");
        break;
    case 4:
        printf("Thursday");
        break;
    case 5:
        printf("Friday");
        break;
    case 6:
        printf("Saturday");
        break;
    case 7:
        printf("Sunday");
        break;
    default:
        printf("Wrong Day");
        break;
    }

}

		
		

In switch case, value must be either integer or character. We cannot use floating and string values. Default is optional in switch.