C Programming Tutorial 61 - How to Write a Switch Statement

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so this video we're gonna be talking about the switch statement now this is totally random but the other day I was talking to my friend about how luxury cars have heated seats and I was thinking would it be cool if you could get luxury laptops with heated keys because I always get like cold fingers and then my typing gets terrible that's just my excuse for not being able to type a sentence without a mistake but anyways the switch statement is very similar to the if statement as I've mentioned and basically we are going to ask in this little app scenario we're going to ask the user this is a diet app by the way so we're gonna ask the user how many slices of pizza they've eaten and we're going to write their diet and tell them if they're doing a good job or not yeah totally useless I know but kind of cool and this is like the basics you know once we get this foundation you can generalize this to actually building a good diet app so these are just the basics but what we're gonna do is we're gonna start with just hard coding the the number of slices that the person ate and then eventually we're gonna get that from input but I just want to focus on the switch statement for right now so I don't want to get distracted so the way you write a switch is you say switch and you put the parentheses and then you put the variable you're doing the conditioning on and then everything goes inside of these curly braces so what you want to do is you have different cases so case one you do something case two you do something see it some cold fingers I can't type gosh and in case three oh man my hands are freezing and lastly we're gonna have a case for and when you have all these cases this works but there's something you always got to remember once you finish your cases you need to put a default I mean actually I don't think it's required but if you want to do like a catch-all then you would want the default okay now before you start coding there's one other thing you need to do and that is you need to put a break statement after each one of these cases the break statements prevent fall through and we'll see that in just a moment and even on the default I like to put a break okay so inside of here is where we can actually do code so all your code needs to go after the case label and before the break statement and here you can compliment your users for doing a very good job for only eating one slice of pizza but clearly you can see that they actually ate three so that one's not gonna be the case and then I'm just gonna do a couple of different versions - next time and then case four well you know I'm not even gonna say much for that I'll shoot okay printf you're a disappointment okay and then the default is you know if if the slices is greater than four I guess technically if if the slices was zero the default would be hit but in my brain this switch is only ever going to get hit if the person says that they ate pizza which assumes that slices is going to be at least greater than zero so let's go into this default and make a printf saying that you really need to eat less pizza okay so now what I want to do is I want to run this and see what happens you did a bad job try to eat - next time okay so that is because we hit case three and you can see that this case number correlates to the value of slices if we put slices to one which who would want to go down and their pizza consumption that's ridiculous now it says you did a great job and lastly let's go to the default case which is you know when you're really hungry enjoy your heart disease and you could go add newlines if you felt like it but I always have to end up adding new lines because I forget and it takes a bunch of time and it doesn't really serve much benefit just for the sake of learning how to use a switch statement okay now let's try getting rid of these break statements and see what happens and let's uh and also we're going to change slices to one you did a great job you did an okay job you did a bad job see the the program can't make up its mind on what to do because what happens is it goes into case one execute this and then it falls through and ends up executing all of these lines and I think that's all that I wanted to cover I did want to get this from user input though so I'm just gonna do that if you've seen this before then you can go ahead and move on to the next video if you want but just for fun I'd suggest you do it as well and for some practice so we're going to have in slices in slices scan F so yeah we've done that about a million times and we need to tell the user what to do and then eventually I think I'd like to make a calculator that kind of calculates some of the calories so I'm gonna do calories per slice and set that equal to 250 which maybe that's something the user could configure or maybe depending on the type of pizza that could be changed and then maybe down here we can just print out you had X calories and buy X I mean we're going to pass in the calories per slice multiplied by the slices and then do a new line oops okay and then add up how many slices you eat let's go with three you did a bad job try to eat - next time you had 750 calories so that is how you create a switch statement now a while ago we created a an if statement for what was it let me see this coding challenge maybe nope nope maybe it was the yeah the elsif and here we did something very similar where we created this menu and this I said that it would be very easy to convert this to a switch statement so my coding challenge for you is to go back and watch my hands-on video on else--if make sure you got this menu written out with if statements and go through the the practice of converting this to a switch statement trying all of the possibilities of input and making sure that things work out right once you got that down you should be pretty solid with switch statements they're not too crazy just make sure please for everyone's sake don't forget to put the break in after your labels okay that's all I got for you guys be sure to check out the description for my C programming crash course which is going to contain all the content from this video in a condensed Mura manly version it was the first thing that came to my mind okay [Music]
Info
Channel: Caleb Curry
Views: 34,674
Rating: 4.9415669 out of 5
Keywords: c programming, programming, tutorial, tutorial 61, how to, write, switch, statement, code, coding, caleb, curry, beginner, intermediate, switch statement, c (programming language)
Id: FSxYyHLF_9A
Channel Id: undefined
Length: 8min 38sec (518 seconds)
Published: Wed Aug 07 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.