What is switch case and its rules in c programming

Switch Statement


Switch case statements are a substitute for long if statements that compare a variable to several integral values. The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. Switch is a control statement that allows a value to change control of execution.


Syntax:

switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases
}



The following rules apply to a switch statement −
The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.
• You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
• The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
• When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
• When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
• Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
• A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.


Example 1:
Input month number and print month in words

#include<stdio.h>
int main()
{
int month_num;
printf("Enter month number ");
scanf("%d",&month_num);
switch(month_num)
{
case 1: printf("\nJan");
break;
case 2: printf("\nFeb");
break;
case 3: printf("\nMar");
break;
case 4: printf("\nApr");
break;
default: printf("Not month number");
}
return 0;
}
output:-
Enter month number 1

Jan

Example 2:
Input a character and print cities names starting with the character 

#include<stdio.h>
int main()
{
char let;
printf("Enter first character of city name ");
scanf("%c",&let);
switch(let)
{
case 'a':
case 'A':
printf("\nAurangabad");
break;
case 'p':
case 'P':
printf("\nPune");
break;
default: printf("\nNot in list");
}
return 0;
}
output
Enter first character of city name p

Pune

Example 3:
Input number and provide menu 1.even/odd 2.+ve/-ve
allow user to select option and execute that process

#include<stdio.h>
int main()
{
int num,choice;
printf("Enter number ");
scanf("%d",&num);
printf("\n1.even/odd\n2.+ve/-ve\nEnter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1: if(num%2==0)
printf("\nEven Number");
else
printf("\nOdd Number");
break;
case 2: num>0?printf("+ve"):printf("-ve");
break;
default: printf("\nInvalid choice");
}
return 0;
}

output:
Enter number -2

1.even/odd
2.+ve/-ve
Enter your choice1

Even Number


Post a Comment

If you have any doubt or suggestions.please let me know.

Previous Post Next Post

Ads by google

Ads by google