If, Else, and Elseif Matlab Statements

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
So it's really silly, but one of the most powerful things you can ever possibly do in programming is write an if statement, because otherwise you're just kind of doing a little bit of like, just calculations. So if statements and stuff like that is what distinguishes your, like MATLAB and your programming from like kind of a simple four-function calculator, because anyone can be like, y is 5, x is 12, and z is X plus y or whatever. So anyone can do that, so that's not the problem. The problem comes in actually doing like stuff. That's programming. So ifs, elses, and else-ifs - that's what we're going to talk about. Ifs, elses, and else-ifs. Anyway, so basically the idea would be like, say I would say, "I am a pirate." And that variable is one if I'm a pirate and zero otherwise. And so it would be very straightforward to say, "If I'm a pirate, (I can't spell) I'm pirate, not a pilot..." Okay so technically I know we're just saying, "If I am a pirate," but I'm gonna say, "If I am a pirate equals one." So don't be like, "Oh my god!" You don't have to do that. I know that. I'm just doing it because, you know, I'm teaching and stuff. So if I say, "If I am a pirate" equals one, then I can say "then display yarggg." Isn't that what pirates say? I don't know. If I was a pirate, I'd probably say something more sophisticated, but anyway... So I'm a pirate, so it displayed yarggg. And then you know I did it for real. So yarggg. Okay, if I'm not a pirate it's not gonna do anything. That's very exciting, right? Okay, so you'll notice that I use two equal signs, and that's because it - remember, whenever you're asking MATLAB, "Are these two things equal?" Then you'd have two equal signs to like ask the question. So the double equal sign asks the question. The single equal sign is actually going to yell at you. You see how it turned red and MATLAB turned red and it's mad - that's because it should be a double equals sign. So, or else I could say...I could just as easily say, "If I am NOT a pirate, display -" I don't know, what do people who don't... aren't pirates... "Have some sherry." People who aren't pirates drink sherry. "Okay, so if I'm not a pirate I'm gonna say, "Have some sherry." Okay so theoretically, I could have two of these, right? I could have the one that says if I'm equal, and then I could say, "If I am a pirate is not equal to 1," then I can say, "Have some sherry." So I could have both of those. And then as my pirate variable changes, then I either have something or I don't. So yarggg, not yargggg. And it doesn't have to be equal to 0 since, note that I said "is not equal to 1"? I can actually go in and I can make that 9, and it would still come back with "Have some sherry." Okay, this is kind of silly though and why would you ever do this, because I'm either a pirate, or I'm not. And in cases that are so clear-cut, you can actually instead of saying, "If this, then this," and then, "If this, then this," you could say, "If I'm a pirate, display 'yarggg'. Otherwise, I want you to display, 'Have some sherry.'" So this is being an example of an if/else statement. So if I'm a pirate say, "yarggg." Otherwise I want you to say, "Have some sherry." So have some sherry, have some sherry, have some sherry, and now I'm a pirate. So it's a yarggg. Make sense? Kind of? Okay, now you might say, "Well, maybe I'm not a pirate, but I could be something else." So let's actually create a new scenario that's a little bit less stupid. Okay. Beautiful. Well, I could do it that way. What if, instead of asking you how much money you make, I say, "How many puppies do you have?" That's better. Okay, so I'm gonna ask the user how many puppies they have. Now, I could say, "If they have...If they have five puppies, then that's awesome. And then I could say - otherwise, I could say, "Onoes!" Because that's not enough puppies. Five is just not enough puppies. So how many puppies do you have? Five Oh, I misspelled puppies. I called it beautiful that's why that doesn't work. I called the variable beautiful. So how many puppies do you have? Five. Awesome! How many puppies do you have? Nine. Onoes! Because that's too many puppies. But you can never have too many puppies, right? So we have to fix this. So what we want to say is, we can change this to say if you have more than five puppies, that's even better, right? So I could say else if - and this is weird because there's no space - and look how it started to freak out and tried to move it. It's like, "Oh, you must be doing something else." And I'm like, "No, that's an F." And it's like, "Oh wow I'm gonna put it back here and make it blue." So I could say else if. If I have more than five puppies, it's gonna say, "That's amazing." Oops, I doubled that. "Absolutely phenomenal!" Now, otherwise, I could say that's not enough puppies. Okay so this is how this will work. I'm gonna run it. How many puppies do you have? I have five puppies, and it goes, "Awesome!" Because it's gonna do these in order, so it's gonna say if I have five puppies, awesome! If I have more than five puppies, it's gonna go, "Even better!" And then if I don't have enough puppies, it's gonna yell at me for not having enough puppies. So else, or if, else if, and then else. You have to have them in this order. If you try to like switch the order, it's not gonna work. it just tells you. You can't even run it. So if I run it like this, it's absolutely great and it works and it's happy. This is kind of interesting that it actually does this in order, so I could say, "If beautiful equals five," once it sees awesome, or once it gets into this if statement, it's done. It doesn't look at anything else. So if I change this to greater than or equal to five, it's not going to show both of the statements. So by saying I have five puppies, it's just gonna show the first one and it's gonna exit the statement, because it's like, "I already met your requirements. I'm out of here." So if beautiful equals five, it's gonna display "Awesome!" but then I'll say, "Well -" That's not even right, because that's not... Yeah, greater than or equal to five, I did that right. So no matter what, it's never going to... Five is never gonna show both of these. So... So there. I could really mess this up and say like, "If beautiful is equal to five, do this." But if it's not equal, say this." Then what's gonna happen is this guy here is never gonna get run, because I'm clearly either equal to five or not equal to five. So even if I put three, it's going to show this one, because it's like, "Well three is not equal to five so I'm going here." So you really want to be careful about your else if. I think that's kind of annoying about programming is it does exactly what you tell it to do. So if you tell it to do something incorrect, it's going to do that, and it's gonna be like, "Well, that's what you told me to do." So you have ifs...You have if, you have else if, and you have else. And you can combine these in any way that you want in order to get the kind of, I guess logic that you're looking for. So now, you could end up in a situation, we'll say that you've got like a ton of puppy options so I could say, "Well, I can have more than five, but I also want to have (I can't spell) less than or equal to ten." So I could say that if I have between five and ten puppies, then that's good. Now MATLAB is yelling at me. See? It's like it wants you to have this double and to make that go away, and I don't know how to fix that, and I don't remember why it does that, but anyway you could put a double and, and I think in MATLAB for the most part, it's not gonna make a difference. I'm gonna leave it the way it is. So I could say I have nine puppies and it says, "That's absolutely phenomenal!" But now if I say I have twelve puppies, it's gonna say that's not enough puppies, and you're like, "That doesn't make any sense." And then you go, "Wait a second. That's exactly what I told the code to do." Because I have more than five and less than ten is what I need to get in here. So I have more than five, but I don't have less than ten. Doesn't seem like I spelled "beautiful" right. I don't know - you ever just like stare at a word and... I'm gonna have to Google this in a second. Beautiful? No, that's not right. I don't know. Anyway that's really bothering me, so I'm gonna change them all. Okay there. Now they're all messed up. Okay. Because it just doesn't look right. Okay, so that's absolutely phenomenal. So now I have a situation where if I have 12 puppies, it's a going to be like, "That's not enough puppies," so I could be like, "Oh man, I need to add another conditional." So now if I have more than ten puppies... So I don't want to say... Well, I guess I could say greater than or equal to, but I'm just gonna go with more than ten puppies. Well, because if I had ten puppies, it's gonna show this. Right? Because ten puppies is greater than 5 and less than or equal to 10, so the next case has to be greater than ten. If I put an equal sign, it's not gonna like ruin it, but it's going to be confusing to me later, because it's not ever going to go here when it's equal to 10. So I could say this is actually too many puppies. So if I have 12 puppies, it's gonna say, "This is actually too many puppies." So what I was trying to say about the equal sign is I could say greater than or equal - but when I put 10, though, it's never gonna say that I have too many puppies. Because that's going to fit into this category. So even though it doesn't matter (ish), it's also very confusing to someone who's trying to edit your code later. And this is like really important code. So you don't want to put that equal sign there, because that implies that at some point, it could be equal to, and it would go into this section. And it's not going to. So, and I could even say, "Well, let's add a condition that says if you have negative puppies, it yells at you. So try doing that. So see if you can add code that will make it yell at you for having negative puppies. Okay, so now if I run that, if I put in negative three... It can't have negative. Stupid. So I can have zero puppies and it says that's not enough puppies. So the difference between a less than or a less than or equal to, would be if I actually said this, it would be like, "You can't have negative puppies!" I'm like, "I don't. I have zero." So you really want to make sure that you're very, very, very specific anytime you write a greater than, or an equal sign, or less than or equal, or any combination of those. You really need to be like, "Okay, do I actually mean less than or equal to or its mean the standard I really mean greater than equal to or do I just mean greater than? That's not enough, though. And I can't have negative puppies, and that's kind of too many puppies. Okay, so that's kind of an introduction to if, else if, else--if...and you can actually have as many else ifs as you want. The last one is kind of the default; it catches all the other options, and you go from there.
Info
Channel: SnugglyHappyMathTime
Views: 107,921
Rating: 4.6587472 out of 5
Keywords: if, else, elseif, MATLAB (Programming Language)
Id: 0biuNnzYuDY
Channel Id: undefined
Length: 11min 42sec (702 seconds)
Published: Thu Feb 19 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.