WEBVTT 0 00:02.060 --> 00:08.150 Welcome! In the previous lectures, you've seen that "Switch" statement is very similar to an "if" statement. 1 00:08.270 --> 00:09.230 right? 2 00:09.320 --> 00:12.430 So there should be an else branch inside a switch statement. 3 00:12.440 --> 00:13.740 as well, right? 4 00:13.970 --> 00:15.510 Yes! that's right. 5 00:15.560 --> 00:16.560 Let's check it out. 6 00:19.880 --> 00:24.010 To keep it simple I removed the other case clauses from here. 7 00:24.030 --> 00:28.700 Now let's say that the user has requested an unknown city name 8 00:28.780 --> 00:33.610 So you want to display a message to the user that the program doesn't know about that city. 9 00:34.060 --> 00:38.110 Let's take a look at how to do that. first, 10 00:38.110 --> 00:42.530 I'm going to change the name of the city to something unknown like Limbo. 11 00:42.710 --> 00:47.330 You may remember this name from the inception movie great movie by the way. 12 00:47.350 --> 00:48.320 Right. 13 00:48.330 --> 00:53.030 Anyways, now I'm going to add a default clause here, like this. 14 00:53.020 --> 01:00.240 So when no case is matched to the given city and this default clause will be executed instead. OK let's 15 01:00.250 --> 01:05.840 print the message when the program can't find the city like this. OK! 16 01:05.850 --> 01:06.900 That's it. 17 01:07.000 --> 01:08.520 Now let's take a look at this. 18 01:08.510 --> 01:10.090 in the coding editor. right 19 01:14.100 --> 01:16.500 coding time as usual. 20 01:16.500 --> 01:18.600 Here it is. OK! 21 01:18.600 --> 01:28.140 First, let me write without a default clause. `go run main.go Limbo`. as you can see it prints nothing. 22 01:28.200 --> 01:29.410 when I give it Limbo. 23 01:29.820 --> 01:32.590 It's because none of the cases matched to limbo. 24 01:34.510 --> 01:35.330 That's normal. 25 01:35.590 --> 01:42.890 Now lets add the default clause to the switch. Here I'm gonna type default with an ending colon 26 01:43.220 --> 01:45.520 like the case clauses like this. 27 01:46.960 --> 01:53.100 then I am gonna print a message. fmt.Println("Where?") 28 01:53.200 --> 01:53.600 All right. 29 01:53.620 --> 01:54.610 Now let's run it again. 30 01:57.710 --> 02:04.600 as you can see this time it's prints where. here none of the cases matched to limbo. 31 02:04.630 --> 02:05.700 Right. 32 02:05.710 --> 02:08.040 There are only Paris and Tokyo. 33 02:08.320 --> 02:16.370 So since it couldn't find a matching case and this default clause is executed instead. its that simple. 34 02:16.440 --> 02:21.870 by the way, you can move the default clause to anywhere inside the switch statement like this. 35 02:22.640 --> 02:24.940 It'll work the same. 36 02:24.960 --> 02:31.940 All right, Lets try it. As you can see it prints where again. 37 02:32.110 --> 02:37.110 Now this right part is this time it brings France. 38 02:37.110 --> 02:42.330 It still works! as you can see the default clause still runs the last. 39 02:42.360 --> 02:45.390 So it's location in the switch statement doesn't matter. 40 02:45.390 --> 02:51.420 By the way, let me also tell you that the else branch in if statement there can be only one 41 02:51.420 --> 02:52.490 default clause. 42 02:52.530 --> 03:00.810 per statement. Let me type another default clause here. as you can see go warns me that there 43 03:00.810 --> 03:02.360 are multiple defaults here. 44 03:02.400 --> 03:05.550 So it doesn't allow me to do so. 45 03:05.600 --> 03:06.800 Let's take it back. 46 03:08.580 --> 03:12.200 So in summary the default clause is just like an else branch. 47 03:12.210 --> 03:18.250 in an iif statement that can be only one default clause and it will be executed if none of the cases 48 03:18.250 --> 03:21.000 match to the switch condition. 49 03:21.000 --> 03:21.490 All right. 50 03:21.510 --> 03:22.540 That's all for now. 51 03:22.710 --> 03:23.850 Thank you for watching. 52 03:23.850 --> 03:25.320 See in the next lecture, Bye Bye.