Your First iOS & SwiftUI App: An App from Scratch

Feb 13 2023 · Swift 5.7, iOS 16, Xcode 14

Part 3: Coding in Swift

25. If / Else Statements

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 24. Challenge: How to Calculate the Difference Next episode: 26. Challenge: Calculate the Difference

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Back in our project let's implement the algorithm we just designed to calculate the difference between the slider and the target value in Swift. Along the way you'll learn about an important new construct in swift development the If/Else statement. Here in our project we're going to switch back to our project navigator and open up game dot swift and take a look at the points method. So, we're going to write some fake placeholder code just to visualize how If/Else statements work. So, the basic syntax for If/Else statements in Swift is If, if something is true and this is just placeholder this code isn't actually going to work. But put some code here that may evaluate to be true. And if it does evaluate to be true then do this thing inside of curly braces. Then you put Else, if something else is true then do that thing in curly braces instead. So, you have another condition here and if it evaluates to true, it'll run whatever code is in here. And then finally you put an Else, with no other conditions or anything there. And if everything else here above it is false, it'll go ahead and run this code in this pair of curly braces. Okay, so let's do this for real, for what we're trying to do with our algorithm here that we just discussed. So, we're going to create a local variable inside this method called the difference. And that's going to be an integer and we're not going to set it to an initial value right now. Instead, we're going to set the value within the If/Else statement. So, now let's fill in our If/Else based on our plain English algorithm. If the slider value is greater than the target once we know that slider value is bigger such difference to slider value minus target. Now we can check for the opposite. We can say Else/If, target is greater than slider value then the difference is the bigger number in this case which is target minus the smaller number slider value. So, set difference to that. And finally, if neither of those conditions are true the two values must be equal. So, the difference is zero. Now that we know the difference basically how far away the two values are we can figure out the awarded points. So, we're going to make another variable called, awarded points, and that's going to be an int. And I'm going to set that to be 100 which is the maximum score minus the difference. And once that's calculated, we can return awarded points. And now that we've written this code let's see if our test will pass. So, I'm going to switch over to the test file and just click that button. I could have also hit Command + U. And after a little bit the test will run and we get green check marks everywhere, nice. Now again, there are better ways to write this code but I wanted to start off with this to demonstrate If/Else statements as one way of doing it. And we're going to work on iterating over this code to make it nicer and nicer in the next few videos. Now let's actually try running this in the context of bullseye and I'll just do that right in the canvasing content view. And I'm actually going to try to get this random value here. So, I was off by three so I got 97 points this run, which is exactly, right. Awesome.