F on the test to tell you that you failed the test so we’re basically gonna have a variable where we’ll store a grade that you would get on a test and we’ll be able to respond to that grade using a switch statement so it’s gonna be pretty cool so down here the first thing I’m gonna do is create a variable and it’s just gonna be a character and I’m just gonna call it grade and I’m gonna set it equal to a so we’re gonna say that whoever took this test got an a on the.
Test and they did really well now what I want to.
Do is I want to create a switch statement so I’m just gonna say switch and I’m gonna make an open and closed parenthesis and inside of this parenthesis I’m gonna put grade so I’m just gonna put grade and I’m gonna make an opening close curly bracket so this is sort of like the basic structure for our switch statement now here’s what I’m basically gonna do I’m gonna take this variable here.
That I pass into the parentheses grade and I’m gonna compare it to a bunch of different values so I’m gonna take this I’m gonna see if it’s equal to one value.
Then I’m gonna see if it’s equal to a different.
Value or a different value or a different value and depending on the value that it ends up being equal to we’re gonna do something different and these are what are called cases so down here I can say case and I just want to type out a character so for example I could put a right here and then I’m gonna put a colon and when I put a right here basically what this is saying is the case.
That grade is equal to a then I’m gonna put some code.
Down here that I want to execute so I could just put like a print statement and I could say like you did great because they did great on the test and basically what this is saying is in the case that the grade is equal to a we’re gonna print this out and then I want to put one more thing down here which is called break and I’m gonna explain what break does in a second so I could make one of these little case statements for every possibility so.
For every possible grade that we could get so I’m actually just gonna copy this whole thing and I’m gonna paste it down here so now I can put a B in here so we’re saying in the case that the grade is equal to B I’m gonna say.
You did all right and then we’re also going to break and down here I’m gonna make another one so I’ll say case and this time we’ll make it C in the case that they get a C I’m gonna print out portal e and there’s a couple more that we can check so we can check if they got a D or an F so in the case they got a D and I forgot to put a break statement.
Here and I’m gonna put one down here too and finally we’re just gonna do one more for if they failed the test so I’m just gonna say in the case that they got an F we will print out you failed so.
Essentially what I’m doing here is I’m.
Writing out all of these different cases so I’m saying in the case that you got an F I’m gonna print out.
You failed and the case they got a D I’m gonna print out you did very bad and then we also have this break statement here and this break statement basically tells our switch statement that we want to leave so whenever we’re executing this switch statement whenever C is executing the switch statement it’s checking to see if grade is equal to each one of these cases so for example let’s say the grade is equal to a it’s gonna execute this print F but if I don’t.
Put this break statement in here then it’s just gonna keep going and keep checking these different cases so it’s always important to put a break statement in there because I basically just want to break out of this switch statement I don’t want to be in here anymore so finally we can check one.
More thing in the case that the user or that we get an invalid grade so for example if someone.
Entered in like a t or G or something that’s not a valid grade so I can use something called default and default is basically like an else so default is going to execute whenever none of these conditions up here or Matt so then I can just put something like invalid grade or something and this will basically be like hey you got an invalid grade so let’s take a look at this switch statement and we’ll just.
Kind of talk a little bit more about what it’s doing basically I’m passing this in a variable or a value.
Or something and I’m gonna check this value against all of these different conditions so in the case the grade is equal to a we’re gonna print you did great and the case that B is equal to grade will print you did all right etc and this is.
Basically doing what an if statement doing so I can replicate this entire statement this entire switch statement as an if statement I can write out if etc else–if else–if else–if.
But this just makes it a lot.
Easier so I don’t have to constantly write out all of that if statement stuff so hopefully we did everything right and let’s actually check and see how our program does so grade.
Is equal to a let’s run our program and see what our message is it says hey you did great because we got an A if I made this an F instead of an A now it should tell.
Us that we failed so it says you failed if I made this a C it’ll tell us that we did poorly and it says you did poorly so this is a super easy way for us to check one single value against a bunch of.
And that’s really the point of a switch statement is you’re checking one single value like grade against a bunch of other values and that’s where it can.
Be extremely useful so consider using the switch statements they’re not appropriate in every situation.
And there’s I’d say like very select few situations where switch statements will come in handy but when they are appropriate switch statements will save you a lot of time and a lot of hassle in this tutorial I want to talk to you guys about strux in C now a struct is a data structure where we can store groups of.
Data types so inside of a struct I could store like an integer alongside of a string alongside a character alongside a double I could sort.
All these different data types in one single data structure.
And there’s tons of uses for structs and one of the things we can do with them is model real world entities so I could basically like a.
Model something in the real world inside of my program and I’m gonna show you guys how to do that in this tutorial we’re gonna.