In this easy C language tutorial, we will learn everything about switch case statement in C programming, how to use and where to use a switch case.
Some times, a variable can have more than one values, and each value needs to execute different statements. For example,
Syntax:
switch(expression)
{
case value_1:
//statements
break;
case value_2:
//statements
break;
case value_3:
//statements
break;
default:
//statements
}
Example 1: switch case example in c, to print week day.
#include<stdio.h>
int main()
{
int day;
printf("\nEnter Weeknum");
scanf("%d", &day);
switch(day)
{
case 1:
printf("\nSunday");
break;
case 2:
printf("\nMonday");
break;
case 3:
printf("\nTuesday");
break;
case 4:
printf("\nWednesday");
break;
case 5:
printf("\nThursday");
break;
case 6:
printf("\nFriday");
break;
case 7:
printf("\nSaturday");
break;
default:
printf("\nWrong Choice");
}
return 0;
}
Sample Output:Enter Weeknum : 3
Tuesday
Rules for Switch Case Statement in C
the swi tch statement allows a variable to be tested, with the list of values, called the case.- the switch statement can have any number of case.
- All case must end with a colon ( : ).
- switch, only accept int or char variable as a parameter.
- float and text values are not allowed.
- If, no case value is matched than default is executed.
- the default is optional, you can skip in your program.
- the break is to end processing and exit from the switch.
Example 2: C program, to accept an alphabet, and check it is vowel or consonant.
#include<stdio.h>
int main()
{
char alph;
printf("\nEnter an Alphabet");
scanf("%c", &alph);
switch(alph)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("\nVowel");
break;
default:
printf("\nConsonant");
}
return 0;
}
Sample Output:Enter an Alphabet : A
Vowel
Mini Quiz
Mini Project
Mini Project: Simple Menu-Driven Calculator
Question:
Write a C program that displays the following menu and accepts the user’s choice.
- Addition
- Subtraction
- Multiplication
- Division
Then, accept two numbers from the user and perform the selected operation using the switch statement. If the user enters an invalid choice, display “Invalid Choice”.
View Answer Code
#include<stdio.h>
int main()
{
int choice, num1, num2;
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("\nEnter Your Choice: ");
scanf("%d", &choice);
printf("Enter Two Numbers: ");
scanf("%d%d", &num1, &num2);
switch(choice)
{
case 1:
printf("Result = %d", num1 + num2);
break;
case 2:
printf("Result = %d", num1 - num2);
break;
case 3:
printf("Result = %d", num1 * num2);
break;
case 4:
printf("Result = %d", num1 / num2);
break;
default:
printf("Invalid Choice");
}
return 0;
}
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
Shubhranshu Shekhar is a coding instructor, mentor, and founder of VSIT Delhi with 20+ years of teaching experience (since 2004). He has guided many students who are now working in multinational companies and specializes in Full Stack Development, Python, Digital Marketing, and Data Analytics.
