1. Home
2. Training Library
3. Programming
4. Programming Courses
5. Using Advanced Techniques in Swift

# Switch Case

2
Struct Overview
PREVIEW6m 10s
4
6
Tuples
7m 23s
7
Guard Let
5m 52s

## The course is part of this learning path

Start course
Overview
Difficulty
Duration
51m
Students
3
Description

This course focuses on advanced techniques in Swift. You'll be introduced to structs and how we can use them in our code.

Transcript

Hi. Within this lecture, we're going to learn about Switch Case control statements. In fact, this is not a very advanced topic, but we haven't had the chance to see this at this point. So, we're going to see this so that you know how to work with switch cases. This is kind of an if statement. This is kind of an if check, but it has a rather different syntax and rather different usage. You're going to see what I mean. So, let me comment out this print. So we will have a good look at over here and let me comment out these lines as well so we will have a clear look, okay? So now I believe we have to comment out some other lines as well like this. So, let me run this one more time. Yap, here we go. We have a clear look. Now, let me come over here and show you the switch case usage. And in order to do that, I'm going to show you the remainder syntax as well. We haven't seen this before as well, so that we will learn two subjects in this lecture. For example, I have an integer called myNum and it's 11. So, what if I want to know what will I get as a remainder if I divide 11 by 2? So, if I divide 11 by 2, I will get the remainder of 1, right? If I divide it by 3, I will get the remainder of 2, right? If I divide it by 4, I will get the remainder of 3. You can do this easily on your mind but if it was a too large number then you would have the difficulty, so let me show you how this goes with Swift. So, you write myNum and have a % over here like this, okay? And then say the number that you want to divide it with. For example, now let me print out this myRemainder variable and you're going to see it's going to print out 1, because if I divide 11 by 2 it will be 1. And if I divide 11 by 3, the remainder will be 2. So, it's working. So, if I divide my number by 4, the remainder will be 3. Now it's working. Now suppose that I want to have an if check on this remainder, so let me write this, if myRemainder is 1, then I'm just going to print out "it's 1," okay? So, it will be like this. And else if myRemainder is 2, I'm going to print out "it's 2." And then one more time, else if myRemainder is 3, then I'm going to print out "its 3." So, it goes like this. So you understand the structure right now. Now, if I run this, of course it's going to print out "its 3" and it's working, but we can do this easily with switch case as well. So, let me show you how to do that, how to do the same thing with switch case control statement. So, switch cases can be very helpful if you have a lot of "else if" statements. So, it goes like this, you say switch, okay? You use the switch keyword and then you have to specify the variable that you're going to switch the value. So, in our case it's myRemainder. So, I'm going to write switch and say myRemainder, okay? And then, I'm going to open a curly brace and this will be my coding block, but this time rather than saying if, or else if, or anything, I'm going to bring in some case keyword. So, this means that, if the value of my remainder is 1, then what I'm going to do? As you can see I'm using a column this time rather than curly brace and it's the first time that we are seeing this, but it's the syntax for switch case. So, if you say case 1 and hit 'Enter' you can write what will happen if this is 1. So, I'm going to print out case 1, "it's 1" and in the case 2, I'm going to say "it's 2," okay? And in case 3, I'm going to say print( "it's 3"). And we have to provide a case default over here as well. So, default means if this doesn't fall into any category that I have defined above, then this will have this value. So, this is kind of an else statement. And I will just call this, none of the above. So, if I run this right now, I will get the exact same result that I got in my if control statement. So, let me comment this out so that we can see the switch. So let me come over here. If I run this, you will see it's saying that "it's 3." Now it's working and you know how to run switch cases. And sometimes it's a lot easier to work with switch cases because you can do this as well. So, you can say that case 1 - 3 and let me delete all of this. And if the case is between 1 and 3, then it will print out 1 - 3. So, if you have a lot of else ifs, then just remember that you can use switch cases in order to write this more efficiently. And if you have a case like this where you can specify some range from 1 - 50 for example, again, switch cases will be very helpful for you. Now, let's stop here, and within the next lecture, we're going to see Breakpoints.

Students
420
Courses
55
Learning Paths
3

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.