WEBVTT 1 00:00:01.005 --> 00:00:03.222 We have already looked at the If Else structure 2 00:00:04.003 --> 00:00:07.111 but theres one other structure that is used for selection 3 00:00:07.116 --> 00:00:10.228 within C programming thats very very useful 4 00:00:11.001 --> 00:00:13.005 and that is the Switch structure. 5 00:00:16.110 --> 00:00:18.220 If Else is a very powerful selection tool 6 00:00:18.222 --> 00:00:21.002 selecting between two possible states 7 00:00:21.117 --> 00:00:24.113 based on whether a test is true or false 8 00:00:24.226 --> 00:00:26.112 and we've seen already that you can build 9 00:00:26.113 --> 00:00:28.111 that up using if elses 10 00:00:28.115 --> 00:00:32.000 nested inside if elses to build up more complex structures 11 00:00:32.110 --> 00:00:35.114 but that becomes difficult to follow very easily 12 00:00:35.221 --> 00:00:37.119 and difficult to check the logic 13 00:00:39.006 --> 00:00:41.225 So there an alternative structure that we can use if we want 14 00:00:41.226 --> 00:00:43.116 to make a selection between several 15 00:00:43.117 --> 00:00:45.110 different possible outcomes 16 00:00:45.117 --> 00:00:47.117 depending on some kind of input condition 17 00:00:47.227 --> 00:00:49.220 and thats the switch statement. 18 00:00:52.223 --> 00:00:54.111 The switch statement can be viewed almost as 19 00:00:54.112 --> 00:00:56.007 being like a multiple choice 20 00:00:56.221 --> 00:00:59.007 where when you enter the switch statement 21 00:00:59.110 --> 00:01:00.220 there'll be a particular variable or 22 00:01:00.221 --> 00:01:02.221 value thats going to be evaluated 23 00:01:03.006 --> 00:01:07.001 its checked and if it meets the first criteria 24 00:01:07.225 --> 00:01:10.226 then that particular option will be executed 25 00:01:11.114 --> 00:01:13.007 and then you will come out 26 00:01:13.116 --> 00:01:15.228 of the switch statement having only executed that path 27 00:01:16.007 --> 00:01:18.110 if it doesn't meet the first criteria 28 00:01:18.118 --> 00:01:21.221 it will go on to evaluate the second criteria 29 00:01:22.008 --> 00:01:24.220 and the third criteria and if any of these are true 30 00:01:24.227 --> 00:01:27.006 then that particular path for example option three 31 00:01:27.007 --> 00:01:28.004 will be taken 32 00:01:28.228 --> 00:01:31.005 if none of the criteria are met 32 00:01:31.117 --> 00:01:34.111 then there's a structure that allows for a default 33 00:01:34.225 --> 00:01:36.115 catch all if you like 34 00:01:36.221 --> 00:01:40.224 to be run if nothing else is found to be correct 35 00:01:42.003 --> 00:01:45.114 so only one of the optional blocks of code 36 00:01:45.225 --> 00:01:49.000 either option one in this case or option two or option three 37 00:01:49.114 --> 00:01:53.223 or the default block will be run nevermore. 38 00:01:55.228 --> 00:01:58.000 The structure of the switch statement 39 00:01:58.110 --> 00:01:59.223 is quite straightforward 40 00:02:00.117 --> 00:02:04.110 the keywords that we use are switch itself 41 00:02:04.225 --> 00:02:07.112 and then for each block of code we want to execute 42 00:02:07.118 --> 00:02:09.222 the start of it is marked by case 43 00:02:10.111 --> 00:02:11.229 and then whatever the evaluation 44 00:02:12.000 --> 00:02:13.113 term that we're going to check 45 00:02:13.228 --> 00:02:16.113 and it ends with break 46 00:02:17.002 --> 00:02:19.118 there is one limitation though about using the switch 47 00:02:19.225 --> 00:02:23.003 the switch statement works with what are termed ordinals 48 00:02:23.223 --> 00:02:27.112 ordinals are things that take on an absolute value 49 00:02:27.225 --> 00:02:30.222 like Integers or individual characters 50 00:02:31.002 --> 00:02:33.007 not strings of characters for example. 51 00:02:36.000 --> 00:02:38.111 So heres the layout of the switch statement 52 00:02:39.006 --> 00:02:41.116 at the top we have the keyword switch 53 00:02:42.221 --> 00:02:44.112 and then in brackets 54 00:02:45.220 --> 00:02:48.112 we put the variable that we're going to evaluate 55 00:02:49.226 --> 00:02:52.112 and then between the opening brace 56 00:02:53.001 --> 00:02:57.003 and the closing brace we have the actual selection stages 57 00:02:58.113 --> 00:03:00.119 and heres the structure of each individual selection of the 58 00:03:00.220 --> 00:03:06.006 term case is used and then what the first 59 00:03:06.220 --> 00:03:08.117 evaluation test will be 60 00:03:09.007 --> 00:03:12.004 if that conditions met in between 61 00:03:12.114 --> 00:03:15.006 that colon and the break statement 62 00:03:15.113 --> 00:03:18.113 you put all the code you want to execute if that is true 63 00:03:19.114 --> 00:03:21.221 if that is true that first option 64 00:03:22.000 --> 00:03:24.003 and when the code executes down 65 00:03:24.009 --> 00:03:26.228 through those various instructions and hits the break 66 00:03:27.005 --> 00:03:29.229 it then jumps right to the end 67 00:03:30.118 --> 00:03:33.006 the switch statement and continues on 68 00:03:33.229 --> 00:03:35.220 so only one path is taken 69 00:03:36.115 --> 00:03:38.008 if the first case isn't taken 70 00:03:38.114 --> 00:03:41.225 then you can have a second alternative a third alternative 71 00:03:42.002 --> 00:03:44.001 as many as you want basically 72 00:03:44.113 --> 00:03:47.009 until eventually if none of those alternatives are met 73 00:03:47.222 --> 00:03:49.111 we have the default option 74 00:03:50.111 --> 00:03:52.229 and that then concludes the catch all code 75 00:03:53.004 --> 00:03:55.114 what to do if none of the above is true 76 00:03:56.002 --> 00:04:00.220 and then break and continue on to the rest of the code 77 00:04:01.112 --> 00:04:03.225 lets have a quick look at a real example. 78 00:04:05.001 --> 00:04:08.003 Obviously to fit on the slide this is a very simple example 79 00:04:08.110 --> 00:04:12.005 but what we're going to do is display on screen 80 00:04:12.119 --> 00:04:16.001 whatever colour the colour sensor is currently seeing 81 00:04:17.222 --> 00:04:20.228 so we've got a complete program here and on the next slide 82 00:04:21.002 --> 00:04:23.113 we'll zoom in more closely at the switch statement 83 00:04:23.221 --> 00:04:25.118 to really look at the structure of it 84 00:04:26.110 --> 00:04:28.118 but just briefly lets look at the main program 85 00:04:28.228 --> 00:04:31.008 we've obviously got the main program setup 86 00:04:31.110 --> 00:04:33.008 with its start and end braces 87 00:04:34.111 --> 00:04:37.114 and then inside that first of all we declare a variable 88 00:04:37.229 --> 00:04:40.117 in this particular case we're using the term short 89 00:04:40.225 --> 00:04:42.229 which simply means its an integer 90 00:04:43.003 --> 00:04:45.110 but requires less storage space because its not 91 00:04:45.111 --> 00:04:47.113 going to have a large range of values 92 00:04:48.000 --> 00:04:50.004 we call it integer currentColour 93 00:04:50.115 --> 00:04:54.008 so currentColour will store a particular numeric integer value 94 00:04:55.005 --> 00:04:57.002 and then we've got a loop setup 95 00:04:57.228 --> 00:05:01.229 which uses the trick of putting while and true 96 00:05:02.006 --> 00:05:05.000 now the while statement normally has a test built into it 97 00:05:05.002 --> 00:05:06.118 which is neither true or false 98 00:05:07.002 --> 00:05:09.003 if you make the while statement permanently true 99 00:05:09.009 --> 00:05:12.002 all that means is between the start of the while 100 00:05:12.119 --> 00:05:16.003 and the end of the while the program will run forever 101 00:05:16.008 --> 00:05:18.005 until we switch the computer system off 102 00:05:18.111 --> 00:05:20.000 or reset the program 103 00:05:20.115 --> 00:05:22.225 so its a way of having a continuous loop 104 00:05:25.002 --> 00:05:27.111 and now we get to the switch statement itself 105 00:05:29.008 --> 00:05:31.007 now to allows us to look at it in detail 106 00:05:31.114 --> 00:05:33.119 what we'll do is move on to the next slide 107 00:05:33.225 --> 00:05:36.220 but do bare in mind that what were seeing is 108 00:05:37.224 --> 00:05:39.222 this switch statement we're reading in 109 00:05:40.222 --> 00:05:43.003 the value of a particular colour from the sensor 110 00:05:43.118 --> 00:05:45.111 and we're storing it in a variable 111 00:05:45.116 --> 00:05:47.115# so that will contain a number 112 00:05:47.225 --> 00:05:51.113 with then going to manipulate and evaluate. 113 00:05:53.229 --> 00:05:55.226 Looking at the switch statement now itself 114 00:05:56.001 --> 00:05:57.220 by zooming in a little bit we're able to look at 115 00:05:57.221 --> 00:05:59.222 the layout slightly more neatly 116 00:06:02.003 --> 00:06:04.225 at the top where the switch statement as I described before 117 00:06:05.005 --> 00:06:07.220 and then we have the variable 118 00:06:07.223 --> 00:06:09.115 that takes on a particular value, 119 00:06:09.225 --> 00:06:11.221 now the particular sensor we're using 120 00:06:11.228 --> 00:06:14.229 the colour sensor reads in values 121 00:06:15.007 --> 00:06:16.220 of the current colour that it sees 122 00:06:16.221 --> 00:06:18.002 and it assigns them a number 123 00:06:18.008 --> 00:06:21.006 between zero and seven so the colour sensor on the robot 124 00:06:21.112 --> 00:06:23.008 can only see eight colours 125 00:06:23.222 --> 00:06:26.113 the first colour it can see is in fact no colour 126 00:06:26.119 --> 00:06:28.111 so you could argue it sees seven colours 127 00:06:28.113 --> 00:06:29.225 and an absence of colour 128 00:06:30.002 --> 00:06:32.224 but basically it returns a number between zero and seven 129 00:06:32.228 --> 00:06:34.221 thats all it can return 130 00:06:36.009 --> 00:06:38.220 so the sensor is read 131 00:06:38.229 --> 00:06:41.003 the value's stored in currentColour 132 00:06:41.008 --> 00:06:43.111 and then switch statement starts to run 133 00:06:44.001 --> 00:06:46.001 and it takes the value of the currentColour and just 134 00:06:46.002 --> 00:06:47.009 for the sake of argument lets say that 135 00:06:47.110 --> 00:06:49.000 currentColour at this moment 136 00:06:49.116 --> 00:06:51.228 contains the value three 137 00:06:53.007 --> 00:06:56.115 as the program executes the switch statement 138 00:06:56.116 --> 00:06:57.115 starts running 139 00:06:58.008 --> 00:07:01.115 and it takes the value of currentColour and compares it 140 00:07:01.228 --> 00:07:04.116 with the first case is it zero no 141 00:07:05.004 --> 00:07:08.111 is it one nope, is it two no 142 00:07:08.118 --> 00:07:10.007 is it three yes 143 00:07:10.112 --> 00:07:13.006 currentColour currently contains three so this case 144 00:07:13.116 --> 00:07:17.110 its true therefore this line here 145 00:07:19.114 --> 00:07:23.000 executes and displays on the screen the word green 146 00:07:23.119 --> 00:07:25.115 the next line we reach is break 147 00:07:25.225 --> 00:07:27.110 so that then breaks out of the 148 00:07:27.116 --> 00:07:29.116 switch statement to the bottom 149 00:07:30.003 --> 00:07:32.116 if you recall the main program on the previous slide 150 00:07:32.220 --> 00:07:35.224 then add a loop so we would go back to the top 151 00:07:36.110 --> 00:07:38.115 we would evaluate currentColour again 152 00:07:38.222 --> 00:07:40.227 and it may be that the value has changed 153 00:07:41.112 --> 00:07:43.008 and its now become four 154 00:07:43.119 --> 00:07:44.229 in which case 155 00:07:45.005 --> 00:07:47.111 we'd run down through the options once again 156 00:07:47.228 --> 00:07:50.003 and when we reached the case of four 157 00:07:50.009 --> 00:07:53.119 we then execute an instruction which says 158 00:07:53.224 --> 00:07:55.005 displays on screen 159 00:07:56.003 --> 00:07:59.226 the word yellow before breaking once more to the bottom 160 00:08:01.003 --> 00:08:03.115 so switch statement simply takes 161 00:08:03.221 --> 00:08:06.227 the variable that is included in the switch bracket 162 00:08:07.006 --> 00:08:08.116 and compares it against 163 00:08:08.118 --> 00:08:10.119 all the different cases that have been provided 164 00:08:11.003 --> 00:08:13.225 but do remember if the value thats stored 165 00:08:14.009 --> 00:08:16.114 has been tested by the switch statement 166 00:08:16.220 --> 00:08:18.225 doesn't match any of the listed ones 167 00:08:19.006 --> 00:08:21.112 there is almost the default statement at the bottom 168 00:08:21.220 --> 00:08:25.110 which allows you to catch any other condition 169 00:08:25.116 --> 00:08:27.111 that you haven't already anticipated 170 00:08:27.114 --> 00:08:29.220 in the various cases you've identified 171 00:08:30.009 --> 00:08:31.224 thats how the switch statement works 172 00:08:32.000 --> 00:08:35.113 and you'll find examples in the handbook 173 00:08:35.220 --> 00:08:38.116 and also you'll find lots of examples online.