Basics of Writing For Loops in MATLAB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
When you're getting started with while loops, it's important to note the main difference between four loops and while loops. So basically the idea are four loops, is whenever you know ahead of time how many times you want to do something. I can't spell, and then while loops, can't spell that either, is when you're not sure. Okay. So, whenever you're not sure how many times you want to do something, but you know you want to do it multiple times, then you're gonna use a While Loop. If you know precisely how many times you want to do something, then you're going to do a For Loop. So for example, I'm going to... I'm going to say, x = 1. I'm going to say, While x is equal to 1. So, I'm going to set x equal to 1, and While x = 1. I'm gonna do an input statement. For X is equal to put is this annoying yet? There's gonna be the answer is yes or no. Okay? So [singing] do do do... and I'm not going to suppress anything just so you can can you see what's going on. So, now I want you to run this code. So you go, pshoo. Oh... seriously? Oh, not input! Sorry. Menu. Menu, there we go. There we go, all right, is this annoying yet? Yes. Is this just annoying yet? Yes. Yes. Yes and it just keeps asking you right? Because you're like, this is stupid! When will this go away? I already told you that it was annoying. But it keeps asking me is it annoying? Okay, so why does it keep coming up? And the answer is, well, every time I click yes, x returns as a 1. And as long as X is equal to 1 it's gonna keep asking. So the only way to get out of this, is to get a return that's not equal to 1. And I can do that by either hitting the x, which will return to 0, or I can do it by right hitting no, which will return it too. So as long as I keep saying that it's annoying, it's going to keep asking me. It's kind of like a little sister or something like that. Is this annoying yet? Is this annoying yet? I'm not hitting you. I'm not hitting you. No, I'm not touching you. Okay, so anyway, so that's a While Loop. It's basically when you'd want to do something over and over and over again, until a certain condition is met. Now I can always say, While X-- instead of saying While X is equal to 1. I could say While X is not equal to 2. So, then it would be, if I ran this... It would be like, is this annoying yet? And if it says a 1. Then it's not equal to 2. 1 is not equal to 2. 1 is not equal to 2. 1 is not equal to 2. A 0 is not equal to 2. So, even though I'm exiting out of it, it's still asking me. And the only way to break out of it, is to make X actually equal to 2, is by saying nope. Kind of makes sense? So, While Loops are pretty good for getting data or input or processing input from a from a user. So... the only thing is, is you always have to remember to initialize... initialize your values. So like up here, I specifically had to say x=1. If I took that out, like that, and we try to run it, nothing would happen. So if I hit this, it says x is undefined, so it actually can't decide whether or not it's going to run the While Loop. Now if I, for some reason, had initialized it where X was equal to 2, then it's still not gonna run it. It's not going to do anything. It's just gonna say x=2. It's never going to ask the question, because it's like, well... I only run this when x is not equal to 2. But it is equal to 2, so I guess you don't want me to run it. So you really want to be careful, that's one of the first things people do wrong whenever they're doing While Loop. It's just like when you're doing For Loops. It's not initializing values properly and in advance. No it's not, I don't think. So, but like I'm saying, you can use this a lot with user input. So say you want to know I don't know, say you're going to ask them, "What is x-- what is 3 + 2?" Alright, so if I want to ask what is 3 +2? I could say well, if... x is not equal to 5, then I want to ask again. Okay and this is, this is not a good way to do this. And then I could say well if it's still not equal to 5, I want to ask again. If it's still not equal to 5, I want to ask again. It's still not equal to 5, I want to ask again. It's still not equal to 5, I want to ask again. So, basically what I've done is I've put in... 6, 7, [correcting self] 6 questions? Okay, so I'm going to ask what 3 + 2, and I'm going to say, I'm gonna miss it once. And then I miss it twice, and I miss it 3 times and I miss it 4 times, 5 times, and 6 times, and it just finally stops asking me. So, the nice part about a While Loop is, it's going to keep asking over and over, and over and over, and over and over, and over and over. So what I could do is instead of saying if X is not equal to five, what is it? I could say while X is not equal to five what is it? Okay, does that kind of make sense? So then what this is gonna do, what is it 0. No, what is it? What is it? What is it? What is it? I'm just gonna keep asking, as long as I get it wrong it's gonna keep asking me for the answer. When I finally get it right, it'll say, "Yay! Thanks." Now this is a little silly, because I'm actually having to ask this question twice. So what if I decide, oh I really wish it had a little space here. So I fixed that, but then I forget to fix him in the other spot. So it's fixed once but not all the time. So, generally rather than doing something like this, what I would do, is I'd set x equal to something that I know is not going to be 5. So, I set it equal to you know... that or something like that. So, I know that that's not going to be the right answer, so that it'll automatically go into the While Loop. And so, now it's really only asking you, [laughs] it's really only asked me at that first time. I like to get the right answer. Okay. Does that kind of make sense? Um... that's kind of 1 of the ways that you'd like to do a While Loop. Another thing that you can do with a While Loop, is you can make what we call infinite While Loops. So, let's say that I said x is equal to... so this is a very common mistake. So, what if I had this, I'm gonna use this exact same code. But, I'm going to, for some reason, I'm going to say Y. And... and I'm going to run this and... oops. Sorry. I'm going to go and go "clc, clear." And, run this and I'm gonna say what is 3 plus 2? And I'm like... 5. It's like no. What is 3 plus 2? And I'm like 5. No. What is 3 plus 2? And it's doing this forever and ever and ever. You're like this makes me no sense, because I keep getting it right. And I'm gonna put in you know, "burger." Burger. Oh, no. Still! "You are stupid," because you know, this code doesn't work. You're like, it's 5! Ahh! So, what's wrong with my code? So, basically what's going on, and I'm going to-- so what you need to do to get out of this. Is you need to hit ctrl C, so ctrl C. [laughs] But not type it, just I'm just typing it, so you can see what I was saying. But, you're gonna hit the ctrl button and the C button, and then it gets out of it. But this is an infinite while loop, because I screwed it up. I messed up big time. Because what I'm not doing, is I'm not changing the value that I'm checking on. So, this is also something you want to look at when you're doing a For Loop, is are you actually changing the value that you're doing the the check on. Is it ever possible to get out of the loop? So, obviously this is a bad thing, to do that. I like that. So, I'd have to fix it. Another thing you could do with the Infinite While Loop... so I could say... "more infinite while loops." I can say... well, x is equal to 5. And I'm going to say while x is greater than 3, I want to say X is equal to x + 1. So, basically the idea is, I want to set x is equal to 5 and every time I go through the loop, I want to add 1 to x. Until x is greater than 3, or until While x is greater than 3. Not until x is greater than 3, but While x is greater than 3. I want to keep adding 1, and obviously, the problem with this is that x is always going to be greater than 3. Because it started out as 1 and I keep adding to it. So, this is definitely not something that we like. So I want to go ahead and I want to hit ctrl C to get out of this. So I click down in the trial, or not the trial. I click down in the command window section and I hit ctrl C, and that will get me out of that While Loop. Now, what we just had here is a pretty interesting little While Loop thingy, really understanding limits or... endpoints? on While Loops. So if I have something like this and I'm gonna change it a little bit. So I'm gonna say while, so let's start out with. I'll do it, I'll just leave it this way. But instead of adding one, I'm gonna subtract one. So I'm gonna start off with X is equal to five. So let's let's think ahead about what this could possibly look like. Okay, so it's gonna be X is gonna be equal to five and then while X is greater than three I want to subtract one. So I'll say X 3 yes. So X is equal to X minus 1, which is 4. Is X greater than 3? Well 4 is greater than 3. Yes. So X is now equal to 4 minus 1 which is 3. Alright, is X greater than 3? No, because 3 isn't greater than 3, so we're done. X is still 3. Okay, so whenever I run this what I expect to see, is that it's going to come back with X is equal to 3. And that's exactly what happens. So then you think, okay, well I'm gonna take the same code and I say well what happens if X is, if I change it to while X is greater than or equal to 3. Okay so this is important, while X is greater than or equal to 3, I wanna use, to subtract 1 off of it. So now I say well is X greater than equal 3. Yes that stays the same. Yes that stays the same. But whenever I get here, I say well is 3 greater than or equal to 3. Well yeah yeah it is, yes. So now X is equal three minus 1, which is 2. And so then I say well is X greater than or equal to 3. No, because it's 2. No cuz it's not. No because it's 2 and so since it's two, it's no longer greater than or equal to 3, so I quit. So X is still 2. So X value X doesn't change it's now going to be 2. So here X been finishes out to be 2. Whereas, in this example, X finished out to be 3. And that's the difference between having the greater than or equal to vs. the greater than, the strictly greater than on the while loop. There's all kinds of stuff that you can do with these. And understanding kind of how these, how these work is is one of those, those things that you I think,. If it's not something that you get right away, it's just one of the things you got to play with it in order to get it to stick. So for example, we could come in here and I could say, what's really good practice I guess is what I'm saying, is to come in here and and practice and think about if you can anticipate with the output the code is going to be. So I have to say, X is equal to seven, and I'll say well while X is less than twenty, I want X to increase by three. Okay so take a minute, see if you can figure out what the output of that code would be. So while X is less than 20, X is going to be equal to X plus three. So if you write out all the steps, do like we did before. What you say you know, is X less than 20? So in this case X is seven. So is seven less than 20? Yes. So X is now going to be equal to 7 plus 3. Which is 10. Okay so kind of walk through that process, pause the video. See if you can get it to make sense before you run it, and see if you can get the right answer. Okay, so we'll pretend that happened and we'll say, so is 10 less than 20? Yes. So now x equals 10 plus 3 is thirteen. And I keep going. Is thirteen less than 20? Yes. So X is going be 13 plus 3. So it's 16. Is 16 less than 20? Yes. See, so X is equal to 16 plus 3 is 19. Is 19 less than 20? Yes, so X is equal to 19 plus 3 is 22. Is 22 less than 20? No. So we're good. Okay, so that's kind of what I'm talking about. So the final value of this should be so X is still 22. So the value of x is still 22. So this is really all it's doing is, it's not running, come on. Oh, it's because I suppress everything. Like access 22 here you, can't see it. so access 22. So what you really want to be able to do, is to kind of look at while loops and and be able to kind of tell what is this going to do. And really if it's, if you, it's really hard a lot of times just to look at it and get it. Sometimes it helps to just kind of go through a stuff like this, and go through a process like this so you can really get used to the idea of what this for loop is actually doing.
Info
Channel: SnugglyHappyMathTime
Views: 62,968
Rating: 4.6437502 out of 5
Keywords: matlab, for loops, index
Id: ELwFNSEm39M
Channel Id: undefined
Length: 11min 6sec (666 seconds)
Published: Wed Mar 04 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.