else if matlab
This is a topic that many people are looking for. thevoltreport.com is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, thevoltreport.com would like to introduce to you If, Else, and Elseif Matlab Statements. Following along are instructions in the video below:
So its really silly, but one of the most powerful things you can ever possibly do in programming is write an if statement, because otherwise youre 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 thats not the problem. The problem comes in actually doing like stuff. Thats programming. So ifs, elses, and else-ifs – thats what were 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 Im a pirate and zero otherwise. And so it would be very straightforward to say, “If Im a pirate, (I cant spell) Im pirate, not a pilot…” Okay so technically I know were just saying, “If I am a pirate,” but Im gonna say, “If I am a pirate equals one.” So dont be like, “Oh my god!” You dont have to do that. I know that. Im just doing it because, you know, Im teaching and stuff. So if I say, “If I am a pirate” equals one, then I can say “then display yarggg.” Isnt that what pirates say? I dont know. If I was a pirate, Id probably say something more sophisticated, but anyway… So Im a pirate, so it displayed yarggg. And then you know I did it for real. So yarggg. Okay, if Im not a pirate its not gonna do anything. Thats very exciting, right? Okay, so youll notice that I use two equal signs, and thats because it – remember, whenever youre asking MATLAB, “Are these two things equal?” Then youd 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 its mad – thats 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 dont know, what do people who dont… arent pirates… “Have some sherry.” People who arent pirates drink sherry. “Okay, so if Im not a pirate Im gonna say, “Have some sherry.” Okay so theoretically, I could have two of these, right? I could have the one that says if Im 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 dont. So yarggg, not yargggg. And it doesnt 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 Im either a pirate, or Im 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 Im 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 Im 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 Im a pirate. So its a yarggg. Make sense? Kind of? Okay, now you might say, “Well, maybe Im not a pirate, but I could be something else.” So lets actually create a new scenario thats 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?” Thats better. Okay, so Im gonna ask the user how many puppies they have. Now, I could say, “If they have…If they have
five puppies, then thats awesome. And then I could say – otherwise, I could say, “Onoes!” Because thats 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 thats why that doesnt work. I called the variable beautiful. So how many puppies do you have? Five. Awesome! How many puppies do you have? Nine. Onoes! Because thats 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, thats even better, right? So I could say else if – and this is weird because theres no space – and look how it started to freak out and tried to move it. Its like, “Oh, you must be doing something else.” And Im like, “No, thats an F.” And its like, “Oh wow Im gonna put it back here and make it blue.” So I could say else if. If I have more than five puppies, its gonna say, “Thats amazing.” Oops, I doubled that. “Absolutely phenomenal!” Now, otherwise, I could say thats not enough puppies. Okay so this is how this will work. Im gonna run it. How many puppies do you have? I have five puppies, and it goes, “Awesome!” Because its gonna do these in order, so its gonna say if I have five puppies, awesome! If I have more than five puppies, its gonna go, “Even better!” And then if I dont have enough puppies, its 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, its not gonna work. it just tells you. You cant even run it. So if I run it like this, its absolutely great and it works and its 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, its done. It doesnt look at anything else. So if I change this to greater than or equal to five, its not going to show both of the statements. So by saying I have five puppies, its just gonna show the first one and its gonna exit the statement, because its like, “I already met your requirements. Im out of here.” So if beautiful equals five, its gonna display “Awesome!” but then Ill say, “Well -” Thats not even right, because thats not… Yeah, greater than or equal to five, I did that right. So no matter what, its 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 its not equal, say this.” Then whats gonna happen is this guy here is never gonna get run, because Im clearly either equal to five or not equal to five. So even if I put three, its going to show this one, because its like, “Well three is not equal to five so Im going here.” So you really want to be careful about your else if. I think thats kind of annoying about programming is it does exactly what you tell it to do. So if you tell it to do something incorrect, its going to do that, and its gonna be like, “Well, thats 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 youre looking for. So now, you could end up in a situation, well say that youve 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 cant spell) less than or equal to ten.” So I could say that if I have between five and ten puppies, then thats good. Now MATLAB is yelling at me. See? Its like
wants you to have this double and to make that go away, and I dont know how to fix that, and I dont remember why it does that, but anyway you could put a double and, and I think in MATLAB for the most part, its not gonna make a difference. Im gonna leave it the way it is. So I could say I have nine puppies and it says, “Thats absolutely phenomenal!” But now if I say I have twelve puppies, its gonna say thats not enough puppies, and youre like, “That doesnt make any sense.” And then you go, “Wait a second. Thats 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 dont have less than ten. Doesnt seem like I spelled “beautiful” right. I dont know – you ever just like stare at a word and… Im gonna have to Google this in a second. Beautiful? No, thats not right. I dont know. Anyway thats really bothering me, so Im gonna change them all. Okay there. Now theyre all messed up. Okay. Because it just doesnt look right. Okay, so thats absolutely phenomenal. So now I have a situation where if I have 12 puppies, its a going to be like, “Thats 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 dont want to say… Well, I guess I could say greater than or equal to, but Im just gonna go with more than ten puppies. Well, because if I had ten puppies, its 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, its not gonna like ruin it, but its going to be confusing to me later, because its not ever going to go here when its equal to 10. So I could say this is actually too many puppies. So if I have 12 puppies, its 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, its never gonna say that I have too many puppies. Because thats going to fit into this category. So even though it doesnt matter (ish), its also very confusing to someone whos trying to edit your code later. And this is like really important code. So you dont 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 its not going to. So, and I could even say, “Well, lets 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 cant have negative. Stupid. So I can have zero puppies and it says thats 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 cant have negative puppies!” Im like, “I dont. I have zero.” So you really want to make sure that youre 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? Thats not enough, though. And I cant have negative puppies, and thats kind of too many puppies. Okay, so thats 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.
if, else, elseif, MATLAB (Programming Language)
Thank you for watching all the articles on the topic If, Else, and Elseif Matlab Statements. All shares of thevoltreport.com are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.