WEBVTT 0 00:03.630 --> 00:04.300 Welcome! 1 00:04.300 --> 00:11.180 Now let's talk about the switch statement. A "switch" statement is similar to an if statement but with a 2 00:11.180 --> 00:17.930 different syntax. there are two types of switch statements actually, expression switches and type switches 3 00:17.940 --> 00:24.430 but for now you're only gonna learn about the expression "switch" statement you'll learn about 4 00:24.430 --> 00:28.300 the type switch later because it's related with interface types. 5 00:28.300 --> 00:29.140 OK. 6 00:29.400 --> 00:30.660 All right let's get started. 7 00:33.170 --> 00:36.770 Now I'm going to start easy and build up your knowledge step by step. 8 00:36.780 --> 00:43.140 As usual this is at that you want to write a program that will print the name of a country for a given 9 00:43.370 --> 00:43.950 name. 10 00:45.270 --> 00:53.250 For example if the is in Paris then it will print France or how French people say it like finds it. 11 00:53.250 --> 00:56.680 I don't know whether I spell it correctly or not and Twice. 12 00:56.850 --> 01:01.840 OK I have a variable called the and it equals to parse. 13 01:01.860 --> 01:04.760 All right here is a sorry statement. 14 01:06.570 --> 01:08.580 As expected it starts with us. 15 01:08.600 --> 01:09.690 Each q r. 16 01:10.140 --> 01:15.540 Ok as I said I want to find paresis and I have a city where able. 17 01:15.990 --> 01:22.050 So I need to type to the variable here like this is you'll see in a minute pseudo statement view. 18 01:22.060 --> 01:25.280 Look up for it is value defined Purus. 19 01:25.300 --> 01:28.600 I need a case clause inside the switch statement like this. 20 01:30.470 --> 01:36.760 Since I'm looking for purpose I'm going to type Paris with a column after what's OK. 21 01:36.760 --> 01:40.410 Now let's take a look at the parts of this statement in the 22 01:44.700 --> 01:49.150 highlighted part is called Subi just condition expression. 23 01:49.330 --> 01:56.200 The type of this condition expression is very important because it determines the type of the case conditions 24 01:59.850 --> 02:00.720 Steve were able. 25 02:00.730 --> 02:03.910 Here is a Swink value right. 26 02:04.050 --> 02:09.180 So the type of all the case conditions should also be strange. 27 02:09.600 --> 02:13.110 That's why Parvis there is a string value. 28 02:13.230 --> 02:20.240 It cannot be an end value for example but if the switch is condition which is still able. 29 02:20.240 --> 02:27.880 Here was an int and the type of the case conditions would also be an invalid OK. 30 02:27.880 --> 02:30.320 Now let's take a look at the case clause in detail. 31 02:32.300 --> 02:40.550 This highlighted part is a case Klause or simply a case after the column here go creates a block Xclusive 32 02:40.640 --> 02:42.650 only forty's case class. 33 02:43.160 --> 02:49.800 So anything that VB declared inside this case class you only ever label to itself. 34 02:49.820 --> 02:58.080 OK and this highlighted part is a case condition expression or simply a case condition. 35 02:58.610 --> 03:06.690 So run the steamer variable equals to Paris go execute these case clauses block and here in this block. 36 03:06.790 --> 03:09.890 There is only Vonne printed on call right. 37 03:10.030 --> 03:15.700 So go execute it and it will print France to the council. 38 03:15.760 --> 03:23.890 It's like typing in an IF statement like this so as so each statement compares the given value to all 39 03:23.890 --> 03:28.420 of its case Klaus's in terms of equality. 40 03:28.590 --> 03:34.590 Here it will execute this case clause because you are able and the case clauses condition equals two 41 03:34.600 --> 03:36.800 parts is by the way. 42 03:36.800 --> 03:40.900 Of course you can also add more cases to us which statement. 43 03:41.020 --> 03:43.590 Now as an example let's add another step. 44 03:43.840 --> 03:51.090 For example Tokyo although it's distric but let's call it here as a city OK. 45 03:52.070 --> 03:55.870 So all I need to do is to add another case clause like this. 46 03:57.680 --> 04:00.290 Dumb going to print its country as Japan 47 04:03.710 --> 04:06.770 does so his statement just compares the stairway. 48 04:06.800 --> 04:14.750 For each case condition one by one and if one of them is equal to the serious condition dango Rantz 49 04:14.760 --> 04:15.200 the code. 50 04:15.220 --> 04:21.250 In that case just block OK let's say that this is tōkyō down to talk. 51 04:21.250 --> 04:26.830 Your case will match then this block will be executed. 52 04:27.030 --> 04:34.100 Remember it's block starts after the column here so it will call the preinstall and there and it will 53 04:34.100 --> 04:35.940 print Japan to the console. 54 04:37.520 --> 04:42.590 If this the last part is done the first case would be executed instead and it will print. 55 04:42.590 --> 04:51.110 France OK let me show you this whole switch statement inside decoders are in action so you can understand 56 04:51.110 --> 04:51.900 it better. 57 04:54.600 --> 04:56.890 Now let's change the city to Tokyo. 58 04:56.880 --> 05:04.210 Here that's it. 59 05:04.260 --> 05:07.110 This time it pre-NST Japan. 60 05:07.160 --> 05:12.780 This is because now here equals to this case condition here right. 61 05:13.820 --> 05:14.560 So it's wrong. 62 05:14.600 --> 05:21.690 These instead remember a silly statement just compares the values whether they are equal or not. 63 05:22.050 --> 05:25.240 And that's all about it. 64 05:25.280 --> 05:32.010 Now let's take the rally from the common line as an argument so let's change talk here to Oist not ours. 65 05:32.010 --> 05:41.940 Instead like this what in Litmus right again it brings France correct. 66 05:42.820 --> 05:48.770 OK let's try to talk you know it brings Japan good. 67 05:48.780 --> 05:53.120 It works Litton type this as an IF statement. 68 05:53.290 --> 05:54.800 I'm going to fast forward this. 69 05:54.850 --> 05:57.220 So you don't need to wait for me to type it here. 70 05:57.220 --> 05:58.140 OK. 71 05:58.330 --> 05:58.690 All right. 72 05:58.690 --> 06:00.060 Here it is. 73 06:00.340 --> 06:04.460 So the statement actually equals to this statement here. 74 06:04.560 --> 06:12.310 For example in C Savvich statements cannot use runtime expressions but Ingall you can use an expression 75 06:12.520 --> 06:15.130 whether it belongs to runtime or to compile time. 76 06:15.160 --> 06:16.940 Doesn't matter. 77 06:17.050 --> 06:21.760 This is because behind the scenes go Comverse a civic statement into an IF statement. 78 06:22.790 --> 06:31.770 So you can use any type of expression as conditions ok for example it and rented Parus both of the statements 79 06:31.970 --> 06:36.420 in France because there are similar statements. 80 06:36.670 --> 06:37.890 So you can think of that. 81 06:37.930 --> 06:40.690 Each case corresponds to an Eve an hour. 82 06:40.720 --> 06:42.950 If branches in an IF statement. 83 06:42.970 --> 06:50.300 OK OK now let's talk a little bit about the rules of the switch statement. 84 06:50.310 --> 06:56.940 The first rule is that values inside the case conditions should be unique among the other case conditions. 85 06:57.660 --> 07:01.140 So there can be only one case condition with the same value. 86 07:01.260 --> 07:06.300 Right for example that change Tokyo to Paris like this. 87 07:07.690 --> 07:12.790 As you can see gold warns me that there are duplicate case conditions. 88 07:12.870 --> 07:17.090 All right let's take it back. 89 07:17.090 --> 07:24.170 The second rule is that the typesof to switch and the case condition expressions should be comparable. 90 07:24.280 --> 07:27.070 For example that may change these part to a number. 91 07:27.070 --> 07:34.990 For example going on says that the steamer table and this number doesn't match because the stairway 92 07:35.170 --> 07:35.980 is a string. 93 07:36.140 --> 07:43.540 However the case condition here is an int so each case condition should always match to this to each 94 07:43.570 --> 07:47.370 condition expirations type. 95 07:47.500 --> 07:54.490 It may take it back by the way for coming from other languages to go down. 96 07:54.510 --> 07:56.300 You may be wondering how this works. 97 07:56.300 --> 08:01.490 Without the break statements there is a break statement Ingo as well. 98 08:01.530 --> 08:07.300 However it is not necessary because it adds down for you automatically. 99 08:07.450 --> 08:09.010 So if we go first don't use it. 100 08:09.010 --> 08:10.230 Most of the time. 101 08:10.360 --> 08:13.100 Let me add a few break statements to this search. 102 08:16.590 --> 08:21.580 As you can see it still works but it's not necessary to add them. 103 08:21.610 --> 08:23.980 I'll show you later that they can be useful. 104 08:24.970 --> 08:29.400 And lastly let's talk about the exclusivity of case Klaus's. 105 08:29.410 --> 08:34.570 As I said each case Clouse creates its own exclusive block right. 106 08:34.720 --> 08:44.590 So for example declare a variable here V.I.P true and this printed printed and VIP trip. 107 08:44.700 --> 08:45.150 Ip. 108 08:45.250 --> 08:51.020 OK this V.I.P variable is only a label inside these case block. 109 08:51.260 --> 08:59.680 For example this duplicate this line here and move with your as you can see there is an error. 110 08:59.680 --> 09:04.100 Now go says that VIP table doesn't exist here. 111 09:05.220 --> 09:08.990 Or let's move with your as it can see. 112 09:08.990 --> 09:13.880 These kids can also see the we will write. 113 09:13.900 --> 09:19.770 So in summary case block creates its own exclusive block. 114 09:19.850 --> 09:20.660 All right. 115 09:20.660 --> 09:21.680 Thank you for watching. 116 09:21.780 --> 09:24.340 That's all for now seen the next lecture. 117 09:24.560 --> 09:24.800 Baba.