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.

Transcript: 25. If / Else Statements

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.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 and then you put some code, if something is true. This is just placeholder text, this code doesn’t actually work.

if something is true {
  then do this
} 

But you put some code here that may evaluate to be true, and if it does evaluate to be true, then do this.

Then you put else if something else is true, then do that instead. So you have another condition here, and if it evaluates to true, it’ll run whatever code is in here.

} else if something else is true {
  then do that instead
}

And then finally you put a else with no other conditions or anything there, and if everything else here is false, it’ll go ahead and run this code.

} else {
  do something when neither of the above are true
}

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.

func points(sliderValue: Int) -> Int {
  var difference: Int

So, now, let’s fill in our if-else based on our plain-english algorithm.

If the sliderValue is greater than the target, and once we know that sliderValue is bigger, set difference to sliderValue - target.

  if sliderValue > target {
    difference = sliderValue - target
  } 

Now, we can check for the opposite. We can say else if target is greater than sliderValue, then the difference is the bigger number in this case, which is target - sliderValue. So set difference to that.

  } else if target > sliderValue {
    difference = target - sliderValue
  } 

And finally if neither of those conditions are true, the two values must be equal, so the difference is zero.

  } else {
    difference = 0
  }

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 awardedPoints, and that’s going to be an Int, and I’m going to set that to a 100, which is the maximum score, minus the difference.

And once that’s calculated, we can return awardedPoints.

  var awardedPoints: Int = 100 - difference
  return awardedPoints
}

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, 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 canvas in ContentView.swift.

And I’m actually going to try to get to this random value here.

So I was off by 3, so I got 97 points this round, which is exactly right, awesome.