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