Join Harrison Ferrone for an in-depth discussion in this video Traditional if statements, part of Learning Swift 3 for Developers.
- [Instructor] Controlling the logical progression of your code is one of the prerequisites for building applications. Let's take a look at what traditional IF else statements look like in Swift 3. I'm going to close out of my collections playground and create a new playground. We're going to name this Ch.4_WorkingWithLogic. Hit next. Make sure that we're saving in our Swift3ForDevelopers folder and hit create.
Go ahead and delete the comment and the default stream and let's add some of our own. Chapter Four Working with Logic Flows. This video is Tradional IF Statements. Let's start by declaring two variables. Var myAge = 27 and var drivingAge = 16.
Next let's use these new variables in a simple conditional statement and print out messages fro each case. If myAge >= drivingAge, we'll simply print out "Let's go get you a license." Now if myAge is not greater than or equal to the drivingAge, we'll handle that with an else statement. Here we'll print out "Sorry, you'll have to wait a bit." Let's change myAge to 14, just for fun.
You'll see in the debug panel that our print statement does change. Let me change this back to 27 and make some more room. Conditional Swift statements can be a lot more versatile that this. Let's try a more complex example. We're going to need two new variables called firstCondition, and these are going to be Booleans. FirstCondition is going to = true. SecondCondition is going to = false. Now we're going to have an IF else statement that uses both of these conditions.
If firstCondition == true and, which we do with a &&, secondCondition is also true, we're going to print out "Continue with the operation." A quick note on syntax: for Booleans, if we delete the == true for the secondCondition, that means the same thing.
So what happens if the firstCondition is true and the secondCondition is false? Well, we need an else IF. And we're going to say just if the firstCondition is true, then we'll print out "Only our first condition was met." If none of these conditions are met, we need to handle that default case with a last else statement. Else open close curly brackets, and we'll print out "Neither conditions were met." In our debug log you'll notice that we have the message "only our first condition was met." Let's change our second condition to true.
There we go. Now we're going to continue with the operation. If they are both false, we'll get the debug message "neither conditions are met." A little note on syntax here: Swift has done away with semi-colons and unnecessary parentheses and brackets in a lot of its implementations. However, they can still be used to write clear, cleaner code. For instance, we could put parentheses around firstCondition == true and that would amount to the same thing.
We could even go back and make this even more explicit. Add back in our == true to our secondCondition IF statement, and then wrap both of these in one large parentheses. This is ultimately a style decision. So it's up to you. But I would steer towards using parentheses when you are using complex conditions or when the conditions get really long-winded.
- Starting a new playground
- Printing to the console
- Declaring variables
- Working with numbers
- Using strings
- Breaking down optionals
- Understanding closures
- Classes vs. structs
- Extensions and protocols