switch case statement in C, sample programs, programs to try
Switch Statement in C
There is one potential problem with the if–else statement: the complexity of the program increases as the number of alternative paths grows.
Using multiple if–else constructs can make a program difficult to read and understand, and sometimes it may even confuse the developer who wrote it.
Solution
The solution to this problem is the switch statement.
Definition
Syntax
Explanation
-
The value of the expression may be:
-
Generated during program execution, or
-
Read as user input.
-
-
The expression can be of type int, char, or enum.
-
The case whose value matches the expression is selected and executed.
-
The default label is optional and runs when no case matches.
-
The position of the default block does not matter.
Rules for Switch Statement
-
An expression must always evaluate to a result.
-
Case labels must be constants and unique.
-
Each case label must end with a colon (
:). -
A break keyword should be present in each case.
-
There can be only one
defaultlabel. -
Multiple switch statements can be nested.
Summary
-
A switch is a decision-making construct in C.
-
It is used in programs where multiple decisions are involved.
-
A switch must contain an executable test expression.
-
Each case should include a break statement.
-
Case labels must be constant and unique.
-
The default case is optional.
-
Multiple switch statements can be nested within one another.
Note
-
All programs written using the switch-case statement can also be written using the if-else statement.
-
If you need to select among a large group of values, a switch statement usually runs faster than nested if statements.
-
The switch statement can only test for equality, while if can evaluate any Boolean expression.
-
Switch statements are commonly used in menu-based applications
int main( )
{
int day;
printf("\nEnter the number of the day:");
scanf("%d",&day);
switch(day)
{
case 1: printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
case 7:
printf("Saturday");
break;
default:
printf("Invalid choice");
}
return 0;
}
Menu driven program using switch.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
main()
{
int ch;
float r,b,h,arc,art;
system("clear");
printf("Menu\n");
printf("1.area of the circle\n");
printf("2.area of the triangle\n");
printf("3.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter radius\n");
scanf("%f",&r);
arc=3.14*r*r;
printf("Area of the circle=%f\n",arc);
break;
case 2:
printf("Enter base and height\n");
scanf("%f%f",&b,&h);
art=0.5*b*h;
printf("Area of the triangle=%f\n",art);
break;
case 3:
printf("Bye\n");
exit(0);
default:
printf("invalid option\n");
}
}
Reading grade and printing Good (o,a,b) or Bad (any other)
#include <stdio.h>
main()
{
char grade;
scanf("%c",&grade);
switch(g)
{
case 'o':
case 'a':
case 'b':printf("Good\n");
break;
default:
printf("Bad\n");
}
}
Program to find all roots of a quadratic equation using switch case
Programs to try
1.Print the month name corresponds to the month number and also print the number of days.Read month number.
2.Check whether the given number is odd or even
3.Read a character.If it is 'P' print Python.If it is 'J' print java.Print invalid character if it is any other character.
4.Read a character and Check for vowel.
5.Create a simple calculator( +,-,*,/).Read the operator and two numbers and do the operation based on the operation.
6.Write a menu driven program to compute area of a circle (option 1) or area of a triangle ( option 2) using switch statement.

Comments
Post a Comment