Digital Learning

Friday, February 21, 2020

Switch-Case Statement in C programming:

Switch Case Statement:
A switch case statement is allows a variable to be checked for equality against a list of values. Every value is called a case, and the variable being switched on is checked for each switch case.
Switch case statement can be used to solve more option type problems, like: Menu like program, where one value is associated with each option and you have to select only one at a time.
Switch case statement is a control statement that allows us to select only one case among the many given cases. The expression in switch process to return an integral value, which is then compared to the values present in different cases. If there is no case match, then default block is executed.

Syntax:


The syntax for a switch statement:


Switch (expression)

{
Case constant-expression:
Statement;
Break; /*optional*/
Case constant- expression:
Statement;
Break; /*optional*/
.
.
.
.default:
Statement;
}
The following points should apply to a switch statement −
The expression used in a switch statement must have an integer type, or be of a class type in which the class has a single conversion function to an integral type.
You can apply any number of case statements within a switch case. Each case is followed by the value.
The expression for a case should be the same data type as the variable data type, and it should be a constant.
When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is meet.
When break statement comes, the current case statement automatically terminate,and next statement will execute.
There is no need to assign break statement after each case. If no break appears, the flow of control will fall through to subsequent cases until a break statement is meet.
A switch case statement can have an optional default case statement, which should appear at the end of the switch statement. The default case can be used for performing a task when none of the cases is find true. There is no need of break in the default case.
Flow Diagram:


Example:

#include<stdio.h>
#include<conio.h>
Int main()
{
Char grade=’b’;
Switch (grade)
{
case ‘a’:
printf(“Very Good”);
Break;
case ‘b’:
case ‘c’:
printf(“Good”);
break;
case ‘d’: 
printf(“fine”);
break;
default:
printf(“Wrong”);
}
Return 0;

}

Program: Program:Enter grade 'A' for Manager, 'B' for Assistant manager, 'C' for Accountant, 'D' for Clerk and print the same.

#include<stdio.h>

#include<conio.h>
void main()
{
char grade;
clrscr();
printf("\n Enter grade");
scanf("%c", &grade);
switch(grade)
{
case 'a':
printf(" Manager");
break;
case 'b':
printf(" Assistant manager");
break;
case 'c':
printf(" Accountant");
break;
default:
printf("clerk");
}
getch();
}

Output:

Enter grade a
Manager

Program: Find out vowel and consonant.

#include<stdio.h>
#include<conio.h>
void main()
{
char grade;
clrscr();
printf("\n Enter grade");
scanf("%c", &grade);
switch(grade)
{
case 'a':
printf("Vowel");
break;
case 'e':
printf("Vowel");
break;
case 'i':
printf("Vowel");
break;
case 'o':
printf(" Vowel");
break;
case 'u':
printf("Vowel");
break;
default:
printf("Consonant"):
}
getch();
}

Output:

Enter grade a
Vowel
Enter grade c
Consonant


Program: Perform addition, subtraction, multiplication, and division operation using switch case statement.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
char operation;
clrscr();
printf("\n Enter operation");
scanf("%c", &operation);
printf("Enter any two number:");
scanf("%d%d",&a,&b);
switch(operation)
{
case '+':
c=a+b;
printf("\n Addition=%d", c);
break;
case '-':
c=a-b;
printf("\n Subtraction=%d",c);
break;
case '*':
c=a*b;
printf("\n Multiplication=%d",c);
break;
case '/':
c=a/b;
printf("\n Division=%d",c);
break;
getch();
}

Output:

Enter operation +
Enter any two numbers: 5
6
Addition=11

No comments:

Post a Comment