1 00:00:01,340 --> 00:00:08,870 The switch statements transfers control to one of several statements depending on the value of a condition, 2 00:00:08,870 --> 00:00:13,910 so which evaluates to either an integer or enumeration type. 3 00:00:14,750 --> 00:00:22,290 The switch keyword denotes a switch statement, so the switch statements provide conditional branching. 4 00:00:22,310 --> 00:00:30,890 When a switch statement executes control, transfer to the case fitting to the condition or to a default 5 00:00:30,890 --> 00:00:34,370 condition if no case matches the condition expression. 6 00:00:34,370 --> 00:00:42,440 So the case keyword denotes a case where the default keyword denotes the default condition. 7 00:00:42,680 --> 00:00:51,860 Somewhat confusingly, execution will continue until the end of the switch statement or to the break 8 00:00:51,890 --> 00:00:52,760 keyword. 9 00:00:52,760 --> 00:01:00,740 So you will almost you will always find a break at each end of each description and condition. 10 00:01:00,740 --> 00:01:02,630 So let's create an example. 11 00:01:02,720 --> 00:01:04,780 Switch statement, for example. 12 00:01:04,790 --> 00:01:05,690 Switch. 13 00:01:06,510 --> 00:01:07,020 Here. 14 00:01:07,020 --> 00:01:08,520 This is our condition. 15 00:01:08,520 --> 00:01:19,950 And then we have a cases case expression here, or you can write case like that in this and then handle 16 00:01:19,950 --> 00:01:22,020 a case here. 17 00:01:22,020 --> 00:01:26,220 And then you will add the here. 18 00:01:28,250 --> 00:01:35,240 And then you will add the break here after this curly braces, you will add break, which I will explain 19 00:01:35,240 --> 00:01:36,170 why. 20 00:01:36,350 --> 00:01:38,630 Uh, what what are these here? 21 00:01:38,810 --> 00:01:45,110 So then, for example, let's add second case case expression to. 22 00:01:46,560 --> 00:01:47,280 Here. 23 00:01:47,940 --> 00:01:51,960 And then let's add here. 24 00:01:52,770 --> 00:01:53,610 Handle. 25 00:01:53,640 --> 00:01:56,640 Handle a case two here. 26 00:01:56,640 --> 00:02:00,930 And then as always, we do in switch statements. 27 00:02:01,950 --> 00:02:04,650 We do break here. 28 00:02:04,770 --> 00:02:11,730 And after that, after that, we have a default keyword in switch statement. 29 00:02:11,730 --> 00:02:13,580 So let's write this as well. 30 00:02:13,590 --> 00:02:19,530 Default keyword, which will explain what is default handle. 31 00:02:19,530 --> 00:02:24,330 The default keyword is default case here. 32 00:02:25,130 --> 00:02:34,700 So now I will begin to explain this, what these keywords are and how this switch statement. 33 00:02:35,570 --> 00:02:37,430 Switch statement structure works. 34 00:02:37,460 --> 00:02:41,210 So let's start with the switch keyword. 35 00:02:41,210 --> 00:02:48,710 So switch keyword followed by the condition in parentheses here. 36 00:02:49,650 --> 00:02:52,050 Actually, we don't need to parentheses here. 37 00:02:52,800 --> 00:02:53,880 It's useless. 38 00:02:54,570 --> 00:02:58,860 So switch statement followed by this condition. 39 00:02:58,860 --> 00:03:00,300 Parentheses here. 40 00:03:01,450 --> 00:03:03,600 So I'll switch begins with this. 41 00:03:03,610 --> 00:03:08,290 You can't you just can't write a case and then switch. 42 00:03:08,290 --> 00:03:13,230 So you have to do switch and then cases and after that default here. 43 00:03:13,320 --> 00:03:19,480 So every switch statement starts with switch and condition here. 44 00:03:19,480 --> 00:03:32,500 And then each case begins with the case keyword here, followed by the cases enumeration or integral 45 00:03:32,500 --> 00:03:33,340 value. 46 00:03:34,420 --> 00:03:38,200 If condition equals here. 47 00:03:38,740 --> 00:03:40,240 For example, case one. 48 00:03:40,390 --> 00:03:49,810 For example, the code in the block containing here handle case here will execute after each statement. 49 00:03:49,810 --> 00:03:54,680 Following a case here, you place a break keyword. 50 00:03:54,700 --> 00:04:04,720 So if condition matches none of the case, the default case here, as you can see here, is executes. 51 00:04:06,790 --> 00:04:09,200 So I want to add something here. 52 00:04:09,220 --> 00:04:17,470 The braces here, uh, enclosing each case are optional, but I highly recommend this. 53 00:04:17,500 --> 00:04:21,010 Without them, you will sometimes get surprising behavior. 54 00:04:21,010 --> 00:04:24,190 So always add these curly braces. 55 00:04:25,000 --> 00:04:33,490 But in the technical sides, technical view, it's not mandatory, but highly recommended. 56 00:04:34,230 --> 00:04:39,720 So let's create a switch statement with the enumeration class. 57 00:04:40,050 --> 00:04:41,530 So I'm going to delete this. 58 00:04:41,550 --> 00:04:43,410 You don't need this here. 59 00:04:44,370 --> 00:04:45,300 And here. 60 00:04:45,900 --> 00:04:47,880 So let's create an first. 61 00:04:47,880 --> 00:04:50,210 Let's create an enum class here. 62 00:04:50,220 --> 00:04:51,200 For example. 63 00:04:51,210 --> 00:04:52,860 Enum class. 64 00:04:54,170 --> 00:04:56,300 Here race. 65 00:04:57,370 --> 00:04:59,200 Or car models. 66 00:05:00,820 --> 00:05:04,570 For my car models here. 67 00:05:04,930 --> 00:05:06,790 I will explain all of these codes. 68 00:05:07,240 --> 00:05:13,030 Here, for example, BMW, BMW, Mercedes. 69 00:05:14,990 --> 00:05:15,680 Burnout. 70 00:05:17,400 --> 00:05:18,000 Ferrari. 71 00:05:20,740 --> 00:05:21,790 Volkswagen. 72 00:05:22,650 --> 00:05:25,720 Port here cheap. 73 00:05:26,160 --> 00:05:28,050 And I think that's enough. 74 00:05:30,110 --> 00:05:33,350 So as you can see, one, two, three, four, five, six, seven. 75 00:05:33,350 --> 00:05:35,540 We have seven car models here. 76 00:05:35,750 --> 00:05:39,740 So in main statement, let's write our switch. 77 00:05:40,220 --> 00:05:41,780 Switch case statements here. 78 00:05:41,900 --> 00:05:46,160 So let's create the object race here. 79 00:05:47,130 --> 00:05:49,260 Or my car models. 80 00:05:49,260 --> 00:05:50,490 My car models. 81 00:05:51,810 --> 00:05:57,300 Okay, so cars here or my cars. 82 00:05:58,110 --> 00:06:00,910 Equals my car models here. 83 00:06:00,930 --> 00:06:07,380 This operator and for example, Ferrari. 84 00:06:10,330 --> 00:06:14,620 So then I will explain all of these codes as I do. 85 00:06:14,620 --> 00:06:17,880 Always switch here. 86 00:06:17,890 --> 00:06:18,700 Switch. 87 00:06:20,160 --> 00:06:27,150 As you can see, our integrated development environment automatically created this braces here. 88 00:06:27,150 --> 00:06:38,240 So now we will give the switch here, my cars, because we will use this in class here and we will use 89 00:06:38,490 --> 00:06:39,150 class members. 90 00:06:39,150 --> 00:06:48,510 So mainly firstly we will give my car as a parameter and then we will write our case and default statements. 91 00:06:49,430 --> 00:06:51,110 So let's start with. 92 00:06:53,250 --> 00:06:56,500 Let's let's follow this order here. 93 00:06:56,550 --> 00:06:58,830 So switch case. 94 00:06:58,860 --> 00:07:00,300 Switch case. 95 00:07:01,330 --> 00:07:02,460 Um, switch. 96 00:07:02,670 --> 00:07:04,590 Case here. 97 00:07:05,400 --> 00:07:08,860 Were my cars, my car models. 98 00:07:08,880 --> 00:07:11,460 For example, BMW and then. 99 00:07:12,630 --> 00:07:14,120 Here curly braces. 100 00:07:14,130 --> 00:07:25,020 If the case is BMW, you have a BMW and then we will break code. 101 00:07:25,800 --> 00:07:31,770 Break this case statement after the curly braces here. 102 00:07:31,860 --> 00:07:34,650 And let's add another case here. 103 00:07:35,100 --> 00:07:41,100 Case, my car models, we have the Mercedes here. 104 00:07:41,430 --> 00:07:54,150 And then print if you have a mercedes Mercedes Benz here and then break our code. 105 00:07:54,630 --> 00:07:55,550 Break our code. 106 00:07:55,560 --> 00:08:00,770 Add another case because we have six variables here. 107 00:08:00,780 --> 00:08:06,660 Then we have a renowned my car models renowned here. 108 00:08:06,660 --> 00:08:13,720 Print f you have a renowned here and then break this as well. 109 00:08:14,910 --> 00:08:16,590 Uh, well, okay. 110 00:08:16,890 --> 00:08:17,520 Yeah. 111 00:08:18,360 --> 00:08:19,740 Uh, what's the problem here? 112 00:08:19,770 --> 00:08:20,430 Yeah. 113 00:08:21,180 --> 00:08:21,990 Fix that. 114 00:08:22,920 --> 00:08:26,990 And my car models renowned. 115 00:08:27,000 --> 00:08:30,090 We have a well here we have renowned. 116 00:08:30,090 --> 00:08:31,920 And let's add Volkswagen. 117 00:08:33,340 --> 00:08:35,380 Print f you. 118 00:08:36,340 --> 00:08:41,460 You have a Volkswagen and then break this code. 119 00:08:41,470 --> 00:08:42,820 I'll also here. 120 00:08:42,820 --> 00:08:51,280 And lastly, we have two car models, case my car models here. 121 00:08:52,030 --> 00:08:54,610 Let's add Ford and. 122 00:08:55,330 --> 00:08:55,960 Here. 123 00:08:56,440 --> 00:09:00,310 Print f you have a fourth. 124 00:09:03,140 --> 00:09:04,100 Break. 125 00:09:04,880 --> 00:09:08,450 And lastly, my car mode. 126 00:09:08,530 --> 00:09:09,050 Oops. 127 00:09:10,150 --> 00:09:15,190 My my car models and jeep here. 128 00:09:16,750 --> 00:09:19,300 Print f you. 129 00:09:20,140 --> 00:09:23,350 You have a jeep here. 130 00:09:23,590 --> 00:09:27,120 And add the brake statement here. 131 00:09:27,220 --> 00:09:30,760 Break here and then we're going to write. 132 00:09:31,520 --> 00:09:32,540 Did default. 133 00:09:33,430 --> 00:09:34,150 Here. 134 00:09:34,150 --> 00:09:35,380 The fault. 135 00:09:36,900 --> 00:09:37,290 Oops. 136 00:09:39,820 --> 00:09:43,180 Here the false statement print. 137 00:09:46,320 --> 00:09:47,220 Print f. 138 00:09:48,590 --> 00:09:51,020 Uh, error here, for example. 139 00:09:52,010 --> 00:09:53,210 User error. 140 00:09:54,570 --> 00:09:55,200 Unknown. 141 00:09:55,670 --> 00:09:56,450 Unknown. 142 00:09:57,550 --> 00:09:58,090 UN. 143 00:10:01,820 --> 00:10:05,690 Unknown make here or let's write model. 144 00:10:05,840 --> 00:10:09,560 Unknown model here and. 145 00:10:10,740 --> 00:10:11,340 Add. 146 00:10:12,050 --> 00:10:12,860 And press here. 147 00:10:12,860 --> 00:10:13,520 So. 148 00:10:16,490 --> 00:10:19,490 But let's now explain this clause here. 149 00:10:19,490 --> 00:10:29,090 The we create the enumeration class declares the enumeration type my car models, which you use to initialize 150 00:10:29,090 --> 00:10:32,510 my car models to Ferrari. 151 00:10:32,540 --> 00:10:42,500 So the switch statement here evaluates the condition race to determine which condition to hand to control 152 00:10:42,500 --> 00:10:42,710 to. 153 00:10:42,710 --> 00:10:51,770 So because your hard coded this to the Ferrari earlier in this code as you can see here execution transfers 154 00:10:51,770 --> 00:10:52,820 to. 155 00:10:53,640 --> 00:10:54,660 Uh, here. 156 00:10:55,720 --> 00:10:56,500 Ops before. 157 00:10:56,530 --> 00:11:00,740 Actually, we forgot to write the Ferrari here after. 158 00:11:01,210 --> 00:11:02,590 Yeah, I'm sorry for it. 159 00:11:02,620 --> 00:11:04,510 Let's add case. 160 00:11:05,530 --> 00:11:09,900 My car models Ferrari here. 161 00:11:09,910 --> 00:11:10,960 Print f. 162 00:11:12,000 --> 00:11:14,220 You have a Ferrari? 163 00:11:15,300 --> 00:11:15,960 Here. 164 00:11:16,700 --> 00:11:17,300 So. 165 00:11:17,840 --> 00:11:19,430 And execution. 166 00:11:21,190 --> 00:11:24,280 Just our let's add break here. 167 00:11:24,280 --> 00:11:24,730 So. 168 00:11:27,200 --> 00:11:30,110 Because you hardcoded this. 169 00:11:32,240 --> 00:11:35,210 Uh, Ferrari in this code. 170 00:11:35,900 --> 00:11:39,080 The execution here transfers to this Ferrari. 171 00:11:39,380 --> 00:11:42,230 So which prints you to. 172 00:11:42,230 --> 00:11:44,270 You have a Ferrari? 173 00:11:45,430 --> 00:11:47,800 So the break here. 174 00:11:48,520 --> 00:11:52,510 The break here terminates the switch statement. 175 00:11:52,510 --> 00:11:58,810 So the default condition at this here is safety feature. 176 00:11:58,810 --> 00:12:07,930 So if someone adds a new race value or a new my new my car models value to the enumeration class, you 177 00:12:07,930 --> 00:12:14,830 will detect that unknown race to runtime and print an error message. 178 00:12:14,830 --> 00:12:26,920 So try setting this my car race models racing models to different values and let's see how the output 179 00:12:26,920 --> 00:12:27,400 changes. 180 00:12:27,400 --> 00:12:31,720 So firstly I want to compile code and run here. 181 00:12:33,030 --> 00:12:39,960 And as you can see here, we got an output that says you have an you have a Ferrari. 182 00:12:39,960 --> 00:12:43,830 So let's let's change this Ferrari to BMW here. 183 00:12:44,640 --> 00:12:51,750 And we will get and this case executes and we got the you have a BMW. 184 00:12:51,750 --> 00:12:58,140 So let's add another make model here, for example. 185 00:13:00,290 --> 00:13:02,810 Let's add GMC here. 186 00:13:04,300 --> 00:13:06,430 And use this GMC. 187 00:13:07,720 --> 00:13:12,730 And here, as you can see here, we got an a user error. 188 00:13:12,760 --> 00:13:24,790 As you can see, we got a printed this because we didn't define this GMC on this switch statement.