‫-: Hi. Within this lecture we are going to see
‫what are if controls.
‫So we are going to control some condition, okay?
‫We are going to see if a condition holds or not,
‫and we are 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 these if controls.
‫Okay?
‫And let me put it in my playground's 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 are gonna start
‫by creating our own variable to work upon.
‫So we are gonna go like this.
‫We are going to create an integer,
‫and we are gonna 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 where are my age? 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 my age is less than 30,
‫then,
‫do this.
‫Okay?
‫So like print 30 minus.
‫If I run this,
‫you will see that this doesn't print anything
‫because whatever you write inside of this code block
‫we'll get run 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 are not doing anything,
‫we are not printing out,
‫we are 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, right?
‫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 beside itself.
‫But if I write something inside of my code block
‫it will only get executed
‫if the condition holds.
‫So this is great, right?
‫It gives us the power to run code
‫whenever we want
‫when a 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 state
‫what do you want to happen?
‫If this condition doesn't hold.
‫For example, you can say 30 plus.
‫And as you can see,
‫we will see the 30 plus 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 gonna say else if
‫so, if my age is less than 30, print this.
‫But if my age is not less than 30
‫else if my age
‫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 ands
‫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 an equal to.
‫Greater than an 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 pipe, double and.
‫So double pipe stands for "or",
‫and double and stands for "and".
‫So in an "and" situation,
‫you just control to see
‫if both of the conditions hold,
‫like "else if" my age is greater than 30,
‫and "else if" my age
‫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
‫I'm in the thirties, okay?
‫So "else",
‫now I can state an "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'll print out 40 plus, 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 plus, right?
‫Here we go, we see the 40 plus.
‫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 my age is between 40,
‫and my age is less than 50.
‫Okay?
‫So I can do some else again,
‫and I can print out 50 plus if I want.
‫Now if I run this rather than saying
‫yeah it'll say again 40 plus because
‫we are in the 45, one more time.
‫So let me do this, something like 90.
‫It'll say 50 plus.
‫Okay?
‫here you go.
‫So we can check to see if a condition holds
‫with if controls and we can
‫state whatever we want to do
‫if that condition holds.
‫So this is actually 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'll give me true, right?
‫But if I add some "and" operator,
‫and if I say
‫five is less than seven.
‫If I run this
‫it will give me true again because why?
‫Because both side of the condition holds.
‫So three is actually less than five,
‫and five is actually less than seven.
‫But if I change one of this,
‫and if any of this doesn't hold,
‫it won't give me true
‫because "and" means both side 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 okay, 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.
‫Okay?
‫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 gonna use if statements
‫in most of the apps that we are gonna build,
‫obviously and we will practice them much more later on.
‫So right now,
‫we are going to stop here
‫and within the next lecture
‫we are going to continue with the functions.