switch-case statement

Shibaji Paul
A free video tutorial from Shibaji Paul
Programming Instructor with 16+ years of experience
4.4 instructor rating • 7 courses • 25,199 students

Lecture description

Learn how you can use switch case statement as an alternative to if-else also you will understand when switch case will be good to use.

Learn more from the full course

C++ programming step-by-step: From Beginner to Advanced

Start with basics of C++ (v11 and v14) then master File I/O, Object Oriented Programming or OOP, STL, Functors, Lambda

27:29:05 of on-demand video • Updated June 2020

  • On completing the course you will have firm grip on C++ language. You will understand the object oriented programming OOPs concept in depth.
  • You will understand the basic programming structures like branching and looping.
  • You will understand details about pointers and dynamic memory allocation along with memory management techniques.
  • You will understand all about functions, parameter passing by value and by reference.
  • All about user defined types using struct and class. Constructor function, method overloading and dynamic polymorphism.
  • You will learn operator overloading, friend functions.
  • In depth understanding of Inheritance and dynamic polymorphism, how runtime polymorphism helps to write more flexible and dynamic code.
English [Auto] Hello will come back. So we have understood if LCF now in this tutorial I'm going to introduce switch case statement. Now the switch statement can be alternative to ill's if for some cases I have a program there that uses if LCF in order to do something as you can see the program is really simple is reading an integer value from the keyboard in a variable and then it is comparing the content of the variable in with three constants one two and three if the content of an is 1 It's renting back or any anyone into the console. If it is to be splinting back T.W. or two into the console. Otherwise if it is three splitting three into the console for all other values of in it's actually printing invalid number for us. I think it's not going to be hard for you to understand this if and if now. OK. Now we can actually really Plus this using a switch case statement now which gets a statement it's an alternative to insist as I say it some people prefer to write a switch case. Some people prefer to write it feel safe. Now it's entirely up to you. What you are going to choose. You can go with the feel safe or you can write a switch case. Now switch case can be alternative to that FLC for some special situation. It's not going to be a good alternative for all the situation when you are going to rewrite. You feel safe with which case you'll understand when you should. Exactly you switch case and when you should stick to the IF. ELSE IF. Go ahead and replace this philosophy with switch gears. That's the best way to understand which case. OK I already have written the code. I'll just comment. This difficulty portion and I'll paste the alternative switch case here as it can see this is the switch gives alternative to the IF. ELSE IF that earlier we had here. OK so let me explain this. First switch is a keyword OK and within the parent Assis you need to write a variable for which you are going to do the comparison. We want to test whether the value in is 1 or 2 or 3. So any to compare the content of and so in goes there. Can the parent assess Please note one point that you can only switch integer or character type variables. It cannot switch real numbers. That means floating point. No it cannot do that. Double not you cannot do that only integers and characters. OK so you need to write the variable here within the Baratz then within the curly braces here for the switch you need to write the cases now. Case 1 is the case when the content of N is one. That means if the value of then is 1 then whatever you're right there after the call of case one is going to be executed. If the value of an is to whatever you write there after the column of case 2 is going to be executed there must be a colon. OK and there is a gap between case and the constant. You can only write a constant here. You cannot write and add the variable here. That's another point to remember with the switch gears and there is another one important point. The order is not mandatory. That means you can write the cases in any order. I could have written this in this way. That's perfectly all right. Now if the value of N is 1 then this case 1 is going to be executed that means this is going to be printed. If the value of and is 2 this is going to be printed. If the value of history. This is going to be printed if anything other than this one two and three. Whatever you have written after default is going to be executed and you can have this default anywhere. You don't need to have that at the end. You can write it here at the beginning as well but in that case you need to give a break now what's the importance of break this break. Actually text the control of the OP to switch gears. If you don't give that prick the control it's going to follow through with the latter cases. Now let me explain. That said I do not have that break at all there. And what will happen if the value of it is three. In this case 3 is true then the control is going to execute this one is a statement and it's going to fall through. That means this is going to be executed. This will be executed as well so the output is going to be 3 1 and 2. Now you just don't need to give that break if you have a last statement there. As I did earlier if you remember now if you are having this default as the last statement then you just want to get this break OK because that's the last one. After that it's going to come out of the switch eventually. But if you are having the other statements then you must give that break there. OK you must get a break here if you don't want that Faltu to happen. In some cases you may want that Faltu to happen as well and discuss that as well. So the break is going to take the control out of the switches. So if this is free OK this break is going to execute under control is going to come out of the switch. Otherwise it's going to execute this one as well. Let me show you that. OK. First of all I'll just execute with the break. Now see there it's executing right. OK. Say two and sprinting two. It's not going to print anything else. I run once more. I'll give say seven is going to print in tablet number for us. So that's working right now. I'm going to just leave. I'm going to just erase those breaks. I mean using the first two bricks now I'm going to give three that the output is going to be three and then one is going to fall through. OK then it's good to find two there. OK. So three one two and then it's going to find break is going to come out of the switch. So the output is going to be three. One two if I give three. You forgive if one is going to be one and two if I get two. It's going to be two only. OK I'll give three there. And here we go. OK. I'll give three as I say it is c that 3 1 2. That's the output. Now if I just remove this break then it's going to be 3 1 2 and in that number for us. Let me show you that as well. OK 3 2 1 3 1 to an invalid number for us. OK. So that's it. Now let me show you one more example where it's going to be useful time to remove the break. So if you want to check in this way they want to test arrange for the value say you want to test whether a given age is teenage or not. OK I'm just replacing this quickly. So in that case I would ask the user to input and H enter your age. OK. And then I'm going to put that in the variable age. Now we want to test whether it is 13 in between 30 and 19. OK. So if the age is teenage then the age is actually going to be greater than it was 13 and less than it was 19. So for all the ages 13 14 15 16 17 18 19. You are going to print. Yes it is a teen age. Otherwise we need to print not snotty teenage son now if you are interested of doing it using switch gears you need to test the range of values for age whether it falls within a range. Right. If you are into sort of doing this using Ifill's if that is really going to be easier because we can just use the logical and operate at their age there. I'm sorry David could have been 13 and age less than it was 19 then. We are going to print. Yes it is teenage registry of teenage else not just not teenage. So that's going to be easier. But we can still do that using switch case. Now please note that if we are interested or doing this in which case it's not going to be a little bit longer because you need to actually write all those cases starting from 13 to 19 guess 14. And just quickly copy so for all these cases the age is teenage so you need to write all these things. Now please notice if you want to test whether a particular value pool is between 1 and 100 or not. In that case you're going to write guess one case to K-Street in this way up to 100. That's going to be really tedious for you. So if you want to test a longer average larger range where there are bad falls within a large range of values then it will be better for you to use the and operator and use. If so as you can see we can still do that using the I'm sorry I'm writing Secord. OK. See out Agee's. Teen age OK. Now we're not giving any breaks in between. We are not giving anything. We are writing only one statement there and that is after the case of 90. Now if the age is 14 it's going to go after the colon of case 14 and it's going to fall through it to find this a statement there after the case and to execute that. So after this we are going to give a break. And for all other cases it's not teenage. So for the default we are going to write it is not the page. Now I'm going to run this build and run. OK he's running. So I'm going to get 15. This is a teenage. Now I'll run it once more and we'll give something that is not teenage 45 that's my age. If you don't know. So it's not a teen age. So that's how you can you can you switch case and it could be an alternative to Ifill's if you really want to test the quality of a variable with some constant fad. Now if you want to test whether a variable falls in that range then it will be better to use if if. OK. And the ad operator with that. So I hope you have understood the concept conception of suitcase. Now please remember that. Remember the points that you cannot use a real number in which only the integers and the characters are out. You don't need to write the cases in order. And another important thing is that you cannot write anything out of case with a switch. Now if you are writing something here says See I'll say blah blah blah blah blah. This thing is never going to be executed because it is out of chaos right. It is not within any case you cannot write anything out of case within which everything has to be within a case. OK. So that's all about switch case. Thank you for watching.