Skip to main content

switch case in c

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;
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
the multiplication is 36

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

Popular posts from this blog

C Programming Basics

C PROGRAMMING BASICS:
1. Introduction to C language: C language was invented in Bell Labs by Dennis Ritchie between 1972-73. C language is a case sensitive language i.e the input given to it varies according to upper and lower case letters. It is executed using binary form data such as (0,1). By using binary data the speed of executing a code increases.
1.1 Header Files:

Header files are those files which contain some predefined functions in it which we are going to use in our code. Every header file is accessed by appending an extension ".h".
Example: #include<stdio.h> ↓                   ⇡
         Tells computer to                  header file with 
         include the header                 extension .h
         file          
There are many header files for different functions such as the following: <conio.h><math.h><string.h>Etc.

1.2 Starting of code and basic Syntax:

void main() or int main()

This tells the computer from where the code is starting. It is wri…

How to start programming for beginners

HOW TO START PROGRAMMING:
1. What is Programming?
Programming is a way of communicating with the computer and giving it set of instructions to do the work as per our needs.
Example: While calculating something with a calculator what has to be done according to the input given to it is regulated by a program.Programming can also be used to build a software to determine whether a particular number is prime or not.2. Can anyone learn Programming ?The answer to this question is simple YES anyone with basic knowledge of maths and some common sense can master programming. The most amazing example of this is Sundar Pichai the CEO of Google he Studied metallurgical engineering at IIT Kharagpur. So if you love it you can do it. 3. Why Should I learn Programming?If you have that passion to solve a problem and enjoy twisting your mind then this is for you else you should leave programming right now.The other important Factor is Money if you become a master in programming then there is no limit on how…

Basic C Programming Questions

Basic Coding Questions:Exercise 1:Q1. Write a programme to calculate area and perimeter of rectangle by accepting its length and breadth.
Ans:
#include<stdio.h> #include<conio.h> void main() { float a,p,l,b; clrscr(); printf("\n enter the length and breadth of rectangle"); scanf("%f%f",&l,&b); a=l*b; p=2*(l+b); printf("\n the area of rectangle is %f and the perimeter is %f",a,p); getch(); }

The above image code is written in code blocks
Output:
enter the length and breadth of rectangle 20(press enter) 10(press enter) the area of rectangle is 200 and the perimeter is 60


Explanation of the above code: Declaring of variables in data type float since length and breadth cannot always be integers.Taking input of length and breadth together by using scanf function.Substituting the values of length and breadth stored in l and b to find out area and perimeter.Displaying the result.
Q2. Write a programme to calculate average of 5 numbers by taking input from user.
Ans: