Control
Statements
· switch statement:
When to choose one alternative out of many switch statements is used. It is also known as
multiway decision-making statement or conditional statement.
Syntax:
switch(variable/expression)
{
case value
Block of statements;
Break;
case value
2:
Block of statements;
Break;
.
.
case value N:
Block of statements;
Break;
default:
Block of statements;
Break;
}
Statement X;
§ Switch statement followed by the variable or expression in the round bracket.
§ The variable can be integer or character only.
§ Switch matches the value of this variable or expression with case value1 to case value N.
§ If any match is found, it executes the block of statements associated with the case value.
§ If any match is not found, it executes the block of default statement.
§ Case label or value must unique, no duplicate cases are allowed in switch.
§ Case label end with colon (:)
§ The case block end with break statement.
§ Break statement is an immediate exit from switch.
Program
no-13
Program to print weekday based on given number
/* Prog no-13 Program
to print weekday based on given number */
#include <stdio.h>
#include <conio.h>
void main()
{
int day;
clrscr();
printf("\n\nEnter number of day from 1
to 7 : " );
scanf("%d",&day);
switch(day)
{
case
1:
printf("\n
Monday");
break;
case
2:
printf("\n
Tuesday");
break;
case
3:
printf("\n
Wednesday");
break;
case
4:
printf("\n
Thursday");
break;
case
5:
printf("\n
Friday");
break;
case
6:
printf("\n
Saturday");
break;
case
7:
printf("\n
Sunday");
break;
default:
printf("\n
invalid number");
break;
}
getch();
}
Output:
0 Comments