1
00:00:08,970 --> 00:00:10,942
In this lecture, we're continuing our

2
00:00:10,942 --> 00:00:13,420
discussion of the selection control
structure.

3
00:00:13,420 --> 00:00:16,870
Here are the module learning objectives
for the module.

4
00:00:20,540 --> 00:00:24,340
Last time, we talked about the different
variations of the

5
00:00:24,340 --> 00:00:28,190
if statement we could use to make
decisions in our program.

6
00:00:29,220 --> 00:00:32,436
This time, we're going to look at another
C# statement we

7
00:00:32,436 --> 00:00:36,870
can use to implement the selection control
structure in C#.

8
00:00:36,870 --> 00:00:41,120
And that statement is called the switch
statement.

9
00:00:41,120 --> 00:00:43,673
So, now you should go do an in-lecture
quiz

10
00:00:43,673 --> 00:00:46,850
to see if you paid attention to my
previous sentence.

11
00:00:52,140 --> 00:00:59,100
I have already built a project that
demonstrates the switch statement.

12
00:00:59,100 --> 00:01:02,168
Although it doesn't really do any of the
demonstration stuff

13
00:01:02,168 --> 00:01:04,730
yet, if I Ctrl+F5, do you want the shiny
thing?

14
00:01:04,730 --> 00:01:08,980
Yes, no, h, whatever, press any key.

15
00:01:08,980 --> 00:01:14,050
So, this is exactly as before, this is
exactly as before.

16
00:01:14,050 --> 00:01:16,650
This is the part that we're going to do
slightly differently.

17
00:01:17,960 --> 00:01:20,850
So the new statement we're looking at is
the switch statement.

18
00:01:20,850 --> 00:01:23,808
And the way the switch statement works is

19
00:01:23,808 --> 00:01:27,150
we type the word switch, which is a
keyword.

20
00:01:27,150 --> 00:01:32,579
And then in parenthesis we put the name of
the variable or an expression if

21
00:01:32,579 --> 00:01:38,049
we choose to.
That we want to switch on basically,

22
00:01:38,049 --> 00:01:43,930
that we want to evaluate its value and
take appropriate action.

23
00:01:43,930 --> 00:01:50,330
So, before we had answer as a key portion
of our Boolean expressions.

24
00:01:50,330 --> 00:01:56,030
And it turns that answer is exactly the
variable that we want to switch on here.

25
00:01:57,490 --> 00:01:59,965
The syntax is a little different, well

26
00:01:59,965 --> 00:02:03,550
maybe a lot different from the if
statement.

27
00:02:03,550 --> 00:02:09,221
So for the different values that the
answer variable can have,

28
00:02:09,221 --> 00:02:14,930
we add cases.
So we have case, lower case y.

29
00:02:17,700 --> 00:02:22,580
I'm going to compare this to i, and I'm
going to print that message that

30
00:02:22,580 --> 00:02:25,220
we used before, that I probably won't

31
00:02:25,220 --> 00:02:29,320
remember verbatim, but we'll remember it
close enough.

32
00:02:32,980 --> 00:02:34,900
You have the shiny thing.

33
00:02:37,650 --> 00:02:40,386
Now, it feels like we should be all the
way done here, but

34
00:02:40,386 --> 00:02:44,640
we're not quite, because as you can see,
we're getting an error message.

35
00:02:44,640 --> 00:02:50,070
And that error message says we can't fall
through from this case to other cases.

36
00:02:50,070 --> 00:02:52,744
Other programming languages let you do
that.

37
00:02:52,744 --> 00:02:56,454
And sometimes that's really useful, and
other times it leads

38
00:02:56,454 --> 00:03:00,130
to horrible bugs that take hours and hours
to find.

39
00:03:00,130 --> 00:03:02,620
So C# doesn't let us do it.

40
00:03:02,620 --> 00:03:06,320
So at the end of every case we do, we're
going to type the word, break.

41
00:03:07,770 --> 00:03:11,160
When we do that, that compilation error
goes away.

42
00:03:11,160 --> 00:03:13,932
And now, we have a form that will behave

43
00:03:13,932 --> 00:03:17,850
exactly like our first if statement worked
last time.

44
00:03:17,850 --> 00:03:24,450
When I Ctrl+F5, if I say lower case yes, I
have the shiny thing.

45
00:03:24,450 --> 00:03:26,349
If I say h,

46
00:03:26,349 --> 00:03:31,390
nothing.
And sadly, if I say

47
00:03:31,390 --> 00:03:36,698
upper case Y, I still get nothing.
So

48
00:03:36,698 --> 00:03:41,690
it turns out, that we can list multiple
cases, multiple

49
00:03:41,690 --> 00:03:46,474
values for the answer variable in this
particular case

50
00:03:46,474 --> 00:03:51,474
that will do this stuff here.
And so, if I just

51
00:03:51,474 --> 00:03:56,913
stack these values like this, then I can
check both answer

52
00:03:56,913 --> 00:04:01,870
being lower case y and the answer being
upper case y.

53
00:04:03,030 --> 00:04:08,220
And it will do this in both cases.
Let's check it to see.

54
00:04:08,220 --> 00:04:11,610
Lower case y still works the way we
expected.

55
00:04:11,610 --> 00:04:14,980
Now upper case Y also gives us the shiny
thing.

56
00:04:16,060 --> 00:04:17,284
And finally,

57
00:04:17,284 --> 00:04:23,884
we'll check that h, nothing.
So,

58
00:04:23,884 --> 00:04:28,844
we can do the

59
00:04:28,844 --> 00:04:33,308
same thing

60
00:04:33,308 --> 00:04:38,650
for no.
Where we don't have the shiny thing.

61
00:04:42,940 --> 00:04:46,720
And I pressed F5 by mistake so it flashes
right away.

62
00:04:46,720 --> 00:04:47,749
Let's try that again.

63
00:04:49,700 --> 00:04:52,550
Lower case n, don't have the shiny thing.
Upper case n, don't have the shiny thing.

64
00:04:58,200 --> 00:05:04,880
Still showing you that y works in both
cases.

65
00:05:06,480 --> 00:05:09,750
And, of course, h still doesn't give us
anything.

66
00:05:09,750 --> 00:05:12,870
Now just to, so this sort of works in

67
00:05:12,870 --> 00:05:16,930
a similar way to the if, else if, else
thing.

68
00:05:17,940 --> 00:05:23,972
Where it figures out which of these things
matches, if you will, and

69
00:05:23,972 --> 00:05:27,750
does the appropriate stuff inside.

70
00:05:27,750 --> 00:05:33,376
I'll point out that I cannot down here
decide, oh I want to do another

71
00:05:33,376 --> 00:05:38,840
thing with lower case y, because that has
already been included.

72
00:05:38,840 --> 00:05:43,540
We've already used that value in a case,
so we can't use it again.

73
00:05:44,840 --> 00:05:48,980
You might wonder though, how we can go
about doing that

74
00:05:48,980 --> 00:05:50,603
your a noob message.

75
00:05:50,603 --> 00:05:57,041
Because we sure don't want to have to say,
case a, case, upper case

76
00:05:57,041 --> 00:06:04,060
A, case b, all of the other possible
characters that are not y or n.

77
00:06:04,060 --> 00:06:06,044
What we really want is something that

78
00:06:06,044 --> 00:06:09,040
corresponds to the else in our if
statement.

79
00:06:09,040 --> 00:06:12,040
And luckily, we get such a thing in the
switch statement.

80
00:06:13,480 --> 00:06:14,155
We type the

81
00:06:14,155 --> 00:06:15,830
word default.

82
00:06:15,830 --> 00:06:21,930
Which really means, if none of these other
things match, then do this instead.

83
00:06:23,140 --> 00:06:26,170
And here's where we can do our new
message.

84
00:06:30,210 --> 00:06:35,046
And, we're still going to have to add a
break, because we add a break

85
00:06:35,046 --> 00:06:39,740
at the end of every clause we include in a
switch statement.

86
00:06:39,740 --> 00:06:45,330
And now, when I Ctrl+F5, I'll still check
y.

87
00:06:47,460 --> 00:06:54,820
I'll still check n.
And I'll try h where I get my new message.

88
00:06:57,530 --> 00:07:04,005
So, you might wonder why we care?
If we already

89
00:07:04,005 --> 00:07:10,210
have this if else, if else structure, why
would we want a switch statement instead?

90
00:07:10,210 --> 00:07:14,123
And it turns out that I covered it here
because it's

91
00:07:14,123 --> 00:07:18,439
sort of the other statement we use for
selection in C#.

92
00:07:19,680 --> 00:07:22,536
We find it to be most useful in our game
development

93
00:07:22,536 --> 00:07:25,660
when we're using something called an
enumeration.

94
00:07:26,890 --> 00:07:32,130
An enumeration is basically a data type
that has specific values.

95
00:07:32,130 --> 00:07:32,920
Don't worry.

96
00:07:32,920 --> 00:07:35,824
We'll look at enumerations and see how
they all work

97
00:07:35,824 --> 00:07:38,990
so, so no worries that we haven't talked
about it yet.

98
00:07:38,990 --> 00:07:43,443
But we'll discover that when we're using
an enumeration, we find that

99
00:07:43,443 --> 00:07:48,890
switch statements tend to seem a little
more intuitive as we implement them.

100
00:07:48,890 --> 00:07:52,548
I'm going to have you, if you do the
optional lab, I'm going to have you do

101
00:07:52,548 --> 00:07:55,090
some practice with both if statements and
switch

102
00:07:55,090 --> 00:07:58,614
statements, because they're both useful
tools for selection.

103
00:07:58,614 --> 00:08:04,010
And you'll pick the most appropriate one
as you continue on with your programming.

104
00:08:04,010 --> 00:08:09,042
So next time, we will start talking what,
we've talked about two different C#

105
00:08:09,042 --> 00:08:14,270
statements.
Next time we'll show using selection

106
00:08:14,270 --> 00:08:17,200
in actual game situations.