2.4 Control Flow Part 2: Conditionals
In addition to loops, another common mechanism to control the flow of your applications is through the use of conditionals. In this lesson we will cover the common forms of conditionals within the Swift language.
1.Introduction4 lessons, 26:24
2.Language Constructs12 lessons, 1:51:34
3.Swift and Object Oriented Programming5 lessons, 27:52
4.Built-In Types5 lessons, 42:18
5.Conclusion1 lesson, 01:19
2.4 Control Flow Part 2: Conditionals
The next logical step when working on controlling the flow of your application using Swift is to be able to use some sort of conditional statements or some branching statements. So those come in the form typically of the if, else if and else statements as well as the switch statement, so let's see what those things look like. So lets say for some reason, we have a va, variable. Now, we'll call this value, and it has a value of 20. So now, based on some sort of happenings within our application, we could take a couple different paths, based on this value to do some other sort of processing. So we do this by using the if statement. Now, within swift you have the option of using the open and close parentheses to define your if clauses but you don't have to. So, in this case I could say something like, if value is equal to and we're using the double equal sign here to denote equality. We could say, if value is equal to 20 then we'll go ahead and do a print line here and we'll say, just right, and we could some of those other sort of processing there should we want to. Well let's say that if it's not the case then we'll use the elf, the else clause at the end of that or the else statement and say Println, Not quite right, like that. So now we have two different paths. We have the, the correct path where the value equals 20, where we print Just right, and the incorrect path where just print out Not quite right. So if we were to change this to be something other than 20, say 10, then now we're printing Not quite right. But let's say there's other, other branches or other possibilities within there. How do we do that? Well just like other programming languages we can accomplish that through the else if statement. So we can say, if value is equal to 20 then we want it to be just right, but we can say, all right well there's also going to be another path that we could do something. We can say if value is less than 20 and in this case I am using the parentheses. I'm going to then print out to little, like that. We'll go ahead and clean this up a little bit. So, there we go. So now we can see since our value is 10 we did not equal twenty. So we skip over this print line and we fall into this clause. We're now we're checking to see if it's less than 20, which it is, and now we can say that it's too little, and then we can even change this print line here. Instead of being not quite right, we could say now too much. So if we're to change this from 10 to 20, we're going to get, Just right and if we change the 20 to 30, we gonna get Too much. So as you can see here, this is the basic way of using if statements to kind of control the flow based on some sort of characteristic or variable or some other aspect of your application to be able to follow different code paths. Now, the if statement is not the only way to do that. We could definitely do that another way, and that's through what's called the switch statement, so this is available on several other programming languages as well, and the basic premise here is very much the same, so we're going to start by specifying the switch keyword and then we're gonna give it some value that we're looking at throughout this statement. So in this case, we're once again going to be looking at values so we'll open close parenthesis and now we have the option to define some number of case statements with inter-switch to say, what happens when we either hit that particular value or maybe a range that includes that value? Then we can go ahead and handle that. So we'll start by saying that we have a case here where if our case is equal to 20, and we end the case statement with a colon, and now we're going to say, all right. Well, in this case what I would like to do is I would like to say print line, and in here I will put in Just right. Just like we had before. Now as you can see here, this isn't going to finish because this is not a complete Switch statement, so let's go ahead and take a look at why, and it must be exhaustive consider adding a default clause. So, like many other languages, you can specify as many case statements in here as you would like, but ultimately, you need to kind of have that catch-all, so that you, your switch statement becomes what they're referring to as exhaustive. So we can drop down here, and we can put in our default clause with a colon. And I like to space these out a little bit, and I can now say print line something went wrong. So that can be our, our catch-all, where we just kind of dump everything that doesn't fall into one of our case statements. So as you can see here, something went wrong, but if I change this to be, to be 20, we hit that just right. So I can start to specify as many of these in here as I would like. So we'll say, case and then I can put in here a case this time, and we'll make this say 30, and we'll say that print line is going to be too much. [BLANK_AUDIO] Something along those lines, and if I were to change this to be 30, we're not gonna get too much. So this is, this is nice but, it's a restrictive if you think about it in terms of those, if statements where I could kind of start to specify maybe some ranges. Well, I can do the same thing within a switch statement, or more specifically, within the case. So I can, what I can do within here is I can say okay, well let's say anything between 20 and 30 is just right. Well, I can specify a range for this case. So instead of calling out 30, this way I can say, between the range of 20 and 30, then we're just right. So as you can see here, I'm printing out just right. Now you may be noticing something a little bit more interesting about this syntax. Where the flow for this particular version of the switch statement within Swift. Swift does not use what's referred to as fall through. So, in ma, many other programming languages, like C and C++ and Java and C# after, at the end of every case statement. You need to explicitly put in a break, and this break tells the flow of the application to hit that break and jump out and stop checking these things because basically, in those other languages, it's going to fall through and it's gonna continue looking if you don't explicitly put in a break statement. And actually, most of the languages will force you to put in a break statement. So that's one thing that you don't have to worry about so much in Swift, you can if it makes you feel comfortable, but it's not required because what Swift is going to do is once it finds one of these case statements that the switch condition hits, it's going to execute that case statement and it's going to break on its own, so you don't have to explicitly tell it to do that. It's gonna handle it all on it's own. And you can continue to do several different combinations of these type of things. One other thing you, and because of this lack of fall through, what you're going to find here is a lot of times what you might have been used to doing in the past is just stacking these case statements. So let's say I would do something like case 20, and then I could also put in here, maybe a case 30, but what's gonna happen here is you're actually going to get a compile time error because in many other languages we would stack these case statements to say. Because there is fall through, if it's 20 or if it's 30 or whatever, eventually if it hit one of those it would fall into the code that it was meant to hit, but in this case, if you take a look at this, you're going to see that we need to have at least one executable statement in all of these cases because there is no fall through. So that's something to be aware of when you're doing this type of switch statement. So along the lines of not having to put in those break statements. So there you have the basics of the conditional statements within the Swift programming language, and we're gonna touch on some more interesting examples of these a little bit later on, in the course as we start to lead into to other topics, but the if and the switch are really gonna be foundational for you as you're starting to add control to your application here. So you're definitely gonna need to become familiar with the basic syntax and usage of these constructs.