Looping structures in MATLAB: WHILE loops

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the screencast on looping structures in MATLAB this time focusing on while loops in this screencast we're going to look at an overview of what a while loop is and compare it to a for loop learning how to setup while loops in a written algorithm look at the structure of how while loops are implemented in MATLAB and a couple of examples on top of that so what is a while loop well a while loop is another way to implement a loop like a for loop but from a different perspective here's an example to illustrate that difference let's say I'm baking cookies baking involves a lot of repetition and it's important to know the conditions under which you should stop for example when baking cookies I have to add in 3 cups of flour if I only have a 1 cup measuring cup I have to repeat a process filling up the cup and emptying it into the mixing bowl a set number of times in this case 3 times but other times I have to repeat a process not for a set number of times but until a certain condition is met for example once I add all the cooking ingredients together in the bowl I have to mix them until the batter is creaming there's no set number of times to stir that will make this happen I just have to monitor the conditions until it does happen now in writing computer programs you run into similar kinds of stopping requirements for loops suppose I want to do something a fixed number of times like when I loop through the elements of a vector this is what a for loop does but other times I might want to repeat something until a condition is met and I don't know how many iterations that will take at the outset so here's a program example that will help us get into the main topic let's write a script that will find the smallest integer n such that the natural logarithm of n is greater than 10 here's how a Briton algorithm for this might go first we're going to start by initializing n equal to 1 and then until the natural logarithm of n is bigger than 10 I'm going to replace n with n plus 1 once I've reached that threshold I display the N and then I stop that is I want to keep adding 1 to n until the natural log of n is bigger than 10 this algorithm works fine in English but to make it work in MATLAB we're going to change this one thing and that's in line to here instead of saying until the log of n is bigger than 10 we're going to say while the log of n is less than or equal to 10 continue to carry out the adding instruction so instead of using until X we're using while the opposite of X this is going to allow us to use the construction in MATLAB and it's also included in many other computer languages called a while loop in MATLAB a while loop is structured as follows we say while and then we specify the condition under which we want the loop to continue and while that condition is met we do stuff instructions are carried out and when the condition fails to be true anymore the while loop stops so it's in it's syntactically similar to a for loop except there's no counter but whether a logical condition that is met tells the loop to continue to execute the instructions and once the condition is violated is no longer true the wild loop stops let's go over to MATLAB and write up our program and see how it would look so now we're in MATLAB and let's enter in our script sticking close to the written algorithm we first started by setting n equal to one and then we said while the log of the end and remember log is the natural logarithm in math math and a MATLAB sorry or in product while that logarithm is less than 10 I'm going to do the following I'm just going to increment in up by one n plus one and then one end and then display the value of n now save this and run it and it gives me back n equals twenty two thousand and twenty seven I'm believing check that and I'll be right along of twenty two thousand twenty seven is ten exactly according to MATLAB here so again just to look in the code here and analyze it a little bit we start with n equal to 1 and we have while and here's my condition while log of n is less than or equal to ten I'm going to keep adding one onto it so when we first encounter the loop in MATLAB n is equal to one and the log of one is zero that's certainly less than 10 and so MATLAB carries out the instructions now and is set equal to two log of two is still less than ten and so madlife continues to carry out the instructions and we just keep doing this until this condition in up top here is violated we do this as long as or why all the log of n is less than or equal to ten at any time the first time that the log of n is less than or equal to ten it is when the log of n is greater than 10 the loop breaks and we bounced out here to this display command here and we see when our n is notice that unlike a for loop there is no counter involved here and so I don't have to I do have to initialize the value of n first until MATLAB explicitly to increment the variable we can get more examples of wild lose by taking any for loop and converting it into a while loop if I want to do so for example take a look at this for loop here in the dock in the editor window we start some at 0 and then for I going from 1 to 10 and incrementing by 1 I'm adding I onto the sum and then displaying all this script does is add up the numbers or the integers between 1 and 10 if I run this program I see that it does add up to the right thing 55 now if I wanted to turn this four loop into a while loop I have to think not about a counter but about a condition that will break the loop now actually if I have a for loop to start with I do have such an ending condition in mind namely it's when I reaches the number 11 in this case once I reaches 11 loops going to stop and so I can use that as this as the stopping condition for a while loop as follows I'm going to just delete this stuff out and set some equal to zero and so there's no counter involved in a while loop I have to initialize my I first and I equal 1 and now I'm going to say while and the condition under which I want to continue the looping is when I is less than 11 I want to loop and continue to add onto my sum until I exceeds the number of 10 and so this is an equivalent condition of that so while I Z is less than 11 I want to add I onto the sum this was in the original for loop and then since this is not a for loop but a while loop they have to explicitly tell MATLAB to increment the counter so I'm going to add one on to I here and I will end and then display the sum let me save this and we'll run it and we'll see that we do get the same result as we have before so in the wild version of this loop I do have to initialize the counter starting at one because that's where it started in the for loop when the wild statement is first encountered I is equal to 1 so in particular it's less than 11 so we do enter the loop I add ion to the sum so now the first time through sum is equal to one then increment I to two matlab looks at I and asks well as my I less than eleven the answer here is yes so I go through the loop again I add to the sum which is now equal to three I add one to I which is also now equal to three and again MATLAB looks at the value of I and decides whether or not it's less than eleven it is this time and so we keep going and we keep going until I increments up to eleven and then we stop the for loop is better for this task than the while loop even though we can do it both ways though because there are two fewer lines of code but both loops can be done using either the for loop or the while so let's recap what we see in the screencast we've learned that there are two ways to do a looping structure in MATLAB if you want to iterate some instructions for a fixed number of times then we use a for loop like we've done before but if I want to iterate a bunch of instructions until a certain condition is reached then I want to use a while loop a while loop use these conditions instead of counters and the loop that we set up for a while loop will continue until my condition is false according to the syntax finally we seen that for loops can also be converted into while loops and also vice versa what we didn't see an example of that but there's good reasons to choose one over the other that's all for now thanks for watching
Info
Channel: RobertTalbertPhD
Views: 56,208
Rating: 4.9359999 out of 5
Keywords: CamtasiaForMac, WHILE, loop, iteration, FOR, looping, Talbert, program, programming, MATLAB, cmp150, tutorial, howto, screencast
Id: O6vD-E3AZoo
Channel Id: undefined
Length: 8min 2sec (482 seconds)
Published: Tue Mar 15 2011
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.