0:00 was going on guys and welcome to your twenty-fifth C plus plus tutorial and in this tutorial. And in this tutorial, I going to be 0:05 talking to you guys about 0:06 a really cool feature of c plus plus, not only C plus plus, 0:10 this is basically an all computer programming languages, but it's called 0:13 the switch statement. 0:14 in the switch statement, in your computer programming career, 0:18 is going to save you a whole lot of time and headaches. 0:21 so let me give you guys an example of when you would use this 0:24 say you have a variable like age. And you want to test age of a user 0:28 and 0:29 and you had a bunch a different uhm, 0:33 like statements you want to print out depending on how old they were. So, if 0:36 they were 16, 0:37 uhm, you'd say "congratulations here's a new car 0:41 since you can drive now" or 18 you would say, "go buy a lottery ticket" 0:45 if they're 21, you say "go buy some beer" so you have 0:49 all these different values that you could have applied applied to the same variable. 0:52 so with our knowledge so far we can build program like this 0:56 doing this. if age equals 16 1:00 let's go ahead and type our body and then after this 1:03 if age equals this yadda yadda equals that 1:08 well all those if statements are getting pretty old, so wouldn't it be cool 1:12 if we could combine this all and kind of a compact 1:16 statement? well it would be cool. but, unfortunately we can't. So, on to the next 1:21 tutorial in the next tutorial. 1:22 I'm just kidding. we actually can do this and that is through the use of something 1:26 called a switch statement. 1:28 now in order to use a switch statement here's what you do. 1:31 type the word "switch" now in parentheses right after that 1:36 write the variable that you want to test. well, we want to test the value 1:40 age so it go ahead and type age right there. now after this 1:44 go ahead and add the body to your statement. now inside your body 1:48 you going to give all the values which you think 1:52 age is equal to. And we are going to be giving it in the form of cases. 1:56 so, pretty much for saying this. In case they enter sixteen 2:00 but don't write in case. It's only case, so in the case 2:04 of sixteen. Then write ":". now after this colon, 2:08 you write whatever statements you want your computer program to perform 2:11 or computer program to perform. you are the computer programmer. 2:15 this is computer programming. There, I got straight in my brain. so 2:18 in case the value of age equal 16, let just go ahead and put something stupid down 2:23 screen like 2:23 cout "hey you can drive now" 2:26 and go ahead and end line. why not. 2:30 and after this add a key word that says "break". Now 2:34 I'm going to be talking about break later on, because it's easier if I 2:37 make the whole program first before I talk about it. now after this we can 2:42 write something like alright, 2:43 what if the case is 18? and what are we going to print out then? 2:46 uhm cout on the screen. uhm 2:49 "go buy some lotto 2:52 tickets" because you can do that 18. 2:55 now in line and of course after this at that break statement that we 2:59 don't know what does yet but we will. and let's go ahead give it one more case, why not? 3:04 And the case of 3:04 21, the best one, uhm, let's go ahead and 3:08 cout "buy 3:12 me some beer" and go ahead and that line, so 3:17 now you can see, through the use of the switch statement, it 3:20 basically saved us the trouble and the time up making a bunch 3:24 of different if statements. So instead of three different if statements, 3:27 fuh, fuh, fuh, there different statements we only had one switch 3:31 and we just tested a bunch different cases. so the last thing 3:35 is "default" what default means, is if none of these cases were true 3:41 if they entered value like two or something like that. 3:44 It ran through all these cases. was equal to 16? 3:47 nope. 18? nope. 21? nope. so what you want me to perform 3:51 by default? and of course you don't need like default too because 3:55 it doesn't have a value. it just all these values failed 3:59 then what do you want me to do by default. by default which is write cout 4:03 uhm, sorry 4:06 you get nothing because 16, 18, 21, 4:10 Thats reality, where the important stuff happens. everything else is just birthday 4:14 so let's go ahead and end that line. And, one thing you might notice 4:18 uhm, when we build and run this, and alright, uh sorry you get nothing, because 4:23 you're too boat. Before you go and don't run this 4:26 "why do you I had a break at the end of default? and what exactly is break? 4:30 well, this break keyword right here pretty much means 4:34 end the switch. So you are saying "alright". why would I want to end it? 4:38 why doesn't it just keep going through the rest of the stuff? 4:41 well the reason you add break after each case is because this: 4:45 say you had 16? and you want to test it. 4:48 we know that whenever ran it printed out, 4:52 hey you can drive now. and that's because we tested age 4:56 and age is equal to 16. so in our first cases it says hey 5:01 this is a match, let's go ahead and run this bit of code right here 5:04 so since we got a match in our first case, 5:07 there's no need to test this one, this one, and we know we're not going to run the 5:12 default, because 5:13 this is the only one we want to run right here. So, what break says 5:17 is break this switch. it pretty much saves your program the trouble of having to 5:20 run through the rest of these cases. 5:22 because it's not going to be 5:26 useful to run through the rest from as soon as he found a match. 5:29 so let's go ahead and say we had 18 in there. well what we would do is 5:33 it runs it and it prints out "go buy some lotto tickets" 5:36 since we have that break in there, it did this, alright, 5:40 in the case of 16, nope you're 18, and the case have 18, 5:44 bingo we have a match. so let's go ahead and print out "go buy some lotto tickets" 5:49 and break out of this loop. so no need to test 21, 5:52 and no need to use the default, since we broke out of it. 5:56 we found our match. so, again what break does 5:59 is it basically saves you from going through the rest the loop. 6:03 and why do I not have one and default because 6:06 by the time we get to the end default it said and that the switch anyway so 6:11 why would you need to break out if you just can exit out of it after the 6:14 default anyways. 6:15 so that is basically a switch statement 6:18 It basically takes a variable and tests the value against 6:22 each one of its cases. 16, 18, 21 6:25 if it isn't any one of those values it runs the default. 6:29 now I have three cases in this example, but you can have one. 6:33 you can have a thousand. you can have a million if you want. 6:36 and yeah, that's all. so anyways, 6:39 that is how a switch works and that is what break is. 6:42 And, I just want to mention one more thing before I let you guys go. 6:46 break, I'm using it in this example of a switch 6:50 but, it is a key word, that we are goint to be coming across a lot in C++ programming 6:55 not only with switch. So, whenever you see it later on, whenever we're doing 6:59 you know just regular loops or something, I just want to let you guys know 7:02 it just isn't only limited to switch statement. we can actually use this 7:06 all throughout C++. So, whenever you come across it, don't be like hey, isn't a 7:10 switch supposed to be going around that, because 7:12 we're not. so now, thank you guys for watching. 7:16 understand the switch and once you do you're ready to move on to my next video. 7:19 so 7:19 thank you guys for watching. Don't forget to subscribe. And I will see you in the next video.