1 00:00:00,750 --> 00:00:01,800 Hello and welcome. 2 00:00:01,830 --> 00:00:08,660 In this lecture we are going to create the method for the equals two button. 3 00:00:09,090 --> 00:00:20,980 So it will capture all the events that happen when the equal to button is clicked so less create these 4 00:00:20,990 --> 00:00:23,580 color code to do that. 5 00:00:23,580 --> 00:00:29,140 You click on the equals two button. 6 00:00:29,460 --> 00:00:37,690 And here in the properties click on that and where you've got the click event just double click. 7 00:00:37,830 --> 00:00:41,820 And it should open up the skeletal code. 8 00:00:41,820 --> 00:00:43,770 So this is the method. 9 00:00:43,860 --> 00:00:53,100 And in between these parentheses here is where we will write our code that will respond to the equals 10 00:00:53,130 --> 00:01:08,100 two button when it is clicked because the equals two button will be used in combination with the operators. 11 00:01:08,100 --> 00:01:17,610 We are going to use what is called a switch statement the switch statement basically is used to perform 12 00:01:17,610 --> 00:01:23,180 different actions based on different conditions. 13 00:01:23,190 --> 00:01:23,610 All right. 14 00:01:23,610 --> 00:01:33,090 So what happens is that when you use a switch statement you select a block of code to be executed based 15 00:01:33,240 --> 00:01:35,910 on certain conditions. 16 00:01:35,910 --> 00:01:43,470 The best way to illustrate this that I'm going to just write a piece of code and I will run through 17 00:01:43,470 --> 00:01:44,070 it with you. 18 00:01:45,980 --> 00:01:46,410 All right. 19 00:01:46,530 --> 00:01:51,920 I have added a chunk of code here this code highlighted here. 20 00:01:52,590 --> 00:01:57,790 And this is basically what is known as a switch statement. 21 00:01:58,170 --> 00:02:05,170 So a switch statement is used to perform different actions based on different condition. 22 00:02:05,190 --> 00:02:12,750 So here when you are writing a switch statement you begin with the word switch and then in inside the 23 00:02:12,750 --> 00:02:21,790 parentheses you have to pass in an expression that will be evaluated once. 24 00:02:21,870 --> 00:02:22,560 All right. 25 00:02:22,560 --> 00:02:29,070 So this what have passed in here is a variable that was created here. 26 00:02:29,070 --> 00:02:32,670 This string here called operate all clicked. 27 00:02:32,670 --> 00:02:41,760 So that is a variable that is the expression this switch is is going to express is going to see what 28 00:02:41,760 --> 00:02:49,380 happens a wrong the code goes true once and it checks this to check which of the operators have been 29 00:02:49,380 --> 00:02:50,300 clicked. 30 00:02:50,310 --> 00:02:54,840 So each of the operators are represented as cases. 31 00:02:54,840 --> 00:03:03,030 So this is case one if for example someone clicks on the does a mathematical addition. 32 00:03:03,300 --> 00:03:08,630 And after that they click on the equals to sign this. 33 00:03:08,760 --> 00:03:17,550 It will references operator or clicked variable and the case will be plus for example with the operator 34 00:03:17,550 --> 00:03:23,040 on perform is a plus it will execute this code. 35 00:03:23,040 --> 00:03:34,590 This result box dot text is the variable is the result box where the answers are displayed for the calculate 36 00:03:35,760 --> 00:03:45,060 and we'll set that to equals to the resulting value variable plus this double here we used in the past 37 00:03:45,090 --> 00:03:53,100 to convert the text from the result box into a value that it understands. 38 00:03:53,610 --> 00:03:54,770 And then we are now. 39 00:03:54,870 --> 00:04:03,030 Then we convert and after that's been converted we then converted into a string using the two string 40 00:04:03,030 --> 00:04:04,100 method. 41 00:04:04,110 --> 00:04:06,270 So this is case 1. 42 00:04:06,270 --> 00:04:16,050 So if the operator that is click is a plus it will execute this code once he's finished doing that. 43 00:04:16,050 --> 00:04:17,250 It will break. 44 00:04:17,250 --> 00:04:22,500 That means the code will be terminated and then it looks for the second case. 45 00:04:22,500 --> 00:04:28,290 If another operator is performed if it's a minus. 46 00:04:28,320 --> 00:04:30,590 It will execute this piece of code here. 47 00:04:30,600 --> 00:04:32,550 Identical to the first one. 48 00:04:32,580 --> 00:04:37,900 Once it's done that it doesn't hang about it breaks out of the code. 49 00:04:38,190 --> 00:04:41,570 Again the next one is a multiplication. 50 00:04:41,570 --> 00:04:47,020 If each of these block code blocks are represented by as a case. 51 00:04:47,070 --> 00:04:49,890 So this is a first case second case third case. 52 00:04:50,310 --> 00:04:57,330 So if the operator clicked if it's a division if it's on a division operator it to execute this piece 53 00:04:57,330 --> 00:04:59,040 of code here. 54 00:04:59,270 --> 00:04:59,830 Okay. 55 00:04:59,850 --> 00:05:06,700 And then with a switch statement you sometimes have what is called a default. 56 00:05:06,930 --> 00:05:14,700 The default keyword basically specifies the code that will run if there is no case match. 57 00:05:15,110 --> 00:05:15,500 OK. 58 00:05:15,510 --> 00:05:22,770 So if none of the scenes here matches the expression you have to specify default which would be the 59 00:05:22,770 --> 00:05:30,630 code that will run the default case does not have to be it doesn't have to be the last case here. 60 00:05:30,630 --> 00:05:34,470 You could place it anyway in the block OK. 61 00:05:34,500 --> 00:05:41,330 However if the default is not the last case in the switch block you must. 62 00:05:41,400 --> 00:05:48,320 And the default case with a break although I've done that here is not really necessary. 63 00:05:48,330 --> 00:05:56,060 But if for example the default was somewhere in the middle you'd have to end it with a break once again 64 00:05:56,180 --> 00:05:59,440 our run through the switch statement with you. 65 00:05:59,780 --> 00:06:04,550 So the way the switch works is that this switch looks for an expression. 66 00:06:04,580 --> 00:06:08,850 In this case the expression is this variable here. 67 00:06:09,110 --> 00:06:19,010 It will evaluate that expression once the value of the expression is thin compared with the values of 68 00:06:19,190 --> 00:06:25,010 each of the cases for example plus minus two times more division. 69 00:06:25,070 --> 00:06:35,730 It would compare that to each of the values in the cases and if there is a match the associated block 70 00:06:35,730 --> 00:06:37,820 of code is executed. 71 00:06:37,830 --> 00:06:44,370 If you find the matching goes through the cases if there is a match it will stop and execute that course 72 00:06:44,460 --> 00:06:54,100 code piece of code once a match is found it it hits the break and the the code stops the execution of 73 00:06:54,100 --> 00:06:58,750 the code will stop when a match is found and the job is done. 74 00:06:58,900 --> 00:07:05,320 The break stops and there will be no more evaluation of the code block. 75 00:07:05,710 --> 00:07:12,520 Note that is not always necessary to put a brake in the last case. 76 00:07:12,520 --> 00:07:18,880 For example if you have the last case in the switch block you don't necessarily have to put a brake 77 00:07:18,900 --> 00:07:22,150 is just good practice. 78 00:07:22,150 --> 00:07:32,520 In this lecture we wrote the code for the equals two button is in the switch statement. 79 00:07:33,070 --> 00:07:39,880 So there's still a bit more to do so we'll continue in the next lecture where we will try and tie up 80 00:07:40,570 --> 00:07:45,970 the rest of the code that will make the calculator all active. 81 00:07:46,000 --> 00:07:52,900 The purpose of this lecture was just to write the piece of code for the equals two button which we have 82 00:07:52,900 --> 00:07:55,540 done using this switch statement. 83 00:07:55,540 --> 00:07:56,860 Thanks so much for watching. 84 00:07:56,860 --> 00:07:57,550 Take care. 85 00:07:57,550 --> 00:07:58,150 Bye for now.