Switch case



A switch case statement is a multi-way decision statement that is a simplified version of an if-else block that evaluates only one variable. Switch statement is a control statement that allows us to choose only one choice among that many given choices.


Syntax of Switch statement


switch(variable)
{
case value1;
Statement block1;
break;
case value2;
statement block 2;
break;
.....................
case value n;
statement block n;
break;
default:
statement block D;
break;
}
statement x;



write a program to enter a number from 1-7 and display the corresponding day of the week using switch case statement


#include<stdio.h>
#include<conio.h>
void main()
{
int day
clrscr();
printf("\n Enter any number from 1 to 7: ");
scanf("%d", &day);
switch(day)
{
case 1: printf("\n SUNDAY");
break;
case 2: printf("\n MONDAY");
break;
case 3: printf("\n TUESDAY");
break;
case 4: printf("\n WEDNESDAY");
break;
case 5: printf("\n THURSDAY");
break;
case 6: printf("\n FRIDAY");
break;
case 7: printf("\n SATURDAY");
break;
default: printf(\n Wrong Number");
}
getch();
}


OUTPUT


Enter any number from 1 to 7: 5
THURSDAY


Comments