1 00:00:00,480 --> 00:00:01,350 ‫Welcome back. 2 00:00:01,380 --> 00:00:06,600 ‫In this video, we are going to cover switch and case statements which are similar to if statements. 3 00:00:06,600 --> 00:00:10,020 ‫And let's go ahead and simply create a switch case. 4 00:00:10,020 --> 00:00:16,020 ‫So I'm here in here in my program and I create a variable first, which is going to be my comparison 5 00:00:16,020 --> 00:00:16,440 ‫variable. 6 00:00:16,440 --> 00:00:22,320 ‫So I want to compare or to check this variable and see if it has a specific value. 7 00:00:22,320 --> 00:00:26,700 ‫And in my switch case statement, I can check those. 8 00:00:26,700 --> 00:00:32,520 ‫So switch is the actual switch like a switch which you can turn on and off. 9 00:00:32,520 --> 00:00:36,720 ‫Or you could, for example, have multiple different values for the switch. 10 00:00:36,720 --> 00:00:40,470 ‫It could switch to ten to 15 to 25, whatever. 11 00:00:40,470 --> 00:00:49,020 ‫And in this case, I just want to compare the age now I need to curly brackets to create my switch body. 12 00:00:49,020 --> 00:00:51,060 ‫And then here I can have my different cases. 13 00:00:51,060 --> 00:00:53,220 ‫So let's say the case 15. 14 00:00:53,220 --> 00:00:57,720 ‫What if somebody is 15 then I want to write to the console. 15 00:00:57,960 --> 00:01:02,280 ‫Too young to party in the club. 16 00:01:02,970 --> 00:01:08,010 ‫So if somebody is 15 he can't get into the club and then we need to break. 17 00:01:08,040 --> 00:01:12,630 ‫Otherwise this case is going to create errors. 18 00:01:12,630 --> 00:01:17,400 ‫So it says break, which means if we get into this code here. 19 00:01:17,400 --> 00:01:21,810 ‫So the case 15 was true, we need to break out of the switch statement. 20 00:01:22,080 --> 00:01:26,970 ‫So the next one would be case, let's say 25. 21 00:01:26,970 --> 00:01:33,570 ‫And here you could use any case, you could use case ten, 20, 30, 15, 16, whatever. 22 00:01:33,840 --> 00:01:38,010 ‫And here I also write something on to the console. 23 00:01:38,430 --> 00:01:40,080 ‫Good to go. 24 00:01:40,080 --> 00:01:42,690 ‫So if somebody is 25, he's good to go. 25 00:01:42,690 --> 00:01:44,850 ‫And here I need to break out as well. 26 00:01:45,060 --> 00:01:52,650 ‫So always this structure, we have the switch statement, then you compare the variable, then you have 27 00:01:52,650 --> 00:01:56,640 ‫the curly brackets and within the curly brackets you have all the different cases. 28 00:01:56,640 --> 00:02:03,240 ‫So you use the word case, then the value that you want to check colon, then the code that you want 29 00:02:03,240 --> 00:02:05,640 ‫to execute and finally you use break. 30 00:02:05,640 --> 00:02:06,000 ‫All right. 31 00:02:06,000 --> 00:02:08,580 ‫So is simply the structure that you have here. 32 00:02:08,640 --> 00:02:14,820 ‫And then there is one case that you can add, and that's the default case. 33 00:02:14,820 --> 00:02:20,310 ‫So this one is called whenever none of the others is true. 34 00:02:20,310 --> 00:02:24,960 ‫So it's similar to the statement in a false statements. 35 00:02:25,320 --> 00:02:32,850 ‫And in here I'm going to say if somebody is neither 15 nor 25, how old are you then? 36 00:02:33,330 --> 00:02:37,980 ‫So that's just going to be the text written on to the console. 37 00:02:38,040 --> 00:02:40,890 ‫And also, don't forget to use the break. 38 00:02:41,340 --> 00:02:41,580 ‫All right. 39 00:02:41,580 --> 00:02:44,250 ‫So that's simply the switch in case statement. 40 00:02:44,250 --> 00:02:50,970 ‫And in here, I'm going to add the console dot, right. 41 00:02:50,970 --> 00:02:54,420 ‫So that we can see something on the console. 42 00:02:54,420 --> 00:02:57,570 ‫So I'm going to start a code or start the program. 43 00:02:57,720 --> 00:03:02,940 ‫And in a second we should see our program and it says Good to go. 44 00:03:02,940 --> 00:03:04,650 ‫That's because we are 25. 45 00:03:04,650 --> 00:03:07,680 ‫So what if we are 19? 46 00:03:08,040 --> 00:03:12,360 ‫Let's run the code and it says, How old are you then? 47 00:03:13,320 --> 00:03:17,190 ‫So you see, this is how you can simply use a switch case statement. 48 00:03:19,020 --> 00:03:20,710 ‫Now is a little challenge for you. 49 00:03:20,760 --> 00:03:24,390 ‫Try to reproduce that as an if statement. 50 00:03:26,930 --> 00:03:27,320 ‫All right. 51 00:03:27,320 --> 00:03:28,550 ‫I hope you tried it. 52 00:03:28,700 --> 00:03:40,490 ‫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 53 00:03:40,490 --> 00:03:48,560 ‫and write console dot right line to young to party in the club. 54 00:03:50,930 --> 00:03:57,200 ‫And then else if age is equal to 25. 55 00:03:58,310 --> 00:03:58,880 ‫Right. 56 00:03:58,880 --> 00:03:59,990 ‫Good to go. 57 00:04:01,610 --> 00:04:03,350 ‫And finally, let's. 58 00:04:05,750 --> 00:04:08,060 ‫How old are you then? 59 00:04:08,690 --> 00:04:13,580 ‫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. 60 00:04:14,030 --> 00:04:22,940 ‫And you can always use if statements, but in some cases a switch case statement is more appealing and 61 00:04:22,940 --> 00:04:24,080 ‫easier to handle. 62 00:04:24,080 --> 00:04:30,950 ‫And well, you can of course still use f l statements if you don't like to use switch case statements. 63 00:04:30,950 --> 00:04:32,720 ‫But don't be confused. 64 00:04:32,720 --> 00:04:39,170 ‫If you ever see a switch case statement and code that you see in a tutorial or code that is used somewhere 65 00:04:39,170 --> 00:04:41,540 ‫in StackOverflow or in the Internet. 66 00:04:41,570 --> 00:04:48,050 ‫Now let's have a quick look at a switch statement when you use strings. 67 00:04:48,050 --> 00:04:54,650 ‫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 68 00:04:54,650 --> 00:04:55,400 ‫Dennis. 69 00:04:55,520 --> 00:05:02,530 ‫So now let's create a switch statement which simply checks, switch checks the username. 70 00:05:04,180 --> 00:05:11,180 ‫And, and here I'm going to have a case of Dennis. 71 00:05:11,180 --> 00:05:17,840 ‫So if Dennis is the case, then go ahead and write something onto the console. 72 00:05:17,840 --> 00:05:25,610 ‫So console dot right line user name is Dennis. 73 00:05:28,740 --> 00:05:30,510 ‫And of course, we need the break. 74 00:05:32,730 --> 00:05:39,040 ‫Now, the next case could be somebody has the username of route, case, route. 75 00:05:41,010 --> 00:05:46,860 ‫Then we simply write root user or user name is root. 76 00:05:47,790 --> 00:05:51,870 ‫And finally, we need to default case. 77 00:05:52,410 --> 00:05:58,950 ‫So if neither of those two cases is true, then just go ahead and write something like. 78 00:06:01,070 --> 00:06:03,890 ‫Username is unknown. 79 00:06:06,190 --> 00:06:06,730 ‫All right. 80 00:06:06,730 --> 00:06:12,990 ‫So I'm just going to change that from Dennis to Frank, just to see what to show you, what happens. 81 00:06:13,000 --> 00:06:16,690 ‫So let's run the code and we see how old are you then? 82 00:06:16,690 --> 00:06:17,650 ‫It's written twice. 83 00:06:17,650 --> 00:06:23,980 ‫That's our two if statements or the switch case and the if statement and then we have the second one. 84 00:06:23,980 --> 00:06:28,510 ‫Username is unknown, so you can't only use it with integers. 85 00:06:28,510 --> 00:06:32,980 ‫You can also use it with strings and with other data types as well.