Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
0 views

while loops matlab

The document provides a detailed guide on using while loops in Matlab, explaining that a while loop executes a block of code as long as a specified condition is true. It includes step-by-step instructions for creating a script, defining a time variable, coding the while loop, and incrementing the time variable within the loop. The document emphasizes the importance of the 'end' statement to close the loop and offers tips for running the code successfully.

Uploaded by

jsgwinn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

while loops matlab

The document provides a detailed guide on using while loops in Matlab, explaining that a while loop executes a block of code as long as a specified condition is true. It includes step-by-step instructions for creating a script, defining a time variable, coding the while loop, and incrementing the time variable within the loop. The document emphasizes the importance of the 'end' statement to close the loop and offers tips for running the code successfully.

Uploaded by

jsgwinn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Matlab: While Loops

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.

1. Open up a new s cript


Click new s cript in the upper left hand corner of Matlab

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

2. Add in your EGR header as shown below:


%{
Student Name
While Loop Example
%}

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

3. Now lets code the following variable

Note : a variable is a piece of information that is given a name


4. Now let's code our while loop.
A while loop bas ically checks the validity of a condition and acts accordingly. If the
condition is true we enter the while loop and if the condition is fals e we do not enter
the while loop. We will s tart our while loop by coding the following line:

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!

5. Add Time Variable within Whole Loop


Now that we unders tand what our while loop means , let's code the res t. Type the
following lines below your while s tatement:

6. Add End to While Loop


Las tly, all while loops in Matlab are of the form
while someCondition
do
some
stuff
end
Notice the "end" at the end. We must include this in order to tell Matlab where our
while loop has ended. So now type "end" to tell Matlab to end your while loop.

Note : The part of the loop labeled "do some stuff" is known as the body of the while
loop

7. Check your While Loop


Let's take a quick break and look at what we our code is doing.
Our first line of code makes a variable named Time and assigns it the value of the 0.

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?

8. Save your program as "WhileLoopExample"

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.

9. Review your code

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

Check Your Code


Below is the solution you should have in the command window of Matlab.

Below is what your Workspace you should look like in Matlab.

You might also like