![while loop matlab while loop matlab](https://people.brandeis.edu/~pmherb/MATLABbootcamp/_images/forloop1_1.png)
![while loop matlab while loop matlab](https://slideplayer.com/slide/7224428/24/images/5/Example+2%3A+Simple+WHILE+Loop.jpg)
So on the first loop pass the new value of x is 5 on this second loop pass the new value of x is 14 and on the third and final loop pass the new value of x is equal to 41. I am going to call it while_loop1 and when I save this, the program will automatically run and what’s interesting is, here is you can see the value of x for each loop pass. So i will go up and click save and run give this a file name. Because I would like to output the value of x for each loop pass and then we finish with the end statement and now we are ready to save and run our program. So I’m going to click enter and Matlab will automatically indent this line for readability and our new value of x is assigned the value of 3 times our current value of x minus 1 and I am not going to add a semicolon. So we start off with while Matlab will automatically format this text as blue and we begin with our logical expression.So while the current value of x is less than 20 we evaluate the following statement. So the value of x is not output to the command window and we are ready to begin our while loop. I will put a semicolon here to suppress this statement.
![while loop matlab while loop matlab](https://httpsak-a.akamaihd.net/62009828001/62009828001_5428068481001_3877437038001-vs.jpg)
Now that we are in Matlab we are going to open a new script file which opens the editor and we’ will begin our program by initializing the variable x is assigned the value of 2. Now let’s take a look at implementing this example problem in MATLAB. This time when we evaluate our logical expression our current value of x is actually greater than 20 so this statement is false and our while loop ends. Our new value for x now has a value of 41. Now we evaluate our statement our new value of x is equal to 3 times the old value of x minus 1. This logical expression is true and we begin our third loop pass. We return to our logical expression and it is still true our current value of x which is 14 is less than 20. This statement is true and we evaluate our statement again our new value of xis equal to three times our current value of x minus one and our new value of x is now 14. We will continue to complete loop passes so our current value of x is 5 that is less than 20. Next, we return to our logical expression, and while this statement is true. Our new value of x is assigned the value of 3 times our current value of x minus 1 and our new value of x is now 5. Now let’s take a look at a flowchart of this example to demonstrate each loop pass.įirst, we initialize our variable x which is assigned the value of 2 and we evaluate our logical expression our current value of x is 2 that is less than 20 that statement is true and we begin our first loop pass by evaluating the statement. So for example if x was always equal to 2 then it would also always be less than 20 and this loop would run infinitely. So in this case our loop variable is x and it must be changed if it is not we would run into an infinite loop. The second note I would like to make is this the variable must be changed by the statements. Now I would like to make two important notes here when working with while loops first the loop variable must have avalue before the while statement in our case we have assign x = 2 if we did not initialize the variable x then we would have nothing to compare to in this logical expression so for example if there was no x then you could not evaluate the logical expression is x less than20.
![while loop matlab while loop matlab](https://peeps.unet.brandeis.edu/~pmherb/MATLABbootcamp/_images/forloop1_1.png)
In this case we start by initializing a variable x which has a value of 2 and while x is less than 20 the statements are evaluated which in this case the new value of x is assigned the value of 3 times the current value of x minus 1.