Java/J2EE and SOA (348 Blogs) Become a Certified Professional

Switch Case In C: Everything You Need To Know

Last updated on May 07,2020 16K Views

Switch Case In C: Everything You Need To Know

The switch case is a very important tool in C. It is a looping statement in programming. It helps us reduce the use of if/else when there are a  number of conditions. In This article we will understand Switch Case in C. Following are the pointers will be discussed in this article,

So let us get started then,

Switch Case In C

In a switch statement, we pass a variable holding a value in the statement. If the condition is matching to the value, the code under the condition is executed. The condition is represented by a keyword case, followed by the value which can be a character or an integer. After this, there is a colon. Each case has a break statement, that makes the control to leave the loop. If no matching case if found it executes the default statement.

Syntax:

</p>
switch (n)
{
case 1 : //execute if n=1
break;
case 2 : //execute if n=2
break;
case 3 : //execute if n=3
break;
default: // execute this code if none of the cases match
}
<p style="text-align: justify;">

This is the way in which a switch case is used.

Remember that the variable passed to the switch must hold a character or an integer value, it must not be complex but only simple value. 

Execution Of Switch case In C

If the value of n is 1 then the code under case 1 will be executed and it breaks from the loop using the break statement. Similarly, if the value is 2 then, it will first check for case 1 if it does not match the control will go to case 2. If there is a match it will be executed and followed by a break statement. If after checking all cases, no condition matched then the default condition is executed.  

The use of break is to prevent the other cases from being executed even after finding the match and executing the statements under it. If there is no break statement then, the statement under the case below it is also executed. Each case should have the break statement except the default. Break of default is optional.

The default case is optional but, it is a good practice to have a default case.

Example switch code:


#include <stdio.h>
int main()
{
int m=0;
printf("enter your choice:n 1.book a car:n 2.book a bus:n 3.book a flight:n");
//accepting user choice
scanf(" %d",&m);
//passing choice in switch case
switch (m)
{
case 1:
printf("Car is booked");
break;
case 2:
printf("Bus is booked");
break;
case 3:
printf("Flight is booked");
break;
default:
printf("Invalid input");
break;
}
return 0;
}

Output:

If a matching case is found.

Feature Image - Switch Case In C - Edureka

If a matching case is not found:

Feature Image - Switch Case In C - Edureka

The program above is a basic demonstration of a switch case in C. We take input from the user based on the choices he has. The user choice is sent in the switch statement. The switch statement checks if there is a matching case. In the above code, the user first selects 3, so the statements under that block are executed, that is “booked a flight”. If the user enters a value beyond the range, then the default case is executed. In the above example, the user enters 7 and the statement under default is executed. 

With this we come to the end of this article on ‘Switch Case In C’. I hope you found this informative and helpful, stay tuned for more tutorials on similar topics.You may also checkout our training program to get in-depth knowledge on jQuery along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.

Got a question for us? Mention them in the comments section of  this article and we will get back to you.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 11th May,2024

11th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 1st June,2024

1st June

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Switch Case In C: Everything You Need To Know

edureka.co