 Hello everyone, Myself Prasheesha working as an assistant professor in WIT, welcome you to this particular video lecture where we are going to discuss about the last type of decision control statements used in programming language C that is nothing but switch case. This is in the continuation with the previous four video lectures where we are discussing about how to use different types of decision control statements inside programming language C. Learning outcome at the end of this session students will be able to write C code using switch case statement ok. So, recall that in the previous video lecture we had discussed about if-else platter and we said that the if-else platter is used for implementing multivator decision making statements. The first job back given by if-else platter is the problem of confusion. So there is a problem of also whenever if we use nested if or if-else platter there is a problem of dangling else inside the program. So the programmer has to take care that it the programmer has to take care that the corresponding else and curly parenthesis have been enclosed properly inside the program. Also if you are implementing more number of alternatives inside the program there is a problem of complexity. So as the number of alternatives go on increasing the complexity of the program increases. So we have one more way of implementing multivator decisions inside C. So what is the switch case statements? It is a easier way of implementing multivator decisions also it is logically similar to if-else platter. So based on the given algorithm or based on the given problem statement you can always decide between which type of multivator decision making you want to use. So either you can go for switch case or you can go for using if-else. What is the syntax of writing switch case inside C? So first of all we have the keyword switch followed by some expression and then we have another keyword that is nothing but case. So case is followed by some values then the corresponding block of statements belonging to first case followed by break. So break is also one of the keyword used in C. So what is the use of using break inside any control structure? Basically break will make the program to come out of the corresponding control structure. So that might be if-else that might be loops or that might be switch case okay. So in this fashion you can include more number of cases along with the corresponding block of statements and break. Along with this you can also use default keyword. So what is this default block? Default is the block which will be executed if the expression does not match with any of the values okay. So how the switch case works? First of all this expression will be taken out and its corresponding output will be considered then it will go for checking out whether it matches with the value one. If it matches with the value one then block one statement will be executed. So every time this expression is compared with all of the values okay. So whenever the match is found the corresponding block of statements will be executed. If this expression does not match with any of the values then default will be executed in that case okay. So this is how what is the syntax of writing switch case inside C okay. Let us again try to understand how exactly switch case works in C okay. So we have some expression okay and that expressions value will be compared with the first case value. If it both of them are matching in that case block one statements will be executed and then if we are putting break for each of the block then rest of the all blocks will be skipped from the part of execution and the program will continue to execute statements below switch case structure. Now if this case like the match is not found then again expression will be compared with the case two value okay. If both of them are matching or the match is found then block two statements will be executed okay. If the expression does not match with the third case that is nothing if the expression does not match with any of the cases that have been put before default block then default block will be executed and then program will continue to execute statements below switch case okay. So when we are using the switch case inside C there are certain things which you need to understand which you need to remember. The first is the all case levels that we are using inside the program all of them should be unique one and all of them should end with a colon. This is how what is the syntax of writing switch case. Also all of the case levels should have the type as integral that means either those can be integer or those can be character. For example see the on the left hand side some switch examples expression examples have been given that means you can use a variable or you can use you can use a variable or you can use an expression or you can use directly an integer value. What is the condition? All of these expression must evaluate to an integer value. On the right hand side the case examples have been here here the case value is 1 here both in both of these examples character have been used inside that okay but it notice how the character has been used it has been used inside single codes. So expression must you mean an integer value so do not use any expression whose value comes out to be a floating point value or a string or a character okay and case levels and expression should be of same data type. There are more things to remember all the case levels may not be order you can put them in any random order you can also nest the switch case control structure. Switch case follows a fall through mechanism is what if we are not using break inside all the cases then even though the match has been found still the program will continue to execute rest of the cases as well. The default is optional time to reflect go through the given program very carefully pause the video for a moment and try to predict the output of the corresponding program. I hope that you have got the corresponding output so what is this? This is a character is used and here integer has been used so whenever you use any character inside case level basically its ASCII value will be utilized for comparison. So we have used X as the expression so basically X expressions value is 97 so 97 value will be compared with the ASCII value of A the ASCII value of A is nothing but 97 so here the case is matching also if you go for the second case here as well we are taking the value as 97 so we have got the problem that there are duplicate cases values have been used. So when we were discussing about the things about the switch case we had said that all case levels should be unique. So the program will not execute because there is a duplicate case error here. Now let us move ahead and let us try to write a program by using switch case using code blocks. So the given problem statement is to write a simple calculator using let us declare two variables on which we are going to perform certain operations. So these are the two numbers on which we are going to implement certain calculations okay. So we have declared two variables for reading two numbers from the user okay. So A and B will be used so as to give the input values and those will be stored inside A and B okay. So we have one more we have declared one more variable which is nothing but K and this K is nothing but the choice entered by the user. So user will choose out of the three different operations which operation has to be executed okay. So since we are using we are declaring two integer variables we are using percentage DRC format specified okay. So we will also ask the user to enter a choice between the three options given such as addition, subtraction, multiplication. And that value will be taken or it will be stored inside the variable case okay. So this is for user whichever value is entered by user that will be taken okay. So how to use? We use keyword switch followed by a K value okay. So this K value will be compared with the three different cases as you can see here case K will be compared with either 1, 2 or 3 okay. So let us try to build this program and let us run this okay. So I am entering two numbers 10 and 20 okay and I need to give out of the three different options I need to choose one. So maybe I will use a value as 1 okay. Now what do you see or what do you observe? The first case is executed that is nothing but 30 result is displayed and along with that the corresponding outputs of subtraction as well as multiplication as well as the default choice have been displayed. And why this happens? We said that this switch case follows a fall through mechanism means what? Whenever the match has been found and if we are not using break within the corresponding block then automatically rest of the cases will also be executed. Now how to correct this program? In this case we must use the keyword break with each of the blocks okay. So then we have break here okay here as well as well as with the default case okay. Now let us again try to run the program once we have modified it okay. So again I will give two different numbers 10 and 20 and I will choose the options now as subtraction. Now what is the answer? Minus tail as the subtraction. So program is showing you the correct output and rest of the cases are not being displayed. That means miss what whenever the case will be found automatically break will make the program to come out of the corresponding switch case structure okay. So in this fashion you can use switch case within the program so as to go for implementing multiple decisions. This particular video was created by using these references. You can always go for using multiple online resources available for more learning thank you.