Hey guys, this is me Padma from Programmees and welcome back to this series on C programming. In this video, we will learn about the switch statement and how to use it to make decision making programs in C programming. Here we will also create a calculator using switch statement.
So let's get started. Before we learn about switch statement, let's consider a scenario. We are given a number between 1 to 7 and based on the number we have to print a number. the day of a week for example if the number is one then the day is sunday and if the number is two then the day is monday and so on now we can use the if else statement for this however for every number there should be a separate condition this will make our program look messy and difficult to understand for such type of problems where we have to choose from multiple options a better approach will be switch statement let's start with the syntax of the switch statement first the switch statement starts with the switch keyword followed by the variable or expression inside the parenthesis inside the switch statement we can create multiple cases and each cases will have its own value now the value of variable or expression is compared with the value of individual cases if the result of the variable or expression is equals to value 1 then the body of case 1 is executed similarly if the result is value 2 then the body of case 2 is executed and so on However, if the result does not match any case, then the body of default is executed.
Now let's get back to our original problem of finding the days of the week. On the screen, you can see the basic C program. Now I'll create a variable number, int number.
then i'll ask the user to enter number between one to seven so i'll use printf statement and i'll print the message like enter the number between one to seven and store the value to the number variable so scanf side quotation percent d comma ampersand name of the variable that is number Then I'll create the switch statement that will print the day of the week. As we have already discussed we'll use switch keyword and then inside parenthesis we'll put variable or expression in this case we have variable number so I'll put number here then a curly braces inside this curly braces we'll add cases so when the case is one I'll print Sunday and I'll use break statement after that. I'll talk about this break statement later on the video.
So let's continue. When the case is 2, I'll print Monday. So Monday and break statement.
So what I will do is I'll copy this and paste this five more times and at the end I'll put default and I'll print invalid number. Invalid number. So invalid number.
Now the case 1 and case 2 is already done. So I'll edit from case 3. and case 3 is tuesday similarly case 4 is wednesday and case 5 is thursday so we'll write thursday here then case 6 is friday and finally case 7 is Saturday so let's make this as capital so here I have included cases from case 1 to case 7 and if the user input 1 then this part of code is executed and Sunday will be printed on the screen and if the user input 2 then this part of code will be executed and Monday will be printed on the screen. However, if the user input any number except 1 to 7, then the code inside default will be executed and invalid number is printed on the screen.
Now let me run this program. I'll enter 5. Now the input value 5 match this case case number five and thursday is printed on the screen now let me run this code again this time i'll enter six as expected we get friday as output now once again i'll run this code and this time i'll enter the number that does not lies between one to seven so i'll enter nine And as you can see, I got the output invalid number, which is present inside the default. Here, the default case is optional.
We can remove this if we are sure that the input value matches one of the cases. Go ahead and remove the default value and try the code. Here, if you have noticed, I have used break statement inside individual cases. Here, this break statement exit the switch statement once the matching case is executed. If we don't use the break statement, all the cases after the matching case will be executed now let me show you I'll remove this break statement from the program so let's remove this now I have removed the break statement from each cases now I'll run this code and I'll enter 5 and it matches the case number 5 and Thursday is printed at first then all the cases after that is also executed so we get the output thursday friday saturday invalid number this is why it is important to use break after every cases by the way if you are watching this there is a good chance you want to improve your skills in C programming.
Lucky for you, we have a mobile app that provides a well structured C programming course with certification at the end, and you can use the app alongside the video to practice on the built in compiler. Our course is free, so download now by scanning this QR code or click the link in the video description. We learned that using break after every case is important. However, sometimes there might be situations where we want to execute multiple cases together.
In such situation, we can omit break statement. Let's see an example. As you can see, I have some new code on the compiler. Here we want to check if the day is weekday or a weekend.
So all the cases from 2 to 6 will print weekday and the case 1 and case 7 will print weekend and then the default will be an invalid number. Now let me run this code. I'll enter foyer. As expected we get weekday.
and i'll run this again this time i'll enter one and you can see weekend is printed on the screen as you can see we can omit the break statement as per our need next we will use the switch case statement to create a simple calculator okay guys we need your support to keep this type of content free for all users youtube really likes engagement on the video so leave a comment below press that like button hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy Enjoy these courses. Now that we know the working of the switch statement, let's create a simple calculator using it. Here I have copy pasted the code from our GitHub repository so that you don't have to see me typing the whole thing.
Now let's see what's happening in this code. Here we have created a character variable, we then ask the user to choose an operator and store the input in the operator variable. Then we ask the user to provide two input values and store them in num1 and num2 variable. Then the result variable will store the value after calculation.
You can see we have used operator variable inside the switch statement and each case statement uses an operator as its value. And based on this operator, the corresponding operation is performed inside the case. Now let me run this code. Here I'll enter plus and I'll enter first number 8 and then second number 12. You can see I get 20 as output. What happens here is I provide plus as operator and this plus operator matches with this case.
Hence the addition operation inside the case is executed and the final result is 8 plus 12 that is 20. The break statement then terminates the switch statement. Now let me run this code again. This time I'll enter asterisk and the first number 9 and the second number 5. Since I entered asterisk the operator match with this case. So the multiplication is performed between number 9 and 5 so we get 45 as our output.
In this way we can also perform subtraction and division. You can try it yourself. Now to revise what we have learned, here is a programming task for you.
Use the switch statement to create a program that will find the month based on the number input. Here take the input from 1 to 12 and print the corresponding month based on the input value. If the number is 1, print January, if number is 2, print February and number is 3, then print March and so on.
You can find the answer to this question in our GitHub repository. Also, if you want to revise today's learning, all the programs will be present inside the repository. The link is in the video description. Now that we have reached the end of this video, it's time for programming quiz.
Which of the cases is executed in the following code? Comment your answer below. See you in the next video.
Happy programming.