Hello and welcome.
In this lecture we are going to create the method for the equals two button.
So it will capture all the events that happen when the equal to button is clicked so less create these
color code to do that.
You click on the equals two button.
And here in the properties click on that and where you've got the click event just double click.
And it should open up the skeletal code.
So this is the method.
And in between these parentheses here is where we will write our code that will respond to the equals
two button when it is clicked because the equals two button will be used in combination with the operators.
We are going to use what is called a switch statement the switch statement basically is used to perform
different actions based on different conditions.
All right.
So what happens is that when you use a switch statement you select a block of code to be executed based
on certain conditions.
The best way to illustrate this that I'm going to just write a piece of code and I will run through
it with you.
All right.
I have added a chunk of code here this code highlighted here.
And this is basically what is known as a switch statement.
So a switch statement is used to perform different actions based on different condition.
So here when you are writing a switch statement you begin with the word switch and then in inside the
parentheses you have to pass in an expression that will be evaluated once.
All right.
So this what have passed in here is a variable that was created here.
This string here called operate all clicked.
So that is a variable that is the expression this switch is is going to express is going to see what
happens a wrong the code goes true once and it checks this to check which of the operators have been
clicked.
So each of the operators are represented as cases.
So this is case one if for example someone clicks on the does a mathematical addition.
And after that they click on the equals to sign this.
It will references operator or clicked variable and the case will be plus for example with the operator
on perform is a plus it will execute this code.
This result box dot text is the variable is the result box where the answers are displayed for the calculate
and we'll set that to equals to the resulting value variable plus this double here we used in the past
to convert the text from the result box into a value that it understands.
And then we are now.
Then we convert and after that's been converted we then converted into a string using the two string
method.
So this is case 1.
So if the operator that is click is a plus it will execute this code once he's finished doing that.
It will break.
That means the code will be terminated and then it looks for the second case.
If another operator is performed if it's a minus.
It will execute this piece of code here.
Identical to the first one.
Once it's done that it doesn't hang about it breaks out of the code.
Again the next one is a multiplication.
If each of these block code blocks are represented by as a case.
So this is a first case second case third case.
So if the operator clicked if it's a division if it's on a division operator it to execute this piece
of code here.
Okay.
And then with a switch statement you sometimes have what is called a default.
The default keyword basically specifies the code that will run if there is no case match.
OK.
So if none of the scenes here matches the expression you have to specify default which would be the
code that will run the default case does not have to be it doesn't have to be the last case here.
You could place it anyway in the block OK.
However if the default is not the last case in the switch block you must.
And the default case with a break although I've done that here is not really necessary.
But if for example the default was somewhere in the middle you'd have to end it with a break once again
our run through the switch statement with you.
So the way the switch works is that this switch looks for an expression.
In this case the expression is this variable here.
It will evaluate that expression once the value of the expression is thin compared with the values of
each of the cases for example plus minus two times more division.
It would compare that to each of the values in the cases and if there is a match the associated block
of code is executed.
If you find the matching goes through the cases if there is a match it will stop and execute that course
code piece of code once a match is found it it hits the break and the the code stops the execution of
the code will stop when a match is found and the job is done.
The break stops and there will be no more evaluation of the code block.
Note that is not always necessary to put a brake in the last case.
For example if you have the last case in the switch block you don't necessarily have to put a brake
is just good practice.
In this lecture we wrote the code for the equals two button is in the switch statement.
So there's still a bit more to do so we'll continue in the next lecture where we will try and tie up
the rest of the code that will make the calculator all active.
The purpose of this lecture was just to write the piece of code for the equals two button which we have
done using this switch statement.
Thanks so much for watching.
Take care.
Bye for now.