 Hello everyone, I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So this is day nine of this C and C++ coding boot camp. And in today's session, I will be discussing about switch selection statement. And I will be giving you some demonstration, like how you can implement it actually in C programming. And the switch selection statement is basically used in different programming languages like C++, Java, Apex. So this is important to know like how this switch is working in C programming so that in different programming languages also you can leverage this. Okay, so I just welcome everybody, those who are watching this session live and I welcome to those as well who will be watching the recording. So I see like lots of people are seeing the recording as well because due to some time constraint, they are not able to make it to watch sessions live. So I'm happy like people are watching recordings as well. So this is about me. If you are for the first time on this channel, Sanjay Gupta Tech School, and if you don't know about Sanjay Gupta, so you can just go through with this slide. So you will be able to know about me. So on this YouTube channel, major content I delivered, but now I'm collaborating with other creators as well, other instructors as well. So they are also delivering some content. So we have one more bootcamp which is related to Appian. So one of my friend instructor is delivering that content, right? So we are doing a lot for you and be consistent because consistency is the key. So if you want to make your career in IT or like Salesforce ecosystem or any other technology that is related to IT, so you need to do consistent learning and do regular practice so that you can learn things and join this telegram group which is specifically for C and C++ coders. So if you see any kind of challenge, you can ask your query in this telegram group so that I or any other folk who is part of that group, they will be answering your query. So do follow Sanjay Gupta Tech School on YouTube, LinkedIn, Instagram and Telegram. And important links are available in the video's description. And nowadays like giveaway is going on my telegram channel. So if you follow that, so you can win exciting prizes through that giveaway event. So that is happening every week and different, different things I am sharing with the top 50 folks, those who are winning the quizzes. Okay, so with this note, let's jump on to the switch selection statement directly. So first I will be explaining you how, like how we can write this and what it is actually. And then we will be implementing a couple of programs in C. So basically a switch statement is used to select one option from many cases. So prior to switch, we also saw how we can implement if else condition. So in if else condition, basically what happens, you provide some condition if it is true. So true part is executed, if it is false, so else part will be executed. But in switch, you can have any number of options or cases and basis on the like selection, whatever option you are selecting, a basis on that particular case will be executed automatically. So if you want to implement manual driven programming where lots of options are available and you want to select one option. So for that, you can do programming with the help of switch. So sometimes switch is better than conditional statements if you have many choices. So for example, if you talk on mobile customer case center, so they ask lots of options like for one, for this press one, for this press two, for this press three. So they have lots of options. So these kind of problems we can implement with the help of switch in C programming. Then in ATM or menu of any hostel or restaurant, phone, a menu. So if you want to configure them like basis on the choices. So in the back end, if you implement any programming language, so for that, you can implement switch statement properly. Okay, so this is the syntax of switch. So we need to understand the syntax properly if you want to implement it in C programming. So basically you need to write a keyword that is switch. Then inside parenthesis, you will be writing integer expression, right? So it should be always integer expression. Float is not supported here. Then that integer expression will be matched with constant one, constant two, and so on. So if that integer expression matches with any of the constant, so that particular case will be executed. And you can see in each case, we have the statements. So whatever statements you will be writing, those will be executed. And after each case, you need to write a break keyword. So if you don't write break keyword, so whatever case matches, so including that case, all the followed case will also be executed. So if you want to execute only one case that is matching with that integer expression, so after each case, you need to write a break keyword so that switch will stop there only. And at the bottom, you can see we have default keyword. So if your integer expression is not matching with any of the case, so in that case, default block will be executed. And in default, you can write any number of statements and those will be executed automatically. So this is the syntax that we are going to implement. Before implementing real-time examples, let's take a look on this. So it is the explanation of the syntax that we saw earlier. So basically in that syntax, we have total four keywords. So first is switch, second is case, third is default and fourth is break. So these four are keywords that we used in previous syntax. So if we go back, so here you can see in this syntax, we have switch, case, break and default. So these are basically keywords. Then always use integer or character constants with case keyword. So if I go back, so here we need to define constants, constant one, constant two. So these can be integer or character. So basically float or real values doesn't work with switch or you can say switch doesn't support those values if we talk about constant or expressions, right? But here when you are writing statements, so in these statement calculation, you can use float. So Mamta is asking, can we use string instead of integer? So no, you can use only two things. One is integer or another is character. Character means the character which is enclosed in single quotes. Strings are basically enclosed in double quotes so you cannot use them. Then break, I already explained break is used to terminate execution of the switch statement. Then default is executed when integer expression does not match with any case constant. So this also I already explained with the syntax and break and default both are option, both are optional in switch statement, right? If you don't write break and default, so your syntax will be appropriate. It won't be giving you any error. But if you don't use break, so there may be chances, unnecessary cases are executing. And if you don't write default and if any of the cases not matching, so nothing will happen, right? So that's why if you write these two, so that will be helpful. So now, we discussed about the syntax now. I'm going to implement first program for you. So we need to write a program to implement simple calculator, right? So I will be giving three, four options like addition, subtraction, multiplication, division and from those options, anyone will be executed, right? So I'm going to implement this code here. So I'm removing extra statements. Okay, so now here I will be implementing the code. So first of all, I am creating some variables. So num1, num2, then a variable result and then ch for choice. So here we need to write printf. So I will be writing printf. So here I'm writing one for addition. Then one more printf. So here I'm writing two for subtraction. So I'm giving the choices. So through printf, all these choices will show up on UI. Three for multiplication and four is for division, right? So we are just creating simple calculation related stuff. So if you have two values, you can perform addition, subtraction, multiplication or division and that too, basis on your choice. So now what I'm going to do, here I'm going to write one more printf which will show enter your choice. So whatever choice you want to perform that you need to enter. So here I'm writing scanf, percent d and then m percent ch. So whatever choice you are writing, like one, two, three, four or any other value other than one, two, three, four. So that will be stored in this ch variable. Now after that, what we need to do, we need to receive two values as well from the user. So you will be writing enter two numbers because we showed all the choices. We received user choice, but we don't have values on which we will be applying the calculation. So for that, what we need to do, we need to receive two values. So enter two numbers and those two numbers, we are going to store in num one and in num two. So first number will be stored in num one and second number will be stored in num two, right? And we have percent d here. So first number will store into num one. Second number will go to num two and percent d because all the variables are of type integer. So this way we need to show the choices. We are reading the choice. We are reading the numbers on which we need to apply the logic. Now here we need to apply switch and inside switch we are going to pass ch. So this ch will be your integer expression. ch will be treated as integer expression and here we are going to implement the case. So case one, one is basically a constant value. So ch will be receiving either one or two or three or four. So if value of ch is matching with case one, right? So what we need to do? We need to perform addition operation. So I can write result equals to num one plus num two. Okay, and then after calculation I'm going to show the result. So sum equals to percent d and then result. So this way this sum equals to will display as is. Percent d will be replaced with the value of this result variable, right? And if you want to terminate your case execution. So after completion of after writing all the statements you can just write a break keyword so that your switch will be terminated, right? So this way I just implemented a case for you. Now we need to implement total four cases. One for addition, one for subtraction, one for multiplication and one for division. So what I'm going to do? I'm just going to copy and paste it. So it will be case two. And here I'm writing num one minus num two. So subtraction will be calculated. So here I'm writing subtraction, right? So this way case two is implemented. Again, I just pasted it. So I'm going to implement case three. So case three is for multiplication. So here you can see num one multiplied num two. So this way multiplication is calculated. So here I can write multiplication. So multiplication will show up as a result. And we need to write case four. So case four is for division. So num one upon num two. And here I need to write division, right? So this way total four cases I have implemented. So if I go above, so first we created all the variables. Then we showed all the choices. We are going to receive the choice. Then we are going to receive two numbers on which we are going to apply the operations. Then we implemented switch and different cases. Now last is default. If any of the case is not matching, so you can write default. And here you can write you have entered wrong choice. Okay, so this way, this switch is implemented. Now we just need to close this main. So main is already closed. So this way all the statements are written. So I hope you understood how I implemented this code with the help of switch. Now after implementation, you just need to click on run so that it will start its execution. So here you can see at the bottom results are available. So it is showing one for addition, two for subtraction, three for multiplication, four for division. So I'm entering one as choice. Now it is asking for two numbers. I'm entering 10 and 20. And you can see it is showing sum equals to 30. So this way whatever code I implemented, it is working fine for first case. Now I'm going to run it again so that I can show you subtraction. So this time I'm going to enter choice as two and numbers I'm entering as 10 and five. And you will see subtraction equals to five. So basis on the choice, it is showing the result. Okay, so one more time I'm going to run this code and this time I'm going to enter choice as five. And it is asking two numbers, but it will show you have entered wrong choice because number five is not available. We have choices from one to four. So this way, like if you have different options and you want to apply switch, so this can be applied in this way. So those who are watching live, if you have any doubt, you can ask that in the chat. So I would be happy to answer your query. So meanwhile, if you have any doubt, you are writing in the chat. So I'm going to show you another problem which is write a program to read two numbers and an arithmetic operator and perform the specific task using switch. So what we need to do, instead of showing these as an option, so what I'm going to do, I'm commenting these, right? And here what we can do, we can read arithmetic operator. So enter arithmetic operator and operators are of type character. So here I'm using care and percent C. So whatever arithmetic character I will be entering, it will go into CH. Two numbers will go into num one, num two and here we are applying CH. So this time what I need to do, I need to write it like this. In single quotes, I will be writing plus. So if you have character entered and that character you want to use as a switch option. So CH we are using as switch in these expressions. So basically it will be checked like this. So plus, minus, then we have multiplication sign. Then we have division sign, right? So this way you will be modifying this. And here you can write you have entered wrong operator. So rest of the code will remain same. A few things I have changed. So I hope you have noticed whatever I have changed. So one more time I'm going to show you what I changed. So here I just created two types of variables integer and character. Then here I just commented all these four lines and then the sprint f and scan f will be receiving an arithmetic operator. It can be plus, minus, multiply or divide. If you want to include modulus, so in that case you need to implement one more case here for modulus, right? So it depends on how much options you want to provide to your user, okay? So this way this code is implemented. So most of the logic is same. I just modified it so that we can receive an operator and basis on that it starts its execution. So again I'm clicking on run. So you can see it is asking for arithmetic operator. So I'm entering plus. Now it is asking for two numbers. So I entered 10 and 20 and you can see it is showing some equals to 30. So this way also your code is working fine. So this way I hope you understood how we can use integer as well as character as constant and as the switch parameter. So we can use either integer or we can use characters for the calculation, okay? So I hope with this way you are able to understand the logic whatever I have implemented here. Now, so two examples I demonstrated in front of you. So whenever you get time, so you can also implement these examples at your own so that you will become expert in this topic. And we have one more problem that says write a program to read a number and perform one of the following operation. So we need to check whether entered number is even or odd or we need to check whether entered number is positive negative or zero. So now what we need to do while implementing switch, we need to implement if else conditions as well. So everybody please understand whenever you are trying to implement switch. So inside case you can implement conditional statements. Those are if else or nested if or else if letter, right? So any without any problem, you can implement those features inside switch. So now I'm going to show you how we can implement solution for this problem, okay? So I hope everybody gone through this problem which is available in front of you. So I'm going to solve this problem number three. First and second problem I already solved for you. So coming here, I'm removing all the code, okay? So we need to read a number. So I'm creating a variable num. So only one number is required for even odd or for positive negative zero. So we need only one variable and one variable we need for choice. Now I'm going to show different choices. One for check even or odd. So even or odd both collectively are one option. So don't think even or odd, they are two options. So this is one option. Either number will be even or it will be odd, right? But it is one option. Then we have another option which will be checking positive negative or zero. Then you can ask enter your choice and then scan of percent D and ampersand CH. So basically we are showing two options. Either you want to check whether a number is even or odd or you want to check whether a number is positive negative or zero. So there are two options. And here I need to write two because it is second option and user will be entering either one or two or any other number. So that choice will be available into the CH variable. Now we need to receive that number as well. So I'm going to receive a number on which we need to apply these conditions. Okay, so something is wrong here. Yeah, now it is correct. So with this printf scanf, I will be able to enter a number and that number will be stored in this num variable, right? So this way I hope till now, whatever I return, you are able to understand. So these are two printers which will show these two options as is. Then this printf scanf will receive the choice and with the help of this enter a number, we'll be able to enter a number and that number will store into num. Now after this, we need to apply a switch and see it carefully. I will be implementing if else inside switch. So this is important to understand how we can merge two features together. So switch and here I'm writing CH. Now inside this switch, I will be implementing the cases. So case one says even or odd. So here I'm writing if num modulus two double equals to zero. If it is true, so here I'm going to display. Number is even else. I'm going to display number is odd. So this way, if you focus on this, so from here switch is starting. So this is switch block and this is case one and inside case one, I implemented even odd logic with the help of if else. So it is possible and it is totally correct. Now after this, I can apply a break. Then I'm going to write case two. So case two will be checking for positive, negative and zero and I showed you two ways to check positive, negative and zero. So we can check it through else if ladder. We can check it through nested if. So here you need to use any one from them. You don't need to write both the ways. You just need to write only one way. So I'm writing if num is greater than zero. So I can write number is positive. Else if number is less than zero. So here I can write number is negative. So I'm using else if letter. If you want to use nested if that is also fine. And in last, I can write print f number is zero. Write and then break. And then you can write default and inside default you can write. You have entered wrong choice. Okay, so this way the whole code is implemented. So first of all, we need to create variables. Then we are going to show the choices. Then we are going to read the choice. Then we are going to read the number on which we need to apply operations. Then switch case one, then switch case two and then default and both the cases are having if else logic implemented, right? So this way we can implement it completely. Now it would be interesting to show to see how it will be executing in actual. So I'm clicking on run. I will be running both the cases. So here you can see it is asking for choice. So I'm entering one. Then it is asking a number. So I'm entering 39 and I'm entering. I'm pressing enter key. So it is showing number is odd. So as per our choice, even our odd is checking. If I run this code again, this time I'm entering two and number I'm entering as minus four, five. So you can see it is showing number is negative. So as per the choice, particular case is executing and particular case is also checking whether number is like even or odd or number is positive, negative or zero. So this way in actual in C programming you can implement switch case statement. So I hope with the help of this demonstration it is more of relatable and fruitful for you to understand how we can actually implement switch case in C programming. And in other programming languages like in C++ it is same in Java also it is same but little bit advancement is there. In Apex also little bit advancement is there. So now you got the basic idea like how we can implement switch. So whenever you are going to learn any other programming language. So you will be having an idea like how switch is actually implemented and basis on that in that programming language whatever way it is being used. So you just need to focus on the syntax. Overall working style will be same, right? So as per the syntax you need to implement switch in that particular programming language. So the idea is like if you are a BTEC beginner you should know how switch is implemented in C programming. If you are non-tech folk and you want to know like what is switch. So I hope with this session you are able to understand the working of switch statement properly, right? So if you have any doubt you can ask in the chat because this is it what I prepared for you for today. And in upcoming sessions so next session will be happening after two days because we have new bootcamp in place. So every week I will be having two to three sessions for C and C++ and two sessions will be having for Appian. So this way we'll try to balance it out. And if you are able to join live so that is good enough. If you are not able to so you can just watch the recording so that you can strengthen your coding skills. And I'm delivering everything totally free of cost. So you just need to watch the sessions understand the concept whatever examples I'm implementing you can implement that at your own. And I will be updating the exercise sheet. So from there also you can pick some of the examples and you can practice, right? So this is it for today. If you have any question you can post in the chat otherwise I take your leave and see you in the next session I will notify in our telegram channel like when next session will be happening. So you can join accordingly. Okay, so I don't see any question. I think it was very easy topic and thank you Mamta for appreciating my efforts. So this actually boost my energy so your words of appreciations are very much important for me. So do provide some reviews or feedback whatever I'm doing for the community, right? So thank you so much for joining this session. See you in the next session and next topic will be loop so it will be very much important. So don't miss those sessions because looping is very much important if you are a beginner in the programming or if you want to learn any other programming language. So you need to understand how actually loop works. So I will be discussing that in our upcoming session. So thank you so much for joining today's session. See you in the next session. Thank you, bye.