# Learn Swift for Beginners 2026 - Lesson 10 - Ternary Operator

## Метаданные

- **Канал:** CodeWithChris
- **YouTube:** https://www.youtube.com/watch?v=-QrtdLcQoew
- **Дата:** 30.04.2026
- **Длительность:** 7:28
- **Просмотры:** 376
- **Источник:** https://ekstraktznaniy.ru/video/49667

## Описание

Master the Ternary Operator in Swift: The shorthand way to write if statements in just one line of code!

Resources:
📚 Full guide: 
https://codewithchris.com/learn-swift?utm_source=youtube&utm_medium=video&utm_campaign=learn_swift_lesson_10

Timestamps:
0:00 Introduction
0:21 Example overview
1:11 Ternary vs if statement
1:33 Breaking down the syntax
2:57 How it works (recap)
3:23 Common use cases
3:54 String interpolation example
4:56 Nesting (not recommended)
5:19 Challenge introduction
5:42 Challenge walkthrough
6:55 Wrap-up
7:02 AI practice prompts
7:25 Outro

In this lesson from the Learn Swift series, you'll learn:

- What the ternary operator is and how it compares to a standard if statement
- How to read and write the condition ? true result : false result syntax
- Real-world use cases like greeting messages and string interpolation
- Why nesting ternary operators is possible but not recommended
- A hands-on challenge: build a pass/fail checker in just 3 lines of code
Whether y

## Транскрипт

### Introduction []

Hi, welcome back to the learn Swift series. So, we are at the last lesson of the making decisions chapter, ternary operator. Don't let this name fool you. It's actually really handy and easy to understand. It's basically a variation of the if statement that you've learned already. So, let's take a look at how this is done. this

### Example overview [0:21]

done. So, let's take a look at this example. Okay, let's fire up Xcode here, this playground. So, what you see here is a constant called temperature that is set to 28. And then we have a string variable called advice that we haven't set the value of just yet. We want to set the string to wear sunscreen if the temperature is over 25. Otherwise, we're going to set it to bring a jacket. Now, we can do this with an if statement. You already know how to do this. If temperature is greater than 25, then we're going to set advice to wear sunscreen. Otherwise, that's what this else is here. And then finally, we print the advice.

### Ternary vs if statement [1:11]

That's not incorrect, but with the ternary operator, it can be done with two lines of code instead of five lines of code that you see here. So, how does this work? Well, let's take a look. We're declaring a constant called quick advice, and we are assigning the right-hand side to it. That's all

### Breaking down the syntax [1:33]

of this. But, what is this line of code? There's all of these strange characters in it. But, if you break it down, it is exactly like this if statement up here. There are three parts. The first is the condition. Is temperature greater than 25? And you can think of this question mark as answering a asking a question. That's kind of just like this part, if temperature is greater than 25, except this is asking is temperature greater than 25? If it is, then this is what the value should be. Otherwise, that's what this colon is. It separates the true and false results. Otherwise, we want to use this string instead. And this all happens in one line. So, asks a question. This is the condition. If it is true, then this value gets used, and it gets assigned to quick advice. Colon separates the true result with a false result. So, if this condition is false, then it's going to say bring a jacket, and it's going to assign that to quick advice. And then finally, this line just prints it out.

### How it works (recap) [2:57]

So, pretty straightforward, right? It just looks like this if statement. Condition. If it is true, then this. If it is false, then this. But, it's all just written out in one line. It's kind of like a shorthand. So, let's take a look at some other extensions of that basic understanding. Here are some common ways to use this operator aside from what you just saw.

### Common use cases [3:23]

Here is a logged-in Boolean. Boolean is true or false. So, we're going to declare a new constant called greeting equals, and then we're going to use the ternary operator. So, is this true? If it is, then let's say welcome back. If it's false, then let's use please sign in and assign that to greeting instead. In this case, logged-in is true, so it's going to use welcome back. And here, that's what the output will be. You can do it inside string

### String interpolation example [3:54]

interpolation. So, that's where you substitute a variable or an expression into a string. So, here we have item count equals one. This is going to say you have one, and then do we want to use item or items? This is where we use the ternary operator. Does item count is it equal to one? If it is, then use the word item. If it's not, then use items. This is a great example. All right, because it's going to print out you have one item in your cart and not one items in your cart. That's another great way to use it. So, here it's kind of mimicking a grocery store. When did grocery stores have memberships? So, if you are a member, then you get 20% off, price times 0. 8. Otherwise, if you're a non-member, you're going to pay the full price. And finally, you can do some pretty crazy stuff with the ternary operator.

### Nesting (not recommended) [4:56]

You can nest them together, but it gets really confusing and hard to read. So, that is not recommended to do. All right, and here's the challenge, pass or fail. Pause the video, try this out on your own. I recommend that. Otherwise, let's resume the video and try this out for ourselves. Challenge. So, what is it that we want

### Challenge introduction [5:19]

to do here? We want to create a constant called test score out of 100. Using the ternary operator, create a constant called result that equals pass if the score is 50 and above, or fail if it's below 50. And then print out a single statement that says score is this and result is this. It should be exactly three lines of code.

### Challenge walkthrough [5:42]

code. Okay, so that is the challenge. We'll say let score equals 100. Let result if We're going to have to ask the question now. What happened here? I didn't even do this challenge before. But, that's basically the answer. Is score above 50? Question mark. If it is, then let's assign passed to the result. Otherwise, let's assign failed. And then let's print out the statement here using string interpolation. We are going to say score Let's just copy this and paste that in here. We're going to substitute the variables. So, instead of 72, we use backslash and substitute score in there. And then finally, for the result, we're going to do the same thing, backslash and then whoops, brackets like that. And then we're going to put in the result there. So, then that is going to print out the statement here. Score is 100, result is passed. However, if this happens to be 40, then we are going to get score is 40, and the result is fail.

### Wrap-up [6:55]

Hopefully, that makes sense. It's got a scary name, but it's actually pretty handy and easy to understand. So

### AI practice prompts [7:02]

this prompt is really great because it's going to quiz you on some of the stuff that we just talked about. This helps you understand when to use a full-out if statement versus a ternary operator. And this one helps you get more practice. So, again, these go over here. Just paste the prompt in there and get an extension to this lesson. I hope you

### Outro [7:25]

found this helpful. I'll see you in the next one.
