When we need to handle numerous or complex possibilities in our conditional code, switch statements are the way to go. Learn about use cases and functionality for different switch statements.
- [Instructor] Let's continue on to switch statements. switch statements take values and run them against possible matching patterns that we define in code. Each possible match can have a different block of code to execute called a case, which will be executed if the evaluating value matches. Let's start out with some test variables. We're going to make these constants. So firstInitial, I'm going to have this equal to "H", and then let hp = 26, and let mp = 24.
Now let's define a simple switch that will evaluate our firstInitial variable. We use the switch keyword, followed by firstInitial, and open and closed curly brackets, and we're going to get an error here because we don't have at least one case or a default block. Switch statements need to be exhaustive, which means that all cases need to be accounted for, or a default case needs to be defined to catch anything that falls through the cracks.
So let's define our first case here, with the case keyword, and the matching value. So I'm going to say "H":. Now I'm just going to add a print statement in here. For this case, I'm going to print out the string, "I'm guessing Harold?" Now on the next line, I'm going to add the default case, and here I'm just going to print, "I've got nothing....".
Above default, I'm going to add one more case, and this is going to check for firstInitial = A. So here I'm going to print, "Maybe Albert?". Now if we change the value of firstInitial to A, Maybe Albert will print out, and if it's anything but H or A, we'll get our default case, I've got nothing.... We can also use number ranges with switch statements.
So let's start a new switch, and this time I'm going to hit enter here when Swift prompts me, and it's going to give us the basic layout. So we're going to switch on hp, and the first case is going to be 0 if our hp is 0. Here I'm just going to print, "I'm sorry to tell you, you're not alive...", and for our default case, I'm just going to print out, "You're not in mortal danger".
Now let's flush this out just a bit more. For the case of 1...25, I'm going to print out, "You need to get some health...fast", and for case 26...50, I'm going to print out, "Did someone hit you recently?" Now again, if we change hp to 0, we'll see, I'm sorry to tell you, you're not alive..., if it's 55 we'll get, You're not in mortal danger, and if it's somewhere between 26 and 50, so let's say 28, you'll see, Did someone hit you recently? Now we can get pretty intricate with the types or numbers of values we can use in switch statements.
So let's try one that evaluates two variables instead of just one. I'm going to start with my switch keyword. For two variables, you need to use an open and close parenthesis, and here we're going to switch on mp and hp at the same time. For our first case, I'm going to check what our values are right now, 24 and 28, so we'll say 24 and 28, and from this, just going to print, "All good!".
Now we need a default case, and this is going to print, "I've still got nothing....". If we change just one of the variables, either mp or hp, this case will not be met. Let's add a second case that's going to use a range. So for case, when mp equals between 1 and 15, and hp equals 20, all the way up to, but not including 25.
Here we're going to print out, "Ranges are the best!". I'm going to change mp to 7 and hp to 22. There we go, we see Ranges are the best, print out in our debug console. Finally, something that's a bit more advanced but super useful, is being able to store a switch value in a local variable that we can use in a specific case body.
I'm going to add one more case statement, open and closed parenthesis, and in here, I'm going to declare a local let called localMP, and another one called localHP. Now in this case, we need to use a new keyword called where to actually define the case match. So this case is going to be where localMP + localHP > 20, and in this case, I'm just going to print out our local variables.
Now this is something to notice, switch's are sequential. So right now, our second case here, is going to execute, find that we have a correct match, and print out, "Ranges are the best!". Even though if it had kept going, our next case where localMP + localHP would be greater than 20. So let's change these. I'm going to change hp to 28 and mp to 30.
Now in the debug console, we're going to see that our local variables are printed out successfully.
- Starting new playgrounds and projects
- Variables and constants
- Writing single and multiline comments
- Core string methods
- Working with numbers
- Working with collections
- Creating arrays
- Working with sets
- Application control flow
- Writing functions
- Basic Swift classes and structs