# Learn Swift for Beginners 2026 - Lesson 9 - Switch Statements

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

- **Канал:** CodeWithChris
- **YouTube:** https://www.youtube.com/watch?v=LWqMRAx05RU
- **Дата:** 29.04.2026
- **Длительность:** 9:47
- **Просмотры:** 387
- **Источник:** https://ekstraktznaniy.ru/video/49668

## Описание

In this Swift tutorial, you'll learn how to use switch statements in Swift, one of the most powerful tools for writing clean, readable code when handling multiple conditions.

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

NASA website for gravity values:
https://solarsystem.nasa.gov/planet-compare/

🕐 Timestamps
0:00 Introduction & what is a switch statement
0:26 Switch vs. if/else — when to use each
1:03 Switch statement syntax walkthrough
1:50 Handling multiple values with commas
3:16 The "exhaustive" requirement & default case
4:30 Other switch features (ranges, wear clause)
5:28 Challenge overview — planet gravity
6:13 Looking up gravity values on NASA's site
6:48 Coding the challenge in Xcode
9:23 Going deeper with AI prompts

🚀 New to Swift? This lesson is part of a beginner-friendly Swift course. Start from Lesson 1 if you're just getting started! Here's the playlist: https://www.youtube.com

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

### Introduction & what is a switch statement []

Hello, welcome back. We're going to continue on with our Swift lessons. So, in chapter two, you learned about if, else if, and else statement. And this allows your code to make decisions. Run this code if this condition is true, otherwise other condition is true. Now, there may be cases where you run into a lot of different options, and your if statement gets really, really long. You then want

### Switch vs. if/else — when to use each [0:26]

to use a switch statement instead, and it allows you also to make decisions, but it's designed for if there are a lot of different options. So, the example here in this lesson is a vending machine. If you are coding the logic for a vending machine, you know the user can punch in different codes. If it's A1, then maybe you get this option. If it's B2, you get this other option. If it's C1, you get, you know, you get the idea. Your if statement would be so long. It's more elegant in this case to use a switch statement. So, let me show you how that looks and how that works inside of an Xcode playground.

### Switch statement syntax walkthrough [1:03]

So, let me just delete this for a second. Let's say that we have a constant called day, and it's set to Monday. And we want to print out a statement depending on what day it is. Now, we could write an if statement and say if day equals Monday, then do this. If else if day equals Tuesday, then do this. You get the idea. But, here's how you can use a switch statement instead. So, you use the keyword switch, and then you put in what you want to switch on. I know switch is kind of a strange keyword, but think of it as you're switching between different options depending on what the value of that variable or constant is. So, we're going to put day here, and then we're going to open up a pair of curly brackets like that.

### Handling multiple values with commas [1:50]

And in between these curly brackets, you're actually going to put all of the different options that you want to handle. So, for example, for day, if it is Monday, then let's print the start of the week. And how we do that is we use the case keyword followed by a space, followed by the value that if day is this value, then you want to do something. The value that we want to handle is Monday. You're going to put a colon, and right under that, you're going to put the code that you want to execute if day happens to be Monday. We want to print out start of the week. If you want to handle additional cases, you just start a new line. You use the keyword case again. And this time, let's handle Tuesday. But, let's say for Tuesday, I want to print out middle of the week. And for Wednesday, I also Instead of creating a case for Tuesday and a case for Wednesday, I can actually use comma, and I can write the other value that this case falls under, and then I can print out middle of the week. So, if day happens to be Tuesday or Wednesday, it's going to come into this case and print out this. Now, one thing you might be wondering is what is this error? Switch must be exhaustive. What does that mean? It

### The "exhaustive" requirement & default case [3:16]

means that you must handle all the options. So, it's basically saying if day is something else that's not one of these two cases, what should we do? Well, you have to use a default case. It's like this. So, if day happens to be something other than any of the cases that we've specified, it comes to default. unknown day. we'll just print that out. So, in this case, now it'll run and it'll print out unknown day. But, if it's something like Wednesday, then it's going to come into here. Now, I want to show you something else that's kind of important to know, and that is for example, if it's Monday, it comes into here as it should, but it doesn't continue on and print out the rest of this. Right? It just executes the code in its own case, and then it exits out of the switch statement. It doesn't carry on the rest. It's kind of like an if statement in that way. When it hits a condition that is true, it executes that code and then it exits. So, that's basically how a switch statement works. Let's take a

### Other switch features (ranges, wear clause) [4:30]

look at some other variations and special things you can do with it. So, this is just the explanation that I just gave you. Here is another way to use a switch statement. You can specify ranges. So, if it's an integer and you want to do something if it's between this number and that number, you can use three dots like that and specify a range for the case. You can also do multiple values per case, which I've shown you how to do using comma. So, here, this case handles four, five, and six. And you can also do a where clause. This is a little bit more advanced, so I'm not going to go over this. But, if you're interested, you can read this. Now, let's take a look at the challenge. Again, pause the video, try this challenge out for yourself if you would like to. That's how you're going to learn and get practice with the switch statement. Otherwise, let's continue

### Challenge overview — planet gravity [5:28]

watching this video, and let's go through it together. So, what we're going to do is create a constant called planet, and we're going to assign it any name. Let's say Earth. We're going to write a switch statement that prints out the gravity fact for at least five planets. Something like Mars is 0. 38 times Earth's gravity. And include a default case that prints out unknown planet. It wants you to group any two planets with similar gravity into a single case using comma. So, I have this website here from NASA, which lets you see all of the gravity values for all of our planets in our solar system. So, let's take a look at

### Looking up gravity values on NASA's site [6:13]

the surface gravity. So, you can see Earth is 9. 8, Mercury is 3. 7, Mars is 3. 71, so that's similar. We're going to want to group Mercury and Mars together. Venus is 8. 87. And Jupiter has crazy gravity, 24. 79. Saturn is 10. 4, but Uranus has 8. 87, which was kind of like Venus right here. So, I don't think I'm going to write everything out. You get the idea, but we'll do a few of them together.

### Coding the challenge in Xcode [6:48]

Let's go back to Xcode, and let's write the challenge. This is going to be the challenge. We're going to let planet equal do Mars because we are comparing it to Earth's gravity. So, we're going to write the switch statement. Going to switch on the variable Mars, or the constant, I mean. And here, we're going to have the different cases. So, if it is let's just start with Mars. colon Then, we're going to want to print out Mars 0. 38 times Earth's gravity. We're going to have a default case. Print out un- known planet. Now, let's take a look at Mars. What did we have? Mars is 3. 7, but Mercury is pretty similar. So, let's group those together, and we can do that using comma. Mercury Now, let's do a different case. case of Venus Print out Venus is Here, we need to do a calculation. We need to divide Earth's gravity by Venus's. And what we can do, if you remember back in string interpolation, we can use backslash followed by a pair of rounded brackets to substitute a variable in there. But, we can also do math in there, which is what we're going to do. So, let's get the values. So, Venus is 8. 87, Earth is 9. 8. Let's just grab that. So, say that divided by 8. 87. And then, we're going to want to do the X because that's what it is there. Let's see what we get if we put in Venus. Wait, sorry. It should be the other way around. We should be dividing this way, so that Venus actually has less gravity than Earth. So, yeah, Venus has 0. 9 0 X Earth's gravity, and so on. So, you get the idea. And that is how you do that challenge.

### Going deeper with AI prompts [9:23]

If we go deeper with AI, this prompt helps you understand when to use if statements versus switch statements. This one shows you an incorrect switch statement, so that you can see what not to do. And finally, this builds an exercise for extra practice. All right, I hope you've enjoyed this lesson. There's another tool in your arsenal now, and I will see you in the next video.
