# Programming in Swift: Fundamentals

Oct 19 2021 Swift 5.5, iOS 15, Xcode 13

## Part 1: Core Concepts

### Lesson Complete

Play Next Lesson
Next

Update Notes: The student materials have been reviewed and are updated as of October 2021.

It’s time for your next challenge! You can find the challenge in the “Challenge - Logical Operators” page of the playground you’ve been using, or you can download a new one from the resources for this video. Open it up, and try solving the challenge questions on your own, then keep watching to compare your work to mine. Good luck!

Challenge 1. You’ve been provided with a a constant named `myAge` below that’s already been assigned a value. Feel free to change the value of this constant to match your actual age.

Use that constant to create an `if-else` statement to print out `"Teenager"` if the value of `myAge` is greater or than 13 but less than or equal to 19, and to print out `"Not a teenager"` if the value is outside that range.

I’m going to leave that constant set to 42, but feel free to change it if you want to help me feel younger. Now, I’ll start constructing my if-else statement. I want to check that myAge is greater than or equal to 13, AND that myAge is less than or equal to 19:

``````if myAge >= 13 && myAge <= 19 {
}
``````

Then, if this is true, I want to print “Teenager”:

``````if myAge >= 13 && myAge <= 19 {
print("Teenager")
}
``````

…but if it’s not true, I’ll add an else clause here, to print out “Not a teenager”:

``````if myAge >= 13 && myAge <= 19 {
print("Teenager")
} else
{
print("Not a teenager")
}
``````

And, clearly, I am a teenager no more. Tell me about it!

Challenge 2! Create a constant named `teenagerName`, and use a ternary conditional operator to set the value of `teenagerName` to your own name as a string if the value of `myAge`, declared above, is greater than or equal to 13, but less than or equal to 19, and to set the value of `teenagerName` to `"Not me!"` if the value is outside that range.

Then print out the value of `teenagerName`. Whoo! Let’s break that down a bit, piece by piece. So, first, you need to declare a constant:

``````let teenagerName =
``````

Now, you know that a ternary conditional operator starts with an expression. In this case, the expression needs to check if myAge is greater than or equal to 13:

``````let teenagerName = myAge >= 13
``````

…and that myAge is less than or equal to 19:

``````let teenagerName = myAge >= 13 && myAge <= 19
``````

If this is true, then return the String “Chris”:

``````let teenagerName = myAge >= 13 && myAge <= 19 ? "Chris"
``````

…but if it’s not true, then return the string “Not me!”:

``````let teenagerName = myAge >= 13 && myAge <= 19 ? "Chris" : "Not me!"
``````

Then, I simply print out the value of teenagerName:

``````print(teenagerName)
``````

That’s it for this challenge! Next up, you’ll finish off this part of the course by learning about a concept in Swift, called Optionals, which lets you deal with the case when you don’t have ANY value at ALL. See you there!