At some point your Swift application will need to make a decision. Like so many other languages, Swift handles branching via
switch statements. Let's see how we can use these constructs within the Swift programming language.
1.Introduction3 lessons, 15:05
2.Foundational Concepts5 lessons, 43:51
3.Working With Collections3 lessons, 26:26
4.Controlling Flow7 lessons, 1:22:24
5.Functions5 lessons, 51:54
6.Creating Types6 lessons, 1:08:10
7.Conclusion1 lesson, 01:15
In this section of the course we are now going to start to talk about some of the ways to control the flow of your application. Whether that be through conditional statements, through looping, through exception handling or a number of different topics. But really we're going to kind of really start to allow you to add in some flexibility to your code. And allow you to actually really start to add in some ways to control the direction that your application goes, through controlling the flow. The first thing that we need to understand is how to do some conditional statements. So, by conditional statements, I mean, if one thing is true then I want to do this and if something else is true, I want to do that. So, there can be a whole number of applications of this type of functionality and you're gonna see this in pretty much every programming language that you Run into for the most part. And so the way that we're gonna handle this is by breaking it down into very simple pieces. So the first thing that we're gonna talk about is the if statement. So the if statement is really just what it sounds like, it's if and then I'm gonna be able to do some sort of check, some sort of conditional check. And then if that is true or if it's false depending on how you're looking at it. Then I'm going to do some sort of operation. So before I do that let's start to create a variable here. Or a constant. We'll say let number = 9. This is going to be a very simplistic example but it's going to show you a number of different variations of our if statement. So we have a constant number 9. So let's say in my application depending on the number or the value of this number I'm going to do some sort of calculation or go down some other path of my application or something like that. So how do I do that? Within Swift. Well, I'm gonna start by specifying the if statement. Now I can say, if, and now I'm gonna check some sort of conditional statements, so something, I'm gonna check to see if some boolean operation results in true. So, I wanna check if our number is less than 10. Now this, once again, is very simple, but it's gonna prove a very interesting point. Now I can do any sort of conditional statement in here after my if statement as long as it results in a boolean value. Remember a boolean value is either true or false. So if this operation that goes on in here results in a true value, then I'm going to execute some block of code, and that block of code is going to be denoted in Swift by the opened and closed curly brackets. Now you can put them on the same line or a different lines, it really doesn't matter, but at the end of the day we just need those open and close curly brackets. So then within here, I can do some sort of operation that is based on the fact that my number is less than 10. And this is going to be very simple, I'm just gonna say, print the number is less than 10. So I will go ahead and Save this, and if I were to run my application down here in my console output you'll see that the number is less than 10 and that's exactly what we would expect. So let's say now that I wanted to do something else if the number was greater than 10. So let's say if it was 11. So if I Save this Run it now, then we shouldn't see anything in our output window. As you can see, we don't. So what I want to do now is I wanna add in another check so what I can absolutely do is I could say, put another check here and if number is now greater than 10. But this is going to be a little bit redundant and I can just enhance my if statement by specifying by specifying else. So I can say, if this happens then do this particular operation, which is just a print line. If that is not true. So if this is true do this true do this else do something else. In this case down here I'm simply going to write print. The number is greater than 10. So once again, nothing too crazy, nothing too out of the ordinary but very simple nonetheless. So I can Save that and Run it. And now we're going to get output down here that the number is greater than 10. All right, that's very good. So what about this area now where the number actually is 10? So if it is 10 and I go ahead and Run my application, well nothing's going to happen and that's a bit of a bummer, but we could check for that too. So I'm going to modify this if statement one more time, so now I can say if my number is less than 10, I can print this out. Now instead of jumping right to an ounce, I can hit an intermediate step in here, and I can say. Else if. And I can do another check here, so I can say if the number's less than 10, then do this. Else if, the number is equal to 10, now see the equal operator here is actually the double equal sign, which is very important for you to understand the difference between the equality operator here and the assignment operator, the single equal sign out. This single equal sign isn't gonna work here. We have to use the double equal sign. So else if the number is equal to 10. Once again, open and close curly brackets. And I'll just make sure and bring this up so it looks a little neater. And then within here, I'll go ahead and print the number is 10, just like that. So I could do if this operation or if this conditional is true then print this line, else if this one is true do this, else this. But I can continue to put a bunch of else ifs in here, as many as I need to, and it will continue to work. So if I go ahead and Run this now, you're gonna see that the number is 10. So that's the basic process of how to create an if statement. Now there's a couple of other variations out there, but this is the absolute basics. And it's very fr, very important for you to understand how this is going to work. Now another similar operation on how to do another conditional check which is somewhat similar to if but structurally a little bit different. And that's known as the Switch statement. So up here, we are dealing with if, and just for the time being, I'm going to use my little multiline comment syntax here. Just so I can use this other block of code down here. I'm gonna be dealing with what's known as the Switch statement. And actually, to be more accurate, this is actually IF...ELSE IF and so now we're gonna talk about the SWITCH statement. So what is a SWITCH statement? Well, a SWITCH statement is another conditional that's going to allow you to check a particular value and compare it to a number of other values. And then based on which one or which comparison succeeds, we're gonna do some sort of operation. So it's just another way to look at a similar logic as we have here, but this time we're gonna use a little bit of a different example, we're gonna say that we have an animal. So we're gonna say let animal = and let's just say this is gonna be cat. Now let's say I wanted to do some sort of operation within my code that's going to check the value of animal and based on the different values available for that animal I'm gonna do some sort of processing. So what I can do is I can create a SWITCH statement, and the switch is going to take in some variable, some constant, something that's gonna contain information that I wanna check against and in this case, I'm talking about an animal. Then we're going to use open and close parenthesis here to contain all of our logic. And within a SWITCH statement, every time you check to see if something matches, what we're checking against here, we're gonna use a case statement. So what I can do now is I can say, in the case that animal is cat, then I want to print out, you found a cat. So nothing very exciting here, but as you can see, I've created a K statement, I've specified what it is I wanna try to match this variable or constant against. And then I specify a colon, and then on the next line, or on the same line, I can do some sort of operation. I can do as many operations in here as I want. I can do several print lines, I can call other functions, I can do all sorts of other things, but this is gonna be a very simple example. So now let's say that I have another scenario where I wanna check to see if I have a dog. So in this case, I wanna say I wanna print you found a dog. So pretty simple, nothing too crazy there. But as you follow along and you're watching what's going on here, you're gonna see that there is a red exclamation point here, and it says that a switch must be exhaustive. Which means I have to specify, literally, every single possible option there is for this particular value that I'm switching against. And if you think about that for a minute, well, that's impossible. You can't possibly do that. So the way we get around that is by using a default clause. And what a default clause does is it says, okay, I'm gonna go through all of my K statements here and if none of them hold true, if none of them succeed, just go to the default, process the default and just move on. So what I do here is I just specify the default clause and then I'm going to simply put down here another print statement, I'll say print you found something else. Cuz I really don't know what it is at this point and I don't really care because it wasn't a cat or a dog. So let's go ahead and Save that, and if I were to Run my application. Now you're going to see that I say you found the cat. Because sure enough, my animal as a cat. It landed into this K statement here, and it printed out, you found a cat. So once again, I can change this to a dog, I can Save and Run that. It will say, you found a dog. That's great. But what about a scenario where I had an uppercase C, or I said, this was my animal? Well, nothing is gonna happen here, and actually, something will happen. It's gonna say you found something else, because these two values are lexigraphically different. This c here does not equal this c because the value of it is different. So how could I handle something like that? Well if you're used to using switch statements in other languages, you may be used to doing something where you could actually specify two case statements together like this, and we would use something called Fall through where this K statement doesn't actually have an implementation or have any lines after it. But it would be coupled with this K statement, and that would ultimately fall into this scenario, but Swift doesn't actually work that way. So if you take a look at our problem here, it says I should have at least one executable statement and that is the way that it basically works. Every time you specify a K statement you have to have at least one executable statement in there. Now it can be a bunch of gibberish but I really don't want to come in here and have to say all right well I'm going to print you found a cat again because hat's gonna be pretty repetitive. Now what I could do though to get around this, is I could specify multiple values within a single K statement. So I could say, where the case it's cat all lower case. Or the case where it's cat with an uppercase C now I'm supposed to find two case values that are gonna fall into the same operation, just like this one, I can Save this and I can Run it and I'm going to get you found a cat again. So there you have it, those are some of the basic conditional statements that you're going to see quite often. Throughout the Swift languages as well as many other languages out there and they definitely are important enough for you to commit them to memory. At least the basic syntax and the basic functionality. There's definitely ways that you can enhance them and use other sort of pieces of functionality to make them a little fancier, but this is the absolute basics. And how you're gonna use the if statement, coupled with the else if and the else, as well as the Switch.