Lecture 12 – Decisions (switch)

switch

There are situations where we do not need to check the range of values, but we need to check some specific values. For example, when you call a cellular company’s helpline; you get an IVR (Interactive Voice Response) menu, from which you can select any one option.

Here I would like to create a very primitive IVR menu. That is the program would ask for a number from the user, and then it will tell you the name of the day associated to it.

Consider a situation in which we implement this solution using if or if-else-if then the solution would be not efficient, because it would need to check the conditions repeatedly.

Number
Name of the Day
1
Monday
2
Tuesday
3
Wednesday
4
Thursday
5
Friday
6
Saturday
7
Sunday

Syntax

switch( variable)

{

case <value>:

                                …….

                                break;

case <value>:

                                …….

                                break;

default:

                                …….

}

Switch, case, break and default are keywords. They cannot be replaced or omitted. Whereas the value can be changed depending upon the type of functionality required.

Please note that the value must be

·         a whole number  or a character

·         not a fraction or a range of values.

Let’s code it




Output

Please enter day number: 1

Monday

 
#include <stdio.h>




#include <conio.h>

void main()

{

int day;




Output

Please enter day number: 2

Tuesday

 

 


clrscr();                                                                                                

 

printf(“\nPlease enter day number: ”);




Output

Please enter day number: 3

Wednesday

 

scanf(“%d”, &day);

 

switch(day)

{




Output

Please enter day number: 4

Thursday

 

case 1:

                                printf(“\nMonday”);

                                break;

case 2:

                                printf(“\nTuesday”);




Output

Please enter day number: 5

Friday

 

                                break;

case 3:

                                printf(“\nWednesday”);

                                break;




Output

Please enter day number: 6

Saturday

 

case 4:

                                printf(“\nThursday”);

                                break;

case 5:




Output

Please enter day number: 7

Sunday

 

                                printf(“\nFriday”);

                                break;

case 6:

                                printf(“\nSaturday”);




Output

Please enter day number: 9

Wrong input – Valid values are from 1-> 7

 

                                break;

case 7:

                                printf(“\nSunday”);

                                break;

default:

                                printf(“\nWrong input – Valid values are from 1->7 \a”);

}

getch();

}

Example 22 - Use of switch statement with break and default

 


 


 

Comments

Popular posts from this blog

Lecture 17 – Functions (continued)

Lecture 6 – Operators in C (continued)

Lecture 10 – Decisions (if-else-if)