Hello and welcome to this lecture.
What are switch statements.
Switch statements are basically used to perform different actions based on different conditions.
Let's have a look at the syntax or how a switch statement is written.
So this is a basic switch syntax basically describing how to write a switch statement.
So you begin with the words switch and then you have the expression which is the condition that must
be met.
For each of these block of course so with this switch statement he can have various cases and each of
the cases has a block of code.
So if case a for example matches the expression here it will execute this code block inside the first
case.
If he doesn't if Kobe matches the expression it will execute the code in Block B and so on.
So based on any of the code blocks matching the expression the code in that case will be executed.
Notice there is a break.
Key word here.
This break.
Key word basically breaks out of the switch block block when an expression has been matched that means
the code will stop execution and none of the other cases the code in the other block of cases will execute.
So what that means basically if a match is found in either of these cases for the expression then the
code once to call has been executed.
He breaks out the switch that means it terminates a break is useful.
He can save a lot of time for execution because it ignores the execution of the rest of the code block
within the switch.
Once he's found a match it no longer needs to execute the other code blocks within the switch.
Also the last case in the switch block you don't necessarily have to put a break in that because the
block will end there anyway so there's no need to put a break but it's just good practice.
Also notice there is a default key word here.
The default key word basically is used to specify the code that will run if there is no match in the
case.
If for example if none of the cases matches the expression then you need to specify code that will run
inside the default keyword.
Also the default keyword doesn't necessarily have to be the last case in the switch.
BLOCK
I have got visual studio open so I'm going to create a new Windows application project and use that
to illustrate how a switch statement works.
So I'm going to click on the new project link here and I'll wait for the templates to load.
So these templates are loaded.
Make sure you go visual see sharp selected and also make sure we've got windows form applications selected.
So in the name here I'm going to change the default name and I'm going to just type switch and click
OK and I'll wait for Visual Studio to create the project.
So Visual Studio is created a project and given us a form as well.
So I'm just going to expand this form a little bit.
I'm going to add a button to this form.
Just going to try again button here and just make that peek a little bit
am just going to go back into the design view
I clicked on the button by clicking on the button what it did.
It generated this event handler here.
This is a stop it stop meaning it's an empty method so that we can to write our code between this curly
braces here for the button click event.
So I'm just gonna pop back into the form designer.
I just want to change the name of this so I right click properties I'll change that to the day of the
week.
Okay.
And I go back into my form designer.
Someone to start by creating a variable component is gonna be an integer and we'll call it foul.
And I'll set the value to
say 1
set of values to 1.
So let's start implementing this sweet statement.
So to begin I just type in the word switch
and then followed by the variable I've just created.
Followed by this variable.
So this is very variable is going to be the expression that will be evaluated for the switch.
So I just need to come here and pull my curly braces in between the curly braces is where I will define
the various cases for the switch statement to save time.
I've already written the statement for the switch to this.
First I started off with creating a variable.
So I created a variable called Val and gave it a value of 1.
This is where I switch step and start from.
So you start with the word switch and then you pass in the expression the expression here is a variable.
So these are the cases you can have as many cases as you want in a sweet statement.
I've got seven cases in the switch statement.
Basically this is a case of the days of the week.
So there are seven days in a week and I'm giving each day of the week a case representation.
So the variable here has a value of 1.
So I'm saying that in each of these cases the which ever case matches the expression that code will
be executed and the other code will not execute.
So here as you can see because the the value of the Variable value close to 1 the first case will be
executed with code will show the day is Sunday.
If the value was to then Monday will be the code block inside.
Case 2 which is Monday will be executed because that matches the variable.
If the variable is set to 2 So let's test that out.
Also if none of these cases matches then it will execute the code in the default block okay.
So as you can see Case 1 here we've got Sunday.
So let's execute that.
I expect the code to show the day is Sunday.
Let's run that and see give it a few minutes to load up.
So this is our button.
So if I click this button it should show Sunday C the day is Sunday.
That's it.
So the code is executed.
So if I was to change his value for example I change a value to say six.
For example he should show the day is Friday because case six will match that expression as give it
a go start click on day of the week.
As you can see he says the day is Friday.
And that's because the the expression in the switch statement here matches the case number six because
of the variable value is six.
So that's basically how a switch statement works.
So I hope the lecture has been useful if there are any questions please let me know.
Thank you.