1 00:00:00,120 --> 00:00:03,090 ‫-: Hi. Within this lecture we are going to 2 00:00:03,090 --> 00:00:05,730 ‫learn a new structure called Switch. 3 00:00:05,730 --> 00:00:09,840 ‫And this is basically the same thing with 'if' statements, 4 00:00:09,840 --> 00:00:12,960 ‫but it's a little bit more clear, 5 00:00:12,960 --> 00:00:15,990 ‫when it comes to 'as if' statements. 6 00:00:15,990 --> 00:00:19,362 ‫So if you have a lot of 'as if' statements 7 00:00:19,362 --> 00:00:23,700 ‫in a given condition in a given 'if' control 8 00:00:23,700 --> 00:00:26,850 ‫then you may want to check out Switch. 9 00:00:26,850 --> 00:00:29,910 ‫So best way to explain this is with an example. 10 00:00:29,910 --> 00:00:33,720 ‫So I'm gonna create a 'day' into the row here 11 00:00:33,720 --> 00:00:36,390 ‫and day will be '1' at first. 12 00:00:36,390 --> 00:00:40,260 ‫So I'm gonna create another day string 13 00:00:40,260 --> 00:00:41,850 ‫and I'm going to check for those. 14 00:00:41,850 --> 00:00:45,150 ‫If day is '1', then I'm gonna print out 'Monday'. 15 00:00:45,150 --> 00:00:48,570 ‫If day is '2', then I'm gonna print out 'Tuesday'. 16 00:00:48,570 --> 00:00:51,480 ‫Okay? So let me create a new day string 17 00:00:51,480 --> 00:00:54,270 ‫over here and this will be empty at first 18 00:00:54,270 --> 00:00:59,270 ‫like an empty string with only two double quotation marks. 19 00:00:59,520 --> 00:01:03,480 ‫And then later on I suggest you pause the video 20 00:01:03,480 --> 00:01:06,540 ‫and try to do this with 'if' statements. 21 00:01:06,540 --> 00:01:09,300 ‫Okay? Because it's fairly easy. 22 00:01:09,300 --> 00:01:14,300 ‫If day is '1', then day string will be 'Monday'. 23 00:01:14,790 --> 00:01:19,790 ‫Okay? And I'm going to print out this day string later on. 24 00:01:20,190 --> 00:01:23,730 ‫We shouldn't bother printing out at this point. 25 00:01:23,730 --> 00:01:28,350 ‫We can do it at the end of the 'if' control like this. 26 00:01:28,350 --> 00:01:33,350 ‫Else if day is '2', day string would be Tuesday, okay? 27 00:01:33,720 --> 00:01:36,870 ‫And else if day is '3', 28 00:01:36,870 --> 00:01:40,827 ‫the day string will be something like 'Wednesday.' 29 00:01:41,730 --> 00:01:46,200 ‫Okay? And I'm not gonna do the whole thing over here. 30 00:01:46,200 --> 00:01:50,340 ‫I'm gonna skip Thursday, Friday, Saturday. 31 00:01:50,340 --> 00:01:54,750 ‫Maybe we can just go for a Sunday with an 'else'. Okay? 32 00:01:54,750 --> 00:01:59,490 ‫So maybe we forgot about the day '4', '5', and '6' 33 00:01:59,490 --> 00:02:04,490 ‫and we just go directly to this Sunday over here. 34 00:02:05,190 --> 00:02:09,420 ‫Of course you can do the whole thing in your own example 35 00:02:09,420 --> 00:02:11,430 ‫but I'm not going to do that 36 00:02:11,430 --> 00:02:14,550 ‫in order not to make it way too much. 37 00:02:14,550 --> 00:02:17,190 ‫But this should work fine. 38 00:02:17,190 --> 00:02:18,810 ‫Let me run this and here you go. 39 00:02:18,810 --> 00:02:21,697 ‫We see the 'Monday', let me change it to '2' 40 00:02:21,697 --> 00:02:25,470 ‫and we're gonna see Tuesday, right? 41 00:02:25,470 --> 00:02:26,303 ‫Here you go. 42 00:02:26,303 --> 00:02:27,243 ‫We see the Tuesday. 43 00:02:28,650 --> 00:02:33,650 ‫Great. Now I'm gonna do the same thing but with 'switch.' 44 00:02:34,860 --> 00:02:39,090 ‫So sometimes it might look a little bit more 45 00:02:39,090 --> 00:02:42,817 ‫complicated even to you then maybe you don't 46 00:02:42,817 --> 00:02:47,100 ‫prefer using Switch, but sometimes people find it much 47 00:02:47,100 --> 00:02:48,060 ‫more easier. 48 00:02:48,060 --> 00:02:50,280 ‫So I'm going to show you anyway. 49 00:02:50,280 --> 00:02:53,460 ‫So you write 'switch' and you write the value 50 00:02:53,460 --> 00:02:56,190 ‫that you may want to check for. 51 00:02:56,190 --> 00:02:58,520 ‫Okay, we are gonna check for day as 52 00:02:58,520 --> 00:03:01,440 ‫we we did in the 'if' statement. 53 00:03:01,440 --> 00:03:05,943 ‫So if day is one, then we are gonna do something. 54 00:03:06,780 --> 00:03:09,445 ‫And over here we use 'case' 55 00:03:09,445 --> 00:03:11,070 ‫keyword, 56 00:03:11,070 --> 00:03:12,840 ‫if case is 57 00:03:12,840 --> 00:03:13,673 ‫'1'. 58 00:03:14,520 --> 00:03:19,470 ‫So 'case 1' means if day is '1' in switch 59 00:03:19,470 --> 00:03:23,220 ‫and rather than same column, we use a column over here 60 00:03:23,220 --> 00:03:27,140 ‫and we don't even use a curly brace and this is one 61 00:03:27,140 --> 00:03:31,590 ‫of the unique places that we use column and just go 62 00:03:31,590 --> 00:03:36,590 ‫to the next line and write whatever we want like this. 63 00:03:36,840 --> 00:03:39,390 ‫And we are writing break over here, 64 00:03:39,390 --> 00:03:42,000 ‫it means that we are done over here. 65 00:03:42,000 --> 00:03:44,850 ‫Just break out of the switch loop 66 00:03:44,850 --> 00:03:47,670 ‫because we got what we want, right? 67 00:03:47,670 --> 00:03:50,820 ‫If case is '1', we are gonna change the day string 68 00:03:50,820 --> 00:03:55,020 ‫to Monday and we're gonna break out of this. 69 00:03:55,020 --> 00:03:57,990 ‫So this is how you work with Switch. 70 00:03:57,990 --> 00:04:00,600 ‫We can just go along and say 71 00:04:00,600 --> 00:04:03,355 ‫case '2' day string is Tuesday. 72 00:04:03,355 --> 00:04:07,350 ‫Okay? And we're gonna break again. 73 00:04:07,350 --> 00:04:09,840 ‫So we can go for case three. 74 00:04:09,840 --> 00:04:13,620 ‫As you can see, it's a little bit more clear 75 00:04:13,620 --> 00:04:16,470 ‫on the code when you first look into it 76 00:04:16,470 --> 00:04:19,890 ‫like cases and it looks better. 77 00:04:19,890 --> 00:04:24,330 ‫But sometimes it can be much more complicated 78 00:04:24,330 --> 00:04:27,930 ‫than a simple 'if' and 'as if' control statement. 79 00:04:27,930 --> 00:04:30,150 ‫So if you didn't like it, just don't use it, 80 00:04:30,150 --> 00:04:33,363 ‫but if you see one, then don't be surprised. 81 00:04:34,320 --> 00:04:35,659 ‫So over here, 82 00:04:35,659 --> 00:04:38,970 ‫we did the case '1', '2', '3'. 83 00:04:38,970 --> 00:04:43,970 ‫And also there is a case of default in the switch as well. 84 00:04:44,190 --> 00:04:47,248 ‫Default means if none of this holds 85 00:04:47,248 --> 00:04:51,420 ‫then, by default the value will be like this. 86 00:04:51,420 --> 00:04:53,400 ‫For example, we can just make it equal 87 00:04:53,400 --> 00:04:57,390 ‫to 'Sunday', it will be exactly the same like we did 88 00:04:57,390 --> 00:04:59,130 ‫in the 'if' control, right? 89 00:04:59,130 --> 00:05:02,100 ‫And we did the same thing with 'else.' 90 00:05:02,100 --> 00:05:04,650 ‫So over here we are doing the same thing 91 00:05:04,650 --> 00:05:07,050 ‫exactly the same thing actually. 92 00:05:07,050 --> 00:05:09,720 ‫If day is '1', we are gonna make it print 'Monday'. 93 00:05:09,720 --> 00:05:12,720 ‫If day is '2', we are gonna make it print Tuesday 94 00:05:12,720 --> 00:05:14,100 ‫and 'Wednesday'. 95 00:05:14,100 --> 00:05:17,910 ‫And if none of the conditions hold, then we're gonna make it 96 00:05:17,910 --> 00:05:21,507 ‫even equal to actually 'Sunday.' 97 00:05:22,620 --> 00:05:24,210 ‫So this is cool. 98 00:05:24,210 --> 00:05:27,450 ‫So maybe we may want to delete everything in the 99 00:05:27,450 --> 00:05:32,430 ‫'if' control right now and then we can just control this 100 00:05:32,430 --> 00:05:36,150 ‫around this, execute this with switch control so 101 00:05:36,150 --> 00:05:39,573 ‫that we can see if we get the same result or not. 102 00:05:40,500 --> 00:05:42,300 ‫So I don't wanna delete this. 103 00:05:42,300 --> 00:05:47,300 ‫So you can get, and you can just get the access from GitHub. 104 00:05:47,550 --> 00:05:50,520 ‫I'm going to comment this out. 105 00:05:50,520 --> 00:05:52,710 ‫So remember how we comment things out, 106 00:05:52,710 --> 00:05:55,530 ‫how we write comments, we put double slashes, 107 00:05:55,530 --> 00:05:58,470 ‫but there are so many lines over here. 108 00:05:58,470 --> 00:06:01,560 ‫So actually there is a better way to do this 109 00:06:01,560 --> 00:06:03,330 ‫with multiple lines. 110 00:06:03,330 --> 00:06:06,840 ‫You put a '/' and a '*' and you put a '*' 111 00:06:06,840 --> 00:06:11,523 ‫and a '/' whenever you may want to end this. 112 00:06:12,690 --> 00:06:17,490 ‫So between those slash and starts, it's all commented out 113 00:06:17,490 --> 00:06:20,970 ‫and it won't get executed once we run it. 114 00:06:20,970 --> 00:06:22,890 ‫So now day is '2' 115 00:06:22,890 --> 00:06:26,460 ‫and I'm executing this and I'm getting 'Tuesday'. 116 00:06:26,460 --> 00:06:29,190 ‫Thanks to the switch that we have written. 117 00:06:29,190 --> 00:06:32,040 ‫Let's try with '3', we are gonna get 'Wednesday'. 118 00:06:32,040 --> 00:06:34,350 ‫Here you go, it works. 119 00:06:34,350 --> 00:06:36,270 ‫So this is how we work 120 00:06:36,270 --> 00:06:40,487 ‫with Switch and this is how we work with 'as if', 121 00:06:40,487 --> 00:06:45,030 ‫and right now, you know how to work with both. 122 00:06:45,030 --> 00:06:47,970 ‫You may choose your own way. 123 00:06:47,970 --> 00:06:51,243 ‫Let's stop here and continue within the next lecture.