while loops matlab
while loops matlab
The while loop cons is ts of a block of code with a condition. The condition
is checked before the block of code is executed. If the condition is TRUE,
then the block of code is executed. This repeats until the condition
becomes FALSE. The while loop control s tructure is often referred to as a
pre-tes t loop. Whereas a control s tructure that checks the condition after
executing the code is called a "do while loop.", which tes ts the
condition/expres s ion after the loop has executed.
Note : you can als o click the "New" icon which has a plus s ymbol and then click s cript
in the drop down menu of options
Note : if you cannot see where to open a new script in the upper left hand corner you
may be in the wrong tab. Make sure you are in the home tab of Matlab
Note : your header s hould be in green font. This green font means that it is a comment
and is ignored by the computer program
Here our condition that we are checking is if "Time <= 10" (Time is les s than or equal to
10). We take the value of our time variable and compare it to s ee if it is les s than or
equal to 10. If our time variable is les s than or equal to 10, then we will enter the while
loop and perform whatever code we put in there and then we will check our while loop
condition again. We will do this proces s of checking the while loop condition, entering
the while loop, performing the code ins ide the while loop, and rechecking the while
loop condition UNTIL our time variable is NOT les s than or equal to 10 [s o 11 or
greater]. Then we can exit the loop and our program moves on to the res t of our code.
Als o note, if our time variable was larger than 10 from the beginning, we would not
enter the while loop at all!
Note : The part of the loop labeled "do some stuff" is known as the body of the while
loop
Next, we move onto the while loop. Our code will take the current value of our time
variable, which we know is 0, and compare to see if it is less than or equal to 10, wh ich
it is. Since our time variable has a value of less than or equal to 10 we enter into our
while loop and perform the code inside.
So first we pause the program for 1 second. This pause literally stops our program for
1 second and then moves to the next line of code.
Afterwards, we increment the value of our time variable by 1. Notice the form in which
we do this:
For those of you new to programming this may seem strange and a bit unintuitive. In
most all programming languages, the right side of the equal sign is performed before
the left. Therefore this line of code will take the current value of our Time variable,
which is still 0 and add 1 to it, resulting in a new value of 1. Then we take this new
value of 1 and update our Time variable to now be 1. This a tricky concept for new
programmers so spend a few minutes really internalizing this concept before moving
on.
After incrementing, we will use the function disp to display the current value of our time
variable, which will now be 1 NOT 0. Remember we just incremented the value of our
time variable in the previous line.
Now what happens after we display our variab le??? Well we are in the while loop, so
we will have to check our while loop condition again
Our while loop condition says while time is less than or equal to 10 we need perform
what is in the body of our while loop. So the current value of our time vari able is 1,
which is less than or equal to 10 so we will repeat the steps we did above. When will
the loop s top?
Note : If you do not see the save icon indicated by the red boxes in the image above
make sure you are in the Editor tab as indicated by the green box above.
10. Now let's run our code! Click the run button ("green play arrow"), which
should be in the top middle of your screen.
Note : if you cannot see a run button, make sure you are in the "Editor" tab in matlab