SWITCH CASE STATEMENT IN C
(MENU DRIVEN PROGRAMMING IN C )
Switch Case In General . . . .
Menu Driven Programs in C Language can be written, in most easiest way using "Switch Case" Statement.
Switch case statements can be used as alternative for "if... else ladder", where you can choose to execute single block of code among multiple blocks.
Then question remains..... what is difference between if... else ladder and Switch case..?
Lets understand this with simple example.
You have a Electric Switch Board with around 20 switches on it. If someone ask you to switch on fan, what would you have done in this situation when you don't know which switch belong to fan. Now suppose that, 18th Switch is fan's switch. you might have start checking from first switch and after checking first 17 switches you would have come to know that 18th switch belongs to fan.
Now, what if same condition occurs once again ..?.
In this case you would have switched on 18th Switch directly instead of checking first 17 switches once again.
Switch case statement in C works in same way. Using Switch case you can directly execute the block of code which you want to execute.
Introduction
- Switch statement tests the Input value with various cases.
- Once the case match is found, appropriate block of statements associated with that particular case is executed.
- After every block of statement "break" statement should be used in all cases to take control out of switch.
- User Input to switch case may contain Numeric Values, Characters or may be Expression.
- we cannot use real (float) numbers in switch case.
- In case, if User Input does not match with any case values "default" case get executes.
- This is exactly similar to if... else ladder. In if.. else ladder.
- In if....else ladder when none of the condition get execute default "else block" executes. So that "else block" is similar to "default case" here.
Data Flow Diagram :
Syntax of Switch Case Statement :
switch(Input Expression)
{
case Constant_1: Block of Code to be executed;
break;
case Constant_2: Block of Code to be executed;
break;
case Constant_3: Block of Code to be executed;
break;
.
.
case Constant_n: Block of Code to be executed;
break;
default : Default Block to be Executed if none of Constant get match with Input Expression;
}
Example of Switch Case Statement :
let us consider below program which will create Menu Driven code for Performing Arithmetic Operations (Addition, Subtraction, Multiplication and Division)
#include<stdio.h>
int main()
{
int n1,n2,choice;
printf("\n\t\t ---- MENU DRIVEN PROGRAM FOR ARITHMETIC OPERATIONS ----");
printf("\n\t\t 1. ADDITION \n\t\t 2. SUBSTRACTION");
printf("\n\t\t 3. MULTIPLICATION \n\t\t 4. DIVISION");
printf("\n\t\t ENTER YOUR CHOICE : ");
scanf("%d",&choice);
printf("\n\t\t INPUT TWO NUMBERS : ");
scanf("%d%d",&n1,&n2);
switch(choice)
{
case 1: printf("\n\t\t ADDITION : %d",n1+n2);
break;
case 2: printf("\n\t\t SUBTRACTION : %d",n1-n2);
break;
case 3: printf("\n\t\t MULTIPLICTION : %d",n1*n2);
break;
case 4: printf("\n\t\t DIVISION : %d",n1/n2);
break;
default: printf("\n\t\t WRONG CHOICE");
}
return 0;
}
Output:
---- MENU DRIVEN PROGRAM FOR ARITHMETIC OPERATIONS ----
1. ADDITION
2. SUBSTRACTION
3. MULTIPLICATION
4. DIVISION
ENTER YOUR CHOICE : 1
INPUT TWO NUMBERS : 25 85
ADDITION : 30
-----------------------------------------------------------------------------------
Explanation of Code :
Variables for Input Numbers and Choice have been declared.
int n1,n2,choice;
Menu has been created for End User.
printf("\n\t\t ---- MENU DRIVEN PROGRAM FOR ARITHMETIC OPERATIONS ----");
printf("\n\t\t 1. ADDITION \n\t\t 2. SUBSTRACTION");
printf("\n\t\t 3. MULTIPLICATION \n\t\t 3. DIVISION");
Choice of Operation has been asked to End User.
printf("\n\t\t ENTER YOUR CHOICE : ");
scanf("%d",&choice);
Choice of Input Numbers have been asked to End User.
printf("\n\t\t INPUT TWO NUMBERS : ");
scanf("%d%d",&n1,&n2);
Input Choice has been compared with case constants.
The block of code of respective case constant which will get match with input choice will be executed.
break statement is used to take control out of switch case after executing particular case
switch(choice)
{
case 1: printf("\n\t\t ADDITION : %d",n1+n2);
break;
case 2: printf("\n\t\t SUBTRACTION : %d",n1-n2);
break;
case 3: printf("\n\t\t MULTIPLICTION : %d",n1*n2);
break;
case 4: printf("\n\t\t DIVISION : %d",n1/n2);
break;
default: printf("\n\t\t WRONG CHOICE");
}
if Input choice doesn't match with Case Constants in Switch Case, Default block gets execute.
for example:
here user has to enter choice from 1 to 4. if user enters anything else, it wont get match with case constants and default block will execute and user will get output message "WRONG CHOICE".
we can also have characters as Case Constants.
Lets convert the same program with characters as Case Constants.
int main()
{
int n1,n2;
char ch;
printf("\n\t\t ---- MENU DRIVEN PROGRAM FOR ARITHMETIC OPERATIONS ----");
printf("\n\t\t +. ADDITION \n\t\t -. SUBSTRACTION");
printf("\n\t\t *. MULTIPLICATION \n\t\t /. DIVISION");
printf("\n\t\t ENTER YOUR CHOICE : ");
scanf("%c",&ch);
printf("\n\t\t INPUT TWO NUMBERS : ");
scanf("%d%d",&n1,&n2);
switch(ch)
{
case '+': printf("\n\t\t ADDITION : %d",n1+n2);
break;
case '-': printf("\n\t\t SUBSTRACTION : %d",n1-n2);
break;
case '*': printf("\n\t\t MULTIPLICTION : %d",n1*n2);
break;
case '/': printf("\n\t\t DIVISION : %d",n1/n2);
break;
default: printf("\n\t\t WRONG CHOICE");
}
return 0;
}
Note : float / Real values cant be used as Case Constants.
-------------------------------------------------------------------------------
More Example on Switch Case :
Write a Menu Driven Program for Calculating Areas of various Geographical shapes like Circle, Rectangle, Triangle, Square etc.
int main()
{
int rad,base,height,breadth,length,side,choice;
float area;
printf("\n\t\t MENU DRIVEN PROGRAM FOR CALCULATING AREAS OF VARIOUS SHAPES");
printf("\n\t\t 1. AREA OF CIRCLE");
printf("\n\t\t 2. AREA OF RECTANGLE");
printf("\n\t\t 3. AREA OF TRIANGLE");
printf("\n\t\t 4. AREA OF SQUARE");
printf("\n\t\t ENTER YOUR CHOICE : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n\t\t ENTER RADIUS : ");
scanf("%d",&rad);
area=3.142*rad*rad;
printf("\n\t\t AREA OF CIRCLE : %f",area);
break;
case 2: printf("\n\t\t ENTER BREADTH AND LENGTH : ");
scanf("%d%d",&breadth,&length);
area=breadth*length;
printf("\n\t\t AREA OF RECTANGLE : %f",area);
break;
case 3: printf("\n\t\t ENTER BASE AND HEIGHT : ");
scanf("%d%d",&base,&height);
area=0.5*base*height;
printf("\n\t\t AREA OF TRIANGLE : %f",area);
break;
case 4: printf("\n\t\t ENTER SIDE OF SQUARE : ");
scanf("%d",&side);
area=side*side;
printf("\n\t\t AREA OF SQUARE : %f",area);
break;
default : printf("\n\t\t WRONG CHOICE CODE ");
}
return 0;
}
Output :
MENU DRIVEN PROGRAM FOR CALCULATING AREAS OF VARIOUS SHAPES
1. AREA OF CIRCLE
2. AREA OF RECTANGLE
3. AREA OF TRIANGLE
4. AREA OF SQUARE
ENTER YOUR CHOICE : 3
ENTER BASE AND HEIGHT : 20 35
AREA OF TRIANGLE : 350.000000
---------------------------------------------------------------------------------
Switch Case Statement with Looping :
Above program will Display Menu Once and will ask user to input choice only once.
What if user want to get menu to be displayed till "Exit Menu" option would be selected. This could be done using looping with switch case statement.
Switch Case Statement can also be used with looping so that the menu will get displayed till end user choose to exit program in following way.
Program with Looping :
int main()
{
int rad,base,height,breadth,length,side,choice=1;
float area;
printf("\n\t\t MENU DRIVEN PROGRAM FOR CALCULATING AREAS OF VARIOUS SHAPES");
while(choice!=5)
{
printf("\n\t\t 1. AREA OF CIRCLE");
printf("\n\t\t 2. AREA OF RECTANGLE");
printf("\n\t\t 3. AREA OF TRIANGLE");
printf("\n\t\t 4. AREA OF SQUARE");
printf("\n\t\t ENTER YOUR CHOICE : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n\t\t ENTER RADIUS : ");
scanf("%d",&rad);
area=3.142*rad*rad;
printf("\n\t\t AREA OF CIRCLE : %f",area);
printf("\n\t\t --------------------");
break;
case 2: printf("\n\t\t ENTER BREADTH AND LENGTH : ");
scanf("%d%d",&breadth,&length);
area=breadth*length;
printf("\n\t\t AREA OF RECTANGLE : %f",area);
printf("\n\t\t --------------------");
break;
case 3: printf("\n\t\t ENTER BASE AND HEIGHT : ");
scanf("%d%d",&base,&height);
area=0.5*base*height;
printf("\n\t\t AREA OF TRIANGLE : %f",area);
printf("\n\t\t --------------------");
break;
case 4: printf("\n\t\t ENTER SIDE OF SQUARE : ");
scanf("%d",&side);
area=side*side;
printf("\n\t\t AREA OF SQUARE : %f",area);
printf("\n\t\t --------------------");
break;
case 5: printf("\n\t\t EXIT THE PROGRAM ");
break;
default : printf("\n\t\t WRONG CHOICE CODE ");
}
} // End of While Loop
return 0;
}
MENU DRIVEN PROGRAM FOR CALCULATING AREAS OF VARIOUS SHAPES
1. AREA OF CIRCLE
2. AREA OF RECTANGLE
3. AREA OF TRIANGLE
4. AREA OF SQUARE
ENTER YOUR CHOICE : 1
ENTER RADIUS : 4
AREA OF CIRCLE : 50.271999
--------------------
1. AREA OF CIRCLE
2. AREA OF RECTANGLE
3. AREA OF TRIANGLE
4. AREA OF SQUARE
ENTER YOUR CHOICE : 3
ENTER BASE AND HEIGHT : 10 6
AREA OF TRIANGLE : 30.000000
--------------------
1. AREA OF CIRCLE
2. AREA OF RECTANGLE
3. AREA OF TRIANGLE
4. AREA OF SQUARE
ENTER YOUR CHOICE : 5
EXIT THE PROGRAM
----------------------------------------------------------------------------
Nested Switch Case / Inner Switch Case Statement
Switch Case Statement can also be of nested form, means, Cases of Switch Case statement can have Switch case as code.
lets understand this with following example.
Lets Create a Menu Menu Asking for Choice of Veg or Non Veg dish.
when user selects to have Veg or Non-veg option, next menu should get display with available options of veg or Non-veg dishes.
int main()
{
char ch;
int veg,nonveg;
printf("\n\t\t NESTED SWITCH CASE/INNER SWITCH CASE DEMONSTRATION");
printf("\n\t\t A. VEG");
printf("\n\t\t B. NON VEG");
printf("\n\t\t ENTER YOUR CHOICE OF FOOD (A OR B) : ");
scanf("%c",&ch);
switch(ch)
{
case 'A': printf("\n\t\t VEG MENU ");
printf("\n\t\t 1. RAJASTANI THALI ");
printf("\n\t\t 2. BENGALI THALI");
printf("\n\t\t 3. MAHARASTRIAN THALI");
printf("\n\t\t 4. KASHMIRI THALI");
printf("\n\t\t CHOOSE YOUR FAVORITE THALI : ");
scanf("%d",&veg);
switch(veg)
{
case 1: printf("\n\t\t YOUR ORDER FOR RAJASTANI THALI HAS BEEN PLACED");
break;
case 2: printf("\n\t\t YOUR ORDER FOR BENGALI THALI HAS BEEN PLACED");
break;
case 3: printf("\n\t\t YOUR ORDER FOR MAHARASHTRIAN THALI HAS BEEN PLACED");
break;
case 4: printf("\n\t\t YOUR ORDER FOR KASHMIRI THALI HAS BEEN PLACED");
break;
default: printf("\n\t\t WRONG CHOICE");
}
break;
case 'B': printf("\n\t\t NON-VEG MENU ");
printf("\n\t\t 1. GRILLED CHICKEN ");
printf("\n\t\t 2. MUTTON KORMA");
printf("\n\t\t 3. TANDOORI LAMB CHOPS");
printf("\n\t\t 4. FISH BIRYANI");
printf("\n\t\t CHOOSE YOUR FAVORITE DISH : ");
scanf("%d",&nonveg);
switch(nonveg)
{
case 1: printf("\n\t\t YOUR ORDER FOR GRILLED CHICKEN HAS BEEN PLACED");
break;
case 2: printf("\n\t\t YOUR ORDER FOR MUTTON KORMA HAS BEEN PLACED");
break;
case 3: printf("\n\t\t YOUR ORDER FOR TANDOORI LAMB CHOPS HAS BEEN PLACED");
break;
case 4: printf("\n\t\t YOUR ORDER FOR FISH BIRYANI HAS BEEN PLACED");
break;
default: printf("\n\t\t WRONG CHOICE");
}
break;
default: printf("\n\t\t WRONG CHOICE");
}
return 0;
}
Output :
NESTED SWITCH CASE/INNER SWITCH CASE DEMONSTRATION
A. VEG
B. NON VEG
ENTER YOUR CHOICE OF FOOD (A OR B) : A
VEG MENU
1. RAJASTANI THALI
2. BENGALI THALI
3. MAHARASTRIAN THALI
4. KASHMIRI THALI
CHOOSE YOUR FAVORITE THALI : 3
YOUR ORDER FOR MAHARASHTRIAN THALI HAS BEEN PLACED
======== End of Article========
Thanks and Regards
Prof. Shivling G. Swami.
Head of Computer Science and Engineering,
Sant Gajanan Maharaj College of Engineering, Mahagaon.
Whats app and Contact Number : 99 22 970 696
Comments
Post a Comment