Contents

keyboard_tab

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
2h 50m
Students
34
Description

This course will provide you with a comprehensive understanding of the fundamentals of Swift. We're going to learn about variables, constants, arrays, dictionaries, sets, if statements, and more! 

Intended Audience

This course is designed for anyone who wants to:

  • Learn about iOS development and coding
  • Move into a career as an iOS developer
  • Master Swift skills

Prerequisites

To get the most out of this course, you should have some basic knowledge of iOS.

Transcript

Hi. Within this lecture, we're going to see what are 'if' controls. So, we're going to control some condition. Okay.

We're going to see if a condition holds or not and we're going to define what to do if it holds or if it doesn't hold. So, let me create a new Playground. I'm going to name this IfControls. Okay. And let me put it in my Playgrounds folder over here so that I can share it with you guys later on. So, let me start by deleting this automatically generated line. And we're going to start by creating our own variable to work upon. So, we're going to go like this. We're going to create an integer and we're going to control to see if it is under some certain level or if it is below or above some certain level. Okay. What do I mean? Let me just do it. So, var myAge = 32. And I'm going to check to see if my age is actually less than 30 or more than 30. 

So, we use the keyword 'if' and it's fairly easy. You just write it like this, if myAge is less than 30, then do this. Okay. So, like print("30 -"). If I run this, you will see that this doesn't print anything because whatever you write inside of this code block will get wrong if only this condition holds. Right now, as you can see, my age is not less than 30. My age is 32. And so, we're not doing anything. We're not printing out. We're not executing the code, like in the while loop or in the for loop, what happens if the condition doesn't hold? We got out of the loop. 

So, this is the same thing. So, if I print something in here, for example, you will see it on the logs. Okay. It doesn't affect anything besides itself. But if I write something inside of my code block, it will only get executed if the condition holds. So, this is great. It gives us the power to run codes whenever we want, when condition holds, when some number exceeds something, when I don't know when the time runs out. Okay. So, we can check to see these conditions and we can say else. Okay. If you say else and open a new curly braces, you can say what do you want to happen if this condition doesn't hold.

For example, you can say 30+. And as you can see, we will see the 30+ in the logs. As you can see, it's here. Because my age is now 32, this didn't hold and since I have given an 'else' statement, it printed out the else statement. It got executed the else statement. And in fact, this isn't the only way to go with the 'if' controls as well. We can have as many as control points in an 'if' statement. I can check for if my age is 32, if my age is 33, if my age is 34. Okay. And to do that, rather than saying 'else', I'm going to say 'else if'. So, if myAge is less than 30, print this, but if myAge is not less than 30, else, if myAge is actually greater than 30, but also we can use a combined statement in here like an 'and' or 'or' operation. 

So, what do I mean by that? I can go and do something like this. With double &&, I can just add one more statement. So, let me tell you about this a little bit so we can continue. So, we have less than, greater than and we have seen the equal or not equal. Okay. We have even seen less than equal to, greater than equal to, but we haven't seen 'and' and 'or'. So, how do we use 'and' and 'or'? In an and operator, we say that both conditions must hold. Okay. And in 'or' operators, we say that one of the conditions must hold. And in order to do this 'or' operator's sign, you have to press option or alt in your keyboard with the dash. So, this is piping by the way, pipe sign. ||, double end. So, || stands for 'or' and && stands for and.

So, in an 'and' situation, you just control to see if both of the conditions hold, like else if myAge is greater than 30 and else if myAge is actually less than 35 or something. So, this doesn't make sense right now because my age is greater than 30 and it should be 40. Then I can print something like it's like I'm in the 30s. Okay. So, else, now I can say then else, else means right now if this condition doesn't hold. Okay. And if these conditions do not hold as well, then come back to do whatever I write in the else. If these conditions hold, it will do what we told the if statement to do and else, it will print out 40+ for example. Let's try this a little bit. Let's run this and we will see 30s because our age is 32. Okay. And let me change the value and test this more.

So, let me do this something like 45. If I run this, I will see 40+. Here we go. We see the 40+. So, our algorithm does work. And if I do something like else if the age is between 40 and 50, 50 and 60, I can add as many as I want, like as if myAge is between 40 and myAge is less than 50. Okay. So, I can do some else again and I can print out 50+ if I want. Now if I run this, rather than say age, it will say again 40+ because we are in the 45 one more time. So, let me do this something like 90. It will say 50+. Okay. Here you go. So, we can check to see if a condition holds with if controls and we can say whatever we want to do if that condition holds.

So, this is actually a great thing to learn in programming languages. So, let me talk a little bit about this 'and' or 'or' operator in a little bit more detail. So, if I say something like this, three is less than five, it will give me true. But if I add some 'and' operator, if I say five is less than seven, if I run this, it will give me true again. Because why? Because both sides of the condition holds. So, three is actually less than five and five is actually less than seven. But if I change one of these. And if any of this doesn't hold, it won't give me true because 'and' means both sides of this equation is true. Okay. So, if I do it like this, eight is less than seven.

This won't give me true even though three is less than five because this is 'and'. And the logic brings us this. If I want to use it in a way that if one of the conditions hold, give me true anyway, then I have to use 'or', okay, not 'and'. I can just say three is less than five. Okay. For example, this is 'and' and this is 'or'. So, in here, let me do something like this, three is less than five or double pipes eight is actually less than seven. So, this will give me true. Because why? Because we didn't say 'and'. This side of the condition holds. The other side doesn't hold but it doesn't matter for us because we used 'or' in this case.

So, you can use 'and' operations and 'or' operators in your 'if' controls, 'if' statements to check and see if a condition holds. If you want both sides of the equation true, you must use 'and'. If you just want one side, then you should go with 'or'. So, do we use if statements with numbers or integers only? Of course not. We can use it with strings as well. For example, let me create a string called James. I can check to see if my string is actually James or not. So, let me open my curly braces and I will just print out something like true and or yes. 

So, as you can see, we see the yes. If I change some letter over here rather than making this capitalized, if I make this lower case,it won't say yes because it has to be exactly the same in order these two strings to be equal. Okay. So, we can use if statements with strings, with booleans, with numbers, whatever we want. We're going to use if statements in most of the apps that we're going to build obviously and we will practice them much more later on. So, right now, we're going to stop here. And within the next lecture, we're going to continue with the functions.

 

About the Author
Students
909
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.

Covered Topics