C++ Programming Tutorial 35 - Switch Statement and Enum

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on everybody this video we're gonna be talking about the switch statement inside of C++ you know I thought I'd switch it up a little bit but first you gotta check out our sponsor Embarcadero rad studio rad studio is the IDE of choice for C++ development quickly build native mobile and desktop applications from a single C++ code base and deployed to Windows Mac iOS and Android with rad studio user interface design has been made easy with hundreds of pre-built components for cross-platform development you can easily integrate with popular source control management systems databases api's and you can make your life easier with numerous third-party extensions let rad studio do the heavy lifting when it comes to C++ development give it a go with a free trial by following the link in the description so when it comes to creating a switch statement what's gonna happen is you're going to say switch put parentheses and then put curly braces so it's the same exact structure as an if statement which looks like that but the actual functionality is a little bit different so what's going to happen is where you would normally put an expression inside of an if statement or actually just going to put a variable the example I gave in an earlier video was age so we're just going to say int age and what we're going to do is we're actually going to get that from input so we'll get C in and store that inside of age and we'll just put an output just so people know what to do so let's ask the audience what their age is so we can put that variable name here in the switch and then inside of here we have case labels so we make a label for different values so we can say the case where the age is hmm I don't know 13 and what we're going to do is we're going to do a C out here and I'm just gonna be very informative and tell them that their age is 13 I know that's really helpful but you guys get to pick you once you understand how the functionality works you can use this for different things this is just a silly example here and then what we're going to do is we're going to say break which says we're done with this case and we're going to talk a little bit about that break keyword here in a minute then we'll say a case 14 yep so the reason I don't like switches is because they're not super useful or versatile like if you're going to have more than a couple options you have to put every single option as its own case and then you can do a catch-all using a default but overall it's kind of complicated and I'm not the biggest fan of switches I think they are pretty to look at when you have a few options so if you have a menu a selection for example you could basically say case 0 case 1 case 2 that would be nice and pretty you are actually going to be using a switch statement later for that I believe so you'll see it later on for that example but for the general purpose use switch is not the most useful thing and you'll see why the first reason why is it has to be an integral type so first let me compile just to make sure I got everything working got all the code working what is your age I need 2 semicolon and there we go so it seems to compile but that's because age is an integral type it's of int but if we do something such as make this a double well now it's not even going to let us compile so that's just really not useful that the type has to be an integral type it makes it very limited but we'll deal with it we'll go with an integer and we'll give it a try so let's compile and run what is your age let's just say our age is 14 you can see we hit the case 14 and says you are 14 thank you computer you're very helpful so that is how I switch works now let's first talk about this break keyword this is actually not a required keyword so it's it's silly because if you don't put it there we're going to have an issue known as fall through which is bad it's not a feature it's it's a it's an unfortunate occurrence so if we compile this and run and let's say our age is 13 what's gonna happen is it says you are 13 you're 14 and then catch all so actually falls through and hits all of these print statements so when you don't have a break statement it's going to fall through and hit all the cases until it has a break or a return so if you don't want to use a break you can use a return such as in the case of main we can return negative 1 or we could return whatever number you want zero means everything works fine negative one means something went bad so where a return should work as well so in this situation if we say our age is 13 it's fine so that is one way you can do it but generally you're going to see the break keyword here so you're always going to want to make sure you put that break keyword in there even if it's the last one just as a best practice in other languages such as C sharp the break is actually required which I personally like because there's not very many scenarios where you're going to want to leave that break off of there the only situation where you might not use a break is if you combine cases so for example if you want to combine 13 and 14 you can get rid of all of the content of case 13 and leave it like so and then you could say you're 13 or 14 so in this situation if you put the value 13 or 14 this will get it so just to see that let's go in there and we'll put the value 13 and if we run it again and put 14 you can see they both hit that output now one thing you might find interesting with a switch statement is you can actually use it for an enumeration now if you guys remember what we did with enumerations we talked about them briefly and we talked about constants some people use them as an alternative to a constant other people use them when you just have a series of potential options so let's go through an example of creating an enum and we'll use it inside of a switch statement why exactly can we use an enum because behind the scenes and enum actually stores possible values as an integral type so it's basically the same thing as using numbers it doesn't look like we're using numbers but we actually are so it's pretty cool so if you haven't worked with enums it's nothing too crazy so don't freak out just follow along so we're gonna do is we're gonna say enum and we'll just say let's go with the seasons and in here we're going to go with summer spring fall and winter and let's just get rid of all this information here because we're just gonna work with the seasons directly what you can do is you can actually create a new season and call it now and set that equal to winter for example which yes it's winter it's freezing and then what we can do is we can switch on that variable now and we're gonna have completely different case values so let's just get rid of all this stuff we'll leave that default in there and get rid of these cases so we can say case and have the different possible case values so we could say case summer ok spring case fall and case winter now because we have a very limited number of options here we're actually not going to need that default because it's not possible for now to have anything else than these four options so let's compile see if I got anything completely wrong yes I forgot to put an S on here so it would actually be better to probably make this singular so we'll just call it season now before we run this we actually want it to do something so we'll just go through the case of winter and we'll do an output just keep it nice and simple and you could do that for all of these but I'm just gonna do it for winter because I know that's the one that's going to be hit here when we run it you can see it says stay warm so that is how you can use a switch statement with an enum nice and simple now I'm also going to try this with what's known as an enum class it works very similar but it's a little bit different I'll typically just put a capital S instead of a lowercase s when I'm using the class type and then you need to preface these with season like so with the double colons and this just allows us to scope our enum to this season so it doesn't just pollute the entire area that it's defined in if that makes any sense at all and then we also need to do that here so we can say season : : winter last thing is we need to capitalize this s sorry yes also when you compile this class enum is a C++ 11 capability so you might need to say standard equals C++ 11 all right that should be everything we should be good to go now now when we run it we get the same exact thing so this is a little bit different and I know this video wasn't a tutorial on a nose but it's definitely something I wanted to squeeze in here somewhere the class enums are just a little bit better just because this summer is only available when we preface it with seasons so it doesn't just make this summer available everywhere so you can look up the pros and cons of whether to use Class C gnomes or just standard items that's not a topic for this video but here you can see the you of enums inside of the switch statement so hopefully I was helpful guys my dogs whining so I should probably go give him some attention and thank you if you've enjoyed the content please be sure to subscribe it really helps not much I'm trying to [Music]
Info
Channel: Caleb Curry
Views: 28,476
Rating: 4.8853288 out of 5
Keywords: c++, programming, tutorial, switch, statement, enum, code, coding, beginner, intermediate, caleb, curry, calebthevideomaker2, software (industry), c (programming language), switch statement
Id: GSja2w-HN20
Channel Id: undefined
Length: 9min 31sec (571 seconds)
Published: Tue Mar 12 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.