2 - 2 - Conditionals (10-39)

6

Click here to load reader

Transcript of 2 - 2 - Conditionals (10-39)

Page 1: 2 - 2 - Conditionals (10-39)

8/13/2019 2 - 2 - Conditionals (10-39)

http://slidepdf.com/reader/full/2-2-conditionals-10-39 1/6

[MUSIC] In this video, we're going to talkabout changing the flow of control of yourprogram.So far, we've been talking about programsthat execute statements one after anotherafter another.So, you can just take a look at the codeand you can scan down the page and you'regoing to see how the program executes.Now, that's not quiet true, we haveintroduced functions and functions changethe flow of control slightly.When you get to a function call, youactually go off, you execute thestatements inside the function one by one,then you come back and you keep executingone by one.Now,This makes your programs a little bit moreinteresting but I still can't change theflow of control of my program based onvalues in the program.I might want to do something different

based on data that I have available to meinside the program, okay?So, let me illustrate this with a verysilly example, okay?Imagine that I'm a computer and I'mwalking around running a computer programand that computer program tells me to giveaway $twenty, alright?So, I walk up to you, I say, hey, here's$twenty, okay, and then I walk to the nextperson, hey, here's $twenty.I walk to the next person,Hey,

Here's $twenty.Now,If I run this program, I'm going to gobroke really quickly,Okay?So, this is not a very intelligentprogram.I'd like to behave differently based onwho I'm talking to.So perhaps I want to come up to you,You're my friend.I know you need $twenty I say, here, goahead, have $twenty, Okay? Then, I walk up

to the next person.They're a stranger.I don't know them.I don't want to give them $twenty so Idon't give them $twenty.Okay?I want to change my behavior be, based onwho I'm talking to.Now, this is what conditionals accomplishin computer programs.

Page 2: 2 - 2 - Conditionals (10-39)

8/13/2019 2 - 2 - Conditionals (10-39)

http://slidepdf.com/reader/full/2-2-conditionals-10-39 2/6

You are checking the value in the program,in this case, it would be the person thatI'm talking to in my silly analogy, okayBased on that value, you execute differentlines of code.So, let's see how that works Alright. Toillustrate conditionals, I want to use mysilly giveaway $twenty program.So, you can see here I have a functioncalled Greet that takes two arguments.One is going to be a Boolean, whether ornot you're my friend.So, this is going to be true or false.The other is the amount of money I have,okay And then, I'm going to do whatever Ido when I greet you and I'm going toreturn the amount of money I have when I'mdone and so, you can see down here, I havethree calls to greet and the way I callgreet is I pass true or false, so thisfirst call,I'm greeting my friend.And I have, however much money I have.When I'm done, I update my money, and then

I print it,Okay?Then, I would greet someone who's not myfriend over here.So, this is false.And then, I greet someone who is myfriend.Okay.So, I start out,Let's say, I start out with $fifteen and Iwalk down the street, and I meet threepeople.What happens?

Well, I go into debt.At least I'm friendly.I say hi to everyone.And I'm in debt for $five and then I'm indebt for 25,And then I'm in debt for 45.Okay.I'm not a very good money managerobviously, right.So, let's improve this a little bit.Let's make me slightly more intelligentwhen I greet people.I'm going to check if you are my friend.

And if so, I will do these things, okay?So, in Python, the way that we can alterthe flow of control based on the value ofdata is to use an if statement, alright?And as you would expect from the Englishword if, if this is true, then I executethe code in this block.Like all other Python sort of constructshere, I end this with a colon, and then, Ihave a block of indented code.

Page 3: 2 - 2 - Conditionals (10-39)

8/13/2019 2 - 2 - Conditionals (10-39)

http://slidepdf.com/reader/full/2-2-conditionals-10-39 3/6

That block of indented code executes,alright, if the predicate, which in thiscase, is friend, is true, okay?So, I have a, the word if, then I havesome Boolean logic expression, in thiscase, it's just a Boolean variable, then Ihave a colon, then I have a block of codethat executes.Now let's try to run a program again.What happens when I run it now?Well.If you're my friend, I would say hi to youand give you $twenty and go into debt, butI meet someone who's not my friend I don'teven say hi, I'm not very friendly to thestrangers then I say hi to my friend againand go further into debt.Okay.That program is getting better.Now, I don't have to just put a Booleanvariable here.Okay.We talked about Boolean logic last time,and there's a reason for that.

I can put an arbitrary logic expressionhere, so let's be even smarter about ourmoney.Let's not just give it away to friends.Let's only give it away if we have it.So, if my, if I have more than $twenty,alright,, I will say hi to you, and I willgive you some money,Alright? Well, let's try this.Hm.I am no longer very friendly, right?I didn't give anybody money.Why?

Cuz I never have $twenty.So, this statement always ends up beingfalse,Right?The friend may be true or false butbecause it's in an and, if money is notgreater than, true than twenty then, thisis going to be false, always making theand expression false.Alright, this isn't really what I wanted.So, I can put.Something else, right?I want to do, I know what I want to do if

you're my friend and I have more than$twenty, Butif you aren't my friend or Idon't have more than $twenty, maybe Istill talk to you.Don't have the exclamation point.I'm not as happy.I don't have as much money,Alright? I'm going to do this otherwise soelse is a Python construct with a colonafter it again, right, and after the

Page 4: 2 - 2 - Conditionals (10-39)

8/13/2019 2 - 2 - Conditionals (10-39)

http://slidepdf.com/reader/full/2-2-conditionals-10-39 4/6

colon, I can have an arbitrary block ofcode that can be as many statements inhere as I want.But else is what you do if the predicateis not true.So, if the predicate is true, I run thiscode.If the predicate is not true, I run thatcode.Okay So now, what's going to happen?Well, at least I say hello to everybody.But I'm still not giving anybody anymoney, right?I even say hello to strangers, you know,Should I really be doing that?I don't, I don't really think I should sayhello to strangers.So, let's structure this even a littlebit, slightly more different, alright?There's another construct I can use, andthat's called elif,Alright?Let's say, elif friend,Then I talk to you, I say hello.

Otherwise, you're not my friend.Well,I've been friendly throughout this.Let's be mean.I'm going to print uh-huh,, and I'm goingto mug you.No, I would never do this, okay?But in the program, let's just pretend I'mgoing to take your money.Alright?So now, what happened here?What did this elif do?Okay.

So, first, we execute the if statement.We check if the predicate is true.If the predicate is true, then we executethe block of code underneath the if.And that's it.Then, we skip down here to return money,Okay? When the predicate is false in theif statement, we look at the next one,elif, and this is a shorthand for else if.So, else, if this predicate, which is justfriend is true, then I execute this blockof code.And I can have as many elifs as I want.

I can only have one if and one else but Ican have as many elifs in between if Iwant.And I don't have to have any elifs and Idon't have to have an else, right?So, I print hello here.If this one, this predicate was false,then I go down here, else, else alwaysworks.So no matter what, okay,

Page 5: 2 - 2 - Conditionals (10-39)

8/13/2019 2 - 2 - Conditionals (10-39)

http://slidepdf.com/reader/full/2-2-conditionals-10-39 5/6

So this means,If you're, if I don't have more than$twenty and you're my friend or if you'renot just my friend, then I'm going to takeyour money, alright? That can be very nicealright?Now, let's run this program, what happensnow?Okay, first person I walk up to is myfriend, I say, hello, I'm a little bitsad, I don't have a lot of money so I keepmy $fifteen dollars then.Second person, [laugh] I'm going to takeyour money Now, I have $25.Last person, hey, I'm pretty happy Now, Ihave $25, I say, hi and I give you mymoney alright?So, you can see that I can alter the flowof control of the program based on valuesthat are in the program.So based on the value of friend, andmoney, in this case.And it doesn't matter that there arearguments in the function.

These could be anywhere.They could be local variables, they couldbe global variables.I can change what code gets executed.This is a very powerful concept.I want to show you another example ofconditionals to help you get the hang ofthem.Now, in this program, I basically am goingto test if a particular year is a leapyear.And so, I wrote a function calledis<u>leap<u>year..</u></u>

That's a programmer's convention whenyou're just testing a predicate with afunction, you start the function name withthe word is.Basically, I'm testing year and returningtrue or false based on whether or not itis a leap year, okay?And this function is just one conditionalthat has several cases, alright?I'm testing three different things andreturning true or false based on theresults of those tests.Now, leap year test is actually pretty

simple if you ignore the corner cases.I need to just check is the year amultiple of four, if so, it's a leap yearotherwise, it's not.So, you can see up here.I test year modular four could equal zero.That means if I divide the year by fourand there's no remainder, well then, it'sa leap year so I return true and if it'snot, I exceed the else clause and return

Page 6: 2 - 2 - Conditionals (10-39)

8/13/2019 2 - 2 - Conditionals (10-39)

http://slidepdf.com/reader/full/2-2-conditionals-10-39 6/6

false,Okay?However, there are these two extra casesthat if the year is a multiple of hundred,then it's not a leap year unless it's alsoa multiple of 400,Right?Now, you'll notice that I have to executethese checks in this order,I will not get the right answer.I have to first check 400, then 100, thenfour, and then do the else on. I inviteyou to experiment and move these aroundand find out whether or not you get theright answer,Right?You know that you won't, cuz I told youbut you should at least convince yourselfthat you understand why.I also have a conditional down here thatshows, allows us to print out a nicemessage based on the result of this leapyear function.Okay, so let's run this.

Is the year 2100 going to be a leap year?No, it is not.Is the year 2000 going to be a leap year?Yes, it is.Is the year 2012 a leap year?Yes, it is.Is the year 2009 was that a leap year?No it's not.Okay. And you can play around with thisand see that it does catch all the casesby executing these conditional in orderthe if, elif, else, you're going to getthe right answer in all cases.

There are very few interesting programsthat execute directly in a straight line.So, the concept of conditional controlflow that I've introduced in this videoare critically important for pretty muchall computer programs.You don't want to give away $twenty toeverybody you meet, do you?[laugh] I certainly don't.Okay.So, I need to be able to have the abilityto change what I do based on the values ofdata in the program, okay?

And the ability to use arbitrary Booleanlogic and predicates gives you a powerfuland flexible mechanism to do so.So, you're going to be using conditionalsto change the code that you execute inyour programs throughout the rest of thiscourse and throughout your programing