1
00:00:00,630 --> 00:00:02,850
Hello and welcome to this lecture.

2
00:00:03,960 --> 00:00:06,510
What are switch statements.

3
00:00:06,510 --> 00:00:18,610
Switch statements are basically used to perform different actions based on different conditions.

4
00:00:18,660 --> 00:00:25,240
Let's have a look at the syntax or how a switch statement is written.

5
00:00:25,740 --> 00:00:34,080
So this is a basic switch syntax basically describing how to write a switch statement.

6
00:00:34,260 --> 00:00:43,050
So you begin with the words switch and then you have the expression which is the condition that must

7
00:00:43,050 --> 00:00:44,030
be met.

8
00:00:44,290 --> 00:00:52,740
For each of these block of course so with this switch statement he can have various cases and each of

9
00:00:52,740 --> 00:00:55,530
the cases has a block of code.

10
00:00:55,680 --> 00:01:06,690
So if case a for example matches the expression here it will execute this code block inside the first

11
00:01:06,690 --> 00:01:07,480
case.

12
00:01:07,770 --> 00:01:16,320
If he doesn't if Kobe matches the expression it will execute the code in Block B and so on.

13
00:01:18,120 --> 00:01:29,390
So based on any of the code blocks matching the expression the code in that case will be executed.

14
00:01:29,460 --> 00:01:31,390
Notice there is a break.

15
00:01:31,610 --> 00:01:32,910
Key word here.

16
00:01:33,030 --> 00:01:34,370
This break.

17
00:01:34,440 --> 00:01:43,200
Key word basically breaks out of the switch block block when an expression has been matched that means

18
00:01:43,740 --> 00:01:51,990
the code will stop execution and none of the other cases the code in the other block of cases will execute.

19
00:01:53,070 --> 00:02:01,590
So what that means basically if a match is found in either of these cases for the expression then the

20
00:02:01,590 --> 00:02:03,980
code once to call has been executed.

21
00:02:04,020 --> 00:02:10,500
He breaks out the switch that means it terminates a break is useful.

22
00:02:10,500 --> 00:02:19,170
He can save a lot of time for execution because it ignores the execution of the rest of the code block

23
00:02:19,380 --> 00:02:20,400
within the switch.

24
00:02:20,400 --> 00:02:30,420
Once he's found a match it no longer needs to execute the other code blocks within the switch.

25
00:02:30,420 --> 00:02:37,890
Also the last case in the switch block you don't necessarily have to put a break in that because the

26
00:02:37,890 --> 00:02:45,570
block will end there anyway so there's no need to put a break but it's just good practice.

27
00:02:45,570 --> 00:02:49,860
Also notice there is a default key word here.

28
00:02:49,860 --> 00:02:59,190
The default key word basically is used to specify the code that will run if there is no match in the

29
00:02:59,190 --> 00:02:59,670
case.

30
00:02:59,670 --> 00:03:07,830
If for example if none of the cases matches the expression then you need to specify code that will run

31
00:03:08,220 --> 00:03:12,260
inside the default keyword.

32
00:03:12,270 --> 00:03:19,890
Also the default keyword doesn't necessarily have to be the last case in the switch.

33
00:03:19,890 --> 00:03:20,310
BLOCK

34
00:03:23,390 --> 00:03:30,920
I have got visual studio open so I'm going to create a new Windows application project and use that

35
00:03:30,920 --> 00:03:34,980
to illustrate how a switch statement works.

36
00:03:35,030 --> 00:03:41,430
So I'm going to click on the new project link here and I'll wait for the templates to load.

37
00:03:41,430 --> 00:03:43,460
So these templates are loaded.

38
00:03:43,550 --> 00:03:51,470
Make sure you go visual see sharp selected and also make sure we've got windows form applications selected.

39
00:03:51,470 --> 00:04:00,780
So in the name here I'm going to change the default name and I'm going to just type switch and click

40
00:04:01,720 --> 00:04:10,740
OK and I'll wait for Visual Studio to create the project.

41
00:04:11,510 --> 00:04:16,970
So Visual Studio is created a project and given us a form as well.

42
00:04:16,970 --> 00:04:20,180
So I'm just going to expand this form a little bit.

43
00:04:20,240 --> 00:04:23,270
I'm going to add a button to this form.

44
00:04:23,270 --> 00:04:29,160
Just going to try again button here and just make that peek a little bit

45
00:04:36,520 --> 00:04:40,510
am just going to go back into the design view

46
00:04:43,120 --> 00:04:47,680
I clicked on the button by clicking on the button what it did.

47
00:04:47,920 --> 00:04:52,570
It generated this event handler here.

48
00:04:52,690 --> 00:05:00,970
This is a stop it stop meaning it's an empty method so that we can to write our code between this curly

49
00:05:01,390 --> 00:05:05,390
braces here for the button click event.

50
00:05:05,710 --> 00:05:08,700
So I'm just gonna pop back into the form designer.

51
00:05:08,700 --> 00:05:21,240
I just want to change the name of this so I right click properties I'll change that to the day of the

52
00:05:21,240 --> 00:05:21,690
week.

53
00:05:29,570 --> 00:05:30,070
Okay.

54
00:05:30,700 --> 00:05:34,450
And I go back into my form designer.

55
00:05:35,000 --> 00:05:47,470
Someone to start by creating a variable component is gonna be an integer and we'll call it foul.

56
00:05:48,470 --> 00:05:51,410
And I'll set the value to

57
00:05:53,630 --> 00:05:55,010
say 1

58
00:06:00,840 --> 00:06:03,680
set of values to 1.

59
00:06:04,860 --> 00:06:07,660
So let's start implementing this sweet statement.

60
00:06:07,740 --> 00:06:10,350
So to begin I just type in the word switch

61
00:06:18,620 --> 00:06:24,240
and then followed by the variable I've just created.

62
00:06:24,240 --> 00:06:25,920
Followed by this variable.

63
00:06:25,920 --> 00:06:33,630
So this is very variable is going to be the expression that will be evaluated for the switch.

64
00:06:33,630 --> 00:06:41,970
So I just need to come here and pull my curly braces in between the curly braces is where I will define

65
00:06:42,000 --> 00:06:49,290
the various cases for the switch statement to save time.

66
00:06:49,290 --> 00:06:56,070
I've already written the statement for the switch to this.

67
00:06:56,070 --> 00:07:00,050
First I started off with creating a variable.

68
00:07:00,040 --> 00:07:05,250
So I created a variable called Val and gave it a value of 1.

69
00:07:05,260 --> 00:07:08,160
This is where I switch step and start from.

70
00:07:08,160 --> 00:07:15,130
So you start with the word switch and then you pass in the expression the expression here is a variable.

71
00:07:15,180 --> 00:07:20,320
So these are the cases you can have as many cases as you want in a sweet statement.

72
00:07:20,340 --> 00:07:23,460
I've got seven cases in the switch statement.

73
00:07:23,460 --> 00:07:29,670
Basically this is a case of the days of the week.

74
00:07:29,790 --> 00:07:38,490
So there are seven days in a week and I'm giving each day of the week a case representation.

75
00:07:38,880 --> 00:07:43,050
So the variable here has a value of 1.

76
00:07:43,140 --> 00:07:53,550
So I'm saying that in each of these cases the which ever case matches the expression that code will

77
00:07:53,550 --> 00:07:58,320
be executed and the other code will not execute.

78
00:07:58,980 --> 00:08:08,280
So here as you can see because the the value of the Variable value close to 1 the first case will be

79
00:08:08,280 --> 00:08:15,080
executed with code will show the day is Sunday.

80
00:08:15,330 --> 00:08:21,570
If the value was to then Monday will be the code block inside.

81
00:08:21,570 --> 00:08:26,700
Case 2 which is Monday will be executed because that matches the variable.

82
00:08:26,700 --> 00:08:32,010
If the variable is set to 2 So let's test that out.

83
00:08:32,160 --> 00:08:41,060
Also if none of these cases matches then it will execute the code in the default block okay.

84
00:08:41,070 --> 00:08:45,370
So as you can see Case 1 here we've got Sunday.

85
00:08:45,660 --> 00:08:46,900
So let's execute that.

86
00:08:46,980 --> 00:08:50,230
I expect the code to show the day is Sunday.

87
00:08:51,090 --> 00:08:59,470
Let's run that and see give it a few minutes to load up.

88
00:09:01,260 --> 00:09:02,930
So this is our button.

89
00:09:03,040 --> 00:09:13,600
So if I click this button it should show Sunday C the day is Sunday.

90
00:09:13,660 --> 00:09:14,140
That's it.

91
00:09:14,260 --> 00:09:16,270
So the code is executed.

92
00:09:16,780 --> 00:09:25,310
So if I was to change his value for example I change a value to say six.

93
00:09:25,450 --> 00:09:35,950
For example he should show the day is Friday because case six will match that expression as give it

94
00:09:35,950 --> 00:09:40,540
a go start click on day of the week.

95
00:09:40,560 --> 00:09:43,250
As you can see he says the day is Friday.

96
00:09:43,530 --> 00:09:54,390
And that's because the the expression in the switch statement here matches the case number six because

97
00:09:54,390 --> 00:09:57,710
of the variable value is six.

98
00:09:57,720 --> 00:10:02,350
So that's basically how a switch statement works.

99
00:10:02,370 --> 00:10:08,000
So I hope the lecture has been useful if there are any questions please let me know.

100
00:10:08,610 --> 00:10:09,090
Thank you.
