1
00:00:00,860 --> 00:00:07,770
Kind of again in this list then we're going to talk about switch switches essentially another control

2
00:00:07,770 --> 00:00:14,360
flow command either helps us with organizing our nested if else statements.

3
00:00:14,400 --> 00:00:16,050
Let's see how we do that.

4
00:00:16,140 --> 00:00:20,650
The school had to exclude in school I'm going to close this.

5
00:00:20,790 --> 00:00:29,470
Start your project command line tool call it sewage project and then my switch project.

6
00:00:29,740 --> 00:00:36,770
The first thing I'm going to do I'm going to show you what is the problem you're trying to are to answer

7
00:00:36,790 --> 00:00:39,480
essentially using a switch statement.

8
00:00:39,580 --> 00:00:41,460
Imagine a situation like this.

9
00:00:41,470 --> 00:00:47,030
Imagine if you had an integer card my ends which is currently a value such as for.

10
00:00:47,200 --> 00:00:52,340
Of course as you know I'm putting a static ideas in here with in real life application.

11
00:00:52,390 --> 00:00:56,160
This idea would be fed to us from another resource.

12
00:00:56,170 --> 00:01:01,770
Now imagine I want to find the what is this likely one based on that do something.

13
00:01:01,780 --> 00:01:05,030
So if it is what I want to do something for this one.

14
00:01:05,050 --> 00:01:13,660
One other thing if this so on and so forth it would become something like this if my Int is zero then

15
00:01:13,750 --> 00:01:16,240
you do something else.

16
00:01:16,330 --> 00:01:29,110
If my end is one then obviously you do something else and then else if my end is I'm not to then the

17
00:01:29,110 --> 00:01:31,390
compiler had to turn something else.

18
00:01:31,390 --> 00:01:39,250
And as you can imagine this list of if else if else if I was keeping freezing depending on my situation

19
00:01:40,000 --> 00:01:44,800
why there's nothing wrong about coding like this there is a much better structure and that's called

20
00:01:44,860 --> 00:01:45,870
a switch.

21
00:01:45,880 --> 00:01:52,510
The switch works based on different cases and so you provide an expression that is your value and then

22
00:01:52,510 --> 00:01:57,540
you provided some cases and based on each case you decide about what you want to do.

23
00:01:57,790 --> 00:02:01,450
So let's come this into a switch statement.

24
00:02:01,450 --> 00:02:06,830
Let me first write a switch on the auto completion gives us some values in here.

25
00:02:07,030 --> 00:02:12,990
It shows us that for each unit to provide me an expression and then in the body of the switch you have

26
00:02:12,990 --> 00:02:16,570
to give me the case statements you want to run on break.

27
00:02:16,780 --> 00:02:19,230
So let's see how that actually works.

28
00:02:19,270 --> 00:02:25,750
I'm going to say the statement the expression that I want to switch is my in the case for it is if it

29
00:02:25,750 --> 00:02:36,310
was a 1 then and this log in it was one for instance then I'm going to say that's one case and then

30
00:02:36,310 --> 00:02:37,850
I have another case.

31
00:02:38,320 --> 00:02:46,990
And then in that case if case was to then say two and then break and then I'm going to say another case

32
00:02:47,080 --> 00:02:54,070
if it was three then right three and me read in then everything nice and tidy.

33
00:02:54,370 --> 00:03:00,630
So as you can see the writing of it is not necessarily shorter than writing it.

34
00:03:00,700 --> 00:03:06,790
If a state has a statement or an if else statement but it's a little bit more readable because you can

35
00:03:06,790 --> 00:03:13,710
see that in case it was one do this thing in case it was to do with the other things so on and so forth.

36
00:03:13,780 --> 00:03:20,070
And of course you need to have a default value that happens if none of them was actually valid actually

37
00:03:20,080 --> 00:03:27,420
going to add that the number say in the default say log on none of the above.

38
00:03:27,940 --> 00:03:30,150
None of them was actually correct.

39
00:03:30,190 --> 00:03:34,870
So that's essentially the structure of a switch is not very difficult.

40
00:03:34,880 --> 00:03:41,590
And you know once you get used to it you can use it for situations when your if else statement has multiple

41
00:03:41,590 --> 00:03:42,880
different situations.

42
00:03:43,030 --> 00:03:49,630
So you will need if else if and for that I would suggest that you use switch and other thing that I

43
00:03:49,630 --> 00:03:54,640
want to mention that is in it you can actually combine some of your cases.

44
00:03:54,640 --> 00:04:01,520
So for instance if you need a case of one and two to be considered the same you can actually do this

45
00:04:01,540 --> 00:04:08,240
you can say case one case two it is or it was one or two.

46
00:04:08,410 --> 00:04:13,730
So right now most of these cases they refer to this part of the court.

47
00:04:13,930 --> 00:04:19,720
And then of course there's a break after it which means you get out of the switch because of this system

48
00:04:19,720 --> 00:04:22,610
where the case gets compiled and calculate that.

49
00:04:22,620 --> 00:04:29,110
And that break happens after it switches off often more efficient than using an if else statement.

50
00:04:29,170 --> 00:04:35,530
If you have reach millions of entries or if else with millions of entries then switch would work a little

51
00:04:35,530 --> 00:04:41,890
bit faster in saying that that's really left up to your judgment depending on what you're doing and

52
00:04:41,890 --> 00:04:45,670
whether you feel like you know if is a better choice for there.

53
00:04:45,760 --> 00:04:47,510
It's an easier quote to right.

54
00:04:47,530 --> 00:04:50,680
Or whether a switch would be a better option.

55
00:04:50,680 --> 00:04:58,090
Now one viewer here I should mention something that switches often constrained that we use to be used

56
00:04:58,210 --> 00:05:00,180
with numerical values.

57
00:05:00,250 --> 00:05:07,930
For instance if an if statement you could do something like this in if else you could say if I don't

58
00:05:07,930 --> 00:05:15,520
know all my name is equal to a string by name.

59
00:05:15,520 --> 00:05:16,870
Exactly.

60
00:05:16,920 --> 00:05:24,190
Then to do something which means in the IF expression we are actually comparing two values of the type

61
00:05:24,270 --> 00:05:25,550
in a string.

62
00:05:25,840 --> 00:05:29,840
Unfortunately this switch doesn't provide much infrastructure.

63
00:05:29,850 --> 00:05:33,480
You cannot do something like this you cannot say switch.

64
00:05:33,810 --> 00:05:42,780
And then you need say a string and then for the cases how the strings again because switch doesn't accept

65
00:05:42,870 --> 00:05:44,970
and as a string as a value.

66
00:05:45,300 --> 00:05:51,880
So something you could do in here is this which is not really a very you know modern way of doing it

67
00:05:51,920 --> 00:05:54,670
but they found out that sometimes they actually help me.

68
00:05:54,900 --> 00:05:57,090
And here is how I would do it.

69
00:05:57,090 --> 00:06:03,330
Imagine I have a string and that this thing has to be compare it against the names.

70
00:06:03,330 --> 00:06:07,640
Vancouver San Francisco New York.

71
00:06:07,800 --> 00:06:09,940
And I don't know maybe.

72
00:06:10,170 --> 00:06:12,210
The other is Paris for instance.

73
00:06:12,450 --> 00:06:16,670
So if I have to compare against this I put all of these within an array.

74
00:06:16,800 --> 00:06:26,270
So I'm going to go ahead and say and this array or the strings array that is in this array and that's

75
00:06:26,310 --> 00:06:27,560
all right.

76
00:06:27,560 --> 00:06:36,750
I can't initialize with objects the objects aren't going to be in Vancouver and the other object is

77
00:06:36,750 --> 00:06:41,410
going to be this code.

78
00:06:41,430 --> 00:06:47,140
If that's how you type it I always have difficulty typing some something something I'm really good at.

79
00:06:47,150 --> 00:06:52,520
And some others are in New York and then Paris.

80
00:06:52,910 --> 00:06:53,450
OK.

81
00:06:53,520 --> 00:06:56,470
So that's about that.

82
00:06:56,790 --> 00:06:59,010
I need one more practice of course.

83
00:06:59,080 --> 00:07:07,550
Give me a stack of them so we can see them nice and neat and proper So now we want to switch a certain

84
00:07:07,550 --> 00:07:08,070
value.

85
00:07:08,090 --> 00:07:11,870
Imagine my value as a string.

86
00:07:11,930 --> 00:07:17,810
My strength is going to be let's say you are.

87
00:07:18,470 --> 00:07:23,200
So we want to know if money is streambed is indoors.

88
00:07:23,240 --> 00:07:25,100
We want to find out.

89
00:07:25,200 --> 00:07:30,310
We want to use a switch for that if as statement here is something you could do.

90
00:07:30,410 --> 00:07:33,040
You could say switch in the switch.

91
00:07:33,080 --> 00:07:35,300
You need to provide an integer.

92
00:07:35,330 --> 00:07:44,720
So I'm going to say integer my code is my integer let's call it city index.

93
00:07:44,720 --> 00:07:51,780
That's going to be s t r array index of objects.

94
00:07:52,100 --> 00:07:53,730
My A C R.

95
00:07:54,260 --> 00:08:00,460
So I'm saying go to these things array and find what is the Index of this guy.

96
00:08:00,470 --> 00:08:03,930
What's that index of New York in here.

97
00:08:04,040 --> 00:08:07,890
So it's going to go 0 1 2 and he's going to find two.

98
00:08:08,120 --> 00:08:15,470
So I'm going to say switch city index and then chase zero.

99
00:08:15,680 --> 00:08:24,950
If it was under zero then I'd say this like it was first for instance or whatever that you wanted to

100
00:08:24,950 --> 00:08:29,720
do for you another case would be case 1 for instance.

101
00:08:30,980 --> 00:08:35,890
And yet I'm to say it was soccer and so on and so forth.

102
00:08:36,010 --> 00:08:40,500
The only warning we have in here and I think we have seen this in the past that this is an integer.

103
00:08:40,510 --> 00:08:42,770
This is a long unsigned.

104
00:08:42,970 --> 00:08:46,470
So I'm going to say Forsett cast it into an integer.

105
00:08:46,570 --> 00:08:52,690
So this would be another way for switching between a string ralliers of course is another very very

106
00:08:52,690 --> 00:08:53,690
common practice.

107
00:08:53,710 --> 00:08:58,480
But you know if you were wondering is it at all possible to switch string.

108
00:08:58,660 --> 00:09:05,020
Yes you can pull all of your values inside an array and find the index of your object the one you're

109
00:09:05,020 --> 00:09:06,650
searching for in there.

110
00:09:06,720 --> 00:09:08,710
And as a result you can actually use it.

111
00:09:08,740 --> 00:09:16,100
So it is kind of a trick because simply again switches using an integer but kind of gets the work done.

112
00:09:16,110 --> 00:09:17,910
So I'd be happy with that.

113
00:09:18,190 --> 00:09:25,470
So a quick recap of everything switches used in a sort of a nested series of if else statements switch

114
00:09:25,490 --> 00:09:31,660
can switch different values expressions and for each of them it provides a case for each of the cases

115
00:09:31,660 --> 00:09:37,720
you decide about what is going to happen and you'll break out of your switch and you can combine different

116
00:09:37,720 --> 00:09:39,040
cases.

117
00:09:39,040 --> 00:09:40,280
That's about that.

118
00:09:40,460 --> 00:09:42,910
And I shall see you guys in the next lesson.
