Switch Statement in C programming with examples


Switch statement allows us to execute one statement from many statement and that statements are called case. Actually in switch statement, inside the body of switch a number of cases are used and a parameter are passed and from which case this parameter is matched, executed

Syntax



1.In the switch statement a value/number is passed in the place of parameter and that case will execute which is equal to that value/number.
2.If no case matched with parameter then default case will execute.

Example 1 

#include<stdio.h>
int main()
{
//Assigning parameter's value
int p=2;
switch(p)
{
case 1:
printf("it is case 1");
break;
case 2:
printf("it is case 2");
break;
case 3:
printf("it is case 3");
break;
default:
printf("no case matched");
}
return 0;
}
### output ###
it is case 2
//because p=2 so case 2 will execute

Example 2 

#include<stdio.h>
int main()
{
//Assigning parameter's value
int p=5;
switch(p)
{
case 1:
printf("it is case 1");
break;
case 2:
printf("it is case 2");
break;
case 3:
printf("it is case 3");
break;
default:
printf("no case matched");
}
return 0;
}
/*
### output ###
no case matched
because value of p is 5 and it will not match with any case
so default case will execute
*/

Example 3 

#include<stdio.h>
int main()
{
//Assigning parameter's value
int p=5;
switch(p)
{
case 1:
printf("Hello");
break;
case 2:
printf("Hi");
break;
case 3:
printf("Tata");
break;
case 2+1:
printf("Bye");
break;
default:
printf("no case matched");
}
return 0;
}
/*
### output ###
Error
*/

Duplicate case are not allowed in switch statement and here case 3 and case 2+1 are duplicate of each other.

Example 4 

#include<stdio.h>
int main()
{
//Assigning parameter's value
int p=2;
switch(p)
{
case 1:
printf("Hello");
break;
case 2:
printf("Hi");

case 3:
printf("Tata");
break;
case 4:
printf("Bye");
break;
default:
printf("no case matched");
}
return 0;
}
/*
### output ###
HiTata
*/

If we remove break from any case then the next case will execute automatically so here value of parameter is 2 and it will match with case so Hi will print but there is no break after case 2 so here case 3 will also execute and it will print Tata so the final output is HiTata.

Example 5 

#include<stdio.h>
int main()
{
//Assigning parameter's value
int p=3;
switch(p)
{
default:
printf("no case matched");
case 1:
printf("Hello");
break;
case 2:
printf("Hi");
break;
case 3:
printf("Tata");
break;
case 4:
printf("Bye");
break;
}
return 0;
}
/*
### output ###
Tata
*/

default case can be write anywhere in the body of switch but it will execute when no case matched.

Program: Check given Alphabet is Vowel or Consonant

#include<stdio.h>
int main()
{
//variable declaration
char ch;
printf("Enter any alphabet\n");
scanf("%c",&ch);
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
	printf("Vowel");
	break;
default:
	printf("Consonant");
}
return 0;
}
/*
### output ###
Enter any alphabet
M
Consonant
*/

Here I am requesting if you found this post was helpful for you then let me know by your comment in below comment box and share it with your friend. Also you can contact me on Instagram @rajusunagar_

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thank you!!.......


Post a Comment

0 Comments