switch case in c:
What is switch case in c ?
- It is used for checking the equality
- One switch statement can have multiple case statement.
- After each case statement break statement is necessary
- break statement is used for terminating the control from switch case in c.
- In switch statement default statement is used for displaying the invalid input.
Basic Syntax:
scanf("%d",x);
switch(x)
{
case 1: code which will be executed when x=1
break;
case 0: code which will be executed when x=0
break;
case 10: code which will be executed when x=10
break;
default : code if any input is invalid
}
Explanation:
In the above syntax the code in case 1 will be executed if the value of x = 1, if x=0 then case 0 will be executed.
So in general any thing written after case is compare with the input given by the user ad if it is equal then the code in that case is executed.
Note:
- (:) - should be present after every case statement.
- Code for switch statement should be written within curly brackets.
- break statement should be present after each case or multiple cases in case where multiple cases needs to be checked together.
- There is no semicolon after switch.
Uses of switch case in c:
Suppose you are making a calculator that can add, subtract, multiply, divide by accepting two numbers from user and the operation which will be executed depends on users choice.
But generally when we write a code for calculating all these mathematical operators we display all results at the same time this is where switch statement comes into role.
To avoid this we provide a user with a menu like this
Press 1 for addition
Press 2 for subtraction
Press 3 for multiply
Press 4 for divide
The number pressed by the user is compared by the switch statement with the case and the case whose number is equal to the input gets executed.
Example Code:
#include<stdio.h>
#include<conio.h>
void main()
{
float n1,n2,r;
int x;
int x;
clrscr();
printf("\n enter the 2 numbers");
scanf("%f%f ",&n1,&n2);
printf("\n Press 1 for addition ");
printf("\n Press 2 for subtraction ");
printf("\n Press 3 for multiplication ");
printf("\n Press 4 for divide ");
scanf("%d ",&x);
switch(x)
{
case 1: r=n1+n2;
printf("\n the addition is %f ",r);
break;
case 2: r=n1-n2;
printf("\n the subtraction is %f ",r);
break;
case 3: r=n1*n2;
printf("\n the multiplication is %f ",r);
break;
case 4: r=n1/n2;
printf("\n the division is %f ",r);
break;
default:
printf("\n invalid input");
}
getch();
}
Output:
enter the 2 numbers
12
3
Press 1 for addition
Press 2 for subtraction
Press 3 for multiplication
Press 4 for division
3
Explanation of the above code:
- In the above code we have declared 3 variables in data type float since calculation can be in decimals.
- The numbers entered by the user is stored in variables n1 and n2 respectively.
- Then the menu of choice of operator (add, subtract, multiply, divide) is displayed in front of the user.
- The number entered by the user is stored in the variable x which is than compared to the cases using the switch statement.
If you want to learn the topic of For loop click on the link given below
If you have any doubts please comment down below and subscribe to get notification whenever I upload a new post.
If you like the post please share it so that i can bring lot more content like this.
Comments
Post a comment
If you have any doubts pls let me know