‫Welcome back.
‫In this video, we are going to cover switch and case statements which are similar to if statements.
‫And let's go ahead and simply create a switch case.
‫So I'm here in here in my program and I create a variable first, which is going to be my comparison
‫variable.
‫So I want to compare or to check this variable and see if it has a specific value.
‫And in my switch case statement, I can check those.
‫So switch is the actual switch like a switch which you can turn on and off.
‫Or you could, for example, have multiple different values for the switch.
‫It could switch to ten to 15 to 25, whatever.
‫And in this case, I just want to compare the age now I need to curly brackets to create my switch body.
‫And then here I can have my different cases.
‫So let's say the case 15.
‫What if somebody is 15 then I want to write to the console.
‫Too young to party in the club.
‫So if somebody is 15 he can't get into the club and then we need to break.
‫Otherwise this case is going to create errors.
‫So it says break, which means if we get into this code here.
‫So the case 15 was true, we need to break out of the switch statement.
‫So the next one would be case, let's say 25.
‫And here you could use any case, you could use case ten, 20, 30, 15, 16, whatever.
‫And here I also write something on to the console.
‫Good to go.
‫So if somebody is 25, he's good to go.
‫And here I need to break out as well.
‫So always this structure, we have the switch statement, then you compare the variable, then you have
‫the curly brackets and within the curly brackets you have all the different cases.
‫So you use the word case, then the value that you want to check colon, then the code that you want
‫to execute and finally you use break.
‫All right.
‫So is simply the structure that you have here.
‫And then there is one case that you can add, and that's the default case.
‫So this one is called whenever none of the others is true.
‫So it's similar to the statement in a false statements.
‫And in here I'm going to say if somebody is neither 15 nor 25, how old are you then?
‫So that's just going to be the text written on to the console.
‫And also, don't forget to use the break.
‫All right.
‫So that's simply the switch in case statement.
‫And in here, I'm going to add the console dot, right.
‫So that we can see something on the console.
‫So I'm going to start a code or start the program.
‫And in a second we should see our program and it says Good to go.
‫That's because we are 25.
‫So what if we are 19?
‫Let's run the code and it says, How old are you then?
‫So you see, this is how you can simply use a switch case statement.
‫Now is a little challenge for you.
‫Try to reproduce that as an if statement.
‫All right.
‫I hope you tried it.
‫So I'm just going to put it just underneath and I'm going to say if H is equal to 15, then go ahead
‫and write console dot right line to young to party in the club.
‫And then else if age is equal to 25.
‫Right.
‫Good to go.
‫And finally, let's.
‫How old are you then?
‫So as you can see, this is how we do it with an f l statement and this is how we do a switch case.
‫And you can always use if statements, but in some cases a switch case statement is more appealing and
‫easier to handle.
‫And well, you can of course still use f l statements if you don't like to use switch case statements.
‫But don't be confused.
‫If you ever see a switch case statement and code that you see in a tutorial or code that is used somewhere
‫in StackOverflow or in the Internet.
‫Now let's have a quick look at a switch statement when you use strings.
‫So I'm going to use a click quick string and I'm going to call it one username and it's going to be
‫Dennis.
‫So now let's create a switch statement which simply checks, switch checks the username.
‫And, and here I'm going to have a case of Dennis.
‫So if Dennis is the case, then go ahead and write something onto the console.
‫So console dot right line user name is Dennis.
‫And of course, we need the break.
‫Now, the next case could be somebody has the username of route, case, route.
‫Then we simply write root user or user name is root.
‫And finally, we need to default case.
‫So if neither of those two cases is true, then just go ahead and write something like.
‫Username is unknown.
‫All right.
‫So I'm just going to change that from Dennis to Frank, just to see what to show you, what happens.
‫So let's run the code and we see how old are you then?
‫It's written twice.
‫That's our two if statements or the switch case and the if statement and then we have the second one.
‫Username is unknown, so you can't only use it with integers.
‫You can also use it with strings and with other data types as well.