Repetition Structures Chapter 9
Structures Sequence Selection Repetition Sequence Selection Repetition (Loop)
Types of Loops Loops are used when you need to repeat a set of instructions multiple times MATLAB supports two types of loops for while
9.1 For Loops for index = [matrix] commands to be executed end The loop is executed once for each element of the index matrix identified in the first line
Check to see if the index has been exceeded True; You’ve run out of values in the index matrix Check to see if the index has been exceeded Flow chart for a for loop Calculations
Here’s a simple example the index can be defined using any of the techniques we’ve learned In this case k is the index – the loop is repeated once for each value of k
Here’s a simple example the index can be defined using any of the techniques we’ve learned In this case k is the index – the loop is repeated once for each value of k
One of the most common ways to use a loop is to define a matrix
Another use is in combination with an if statement to determine how many times something is true Each time through the loop we evaluate a single element of the scores matrix
While Loops While loops are very similar to for loops. The big difference is the way MATLAB decides how many times to repeat the loop. While loops continue until some criterion is met. while criterion commands to be executed end
We have to increment the counter (in this case k) every time through the loop – or the loop will never stop!!
This loop creates the matrix a, one element at a time
This program counts how many scores in the array are greater than 90, and displays the result in the command window
Hint If you accidentally create a loop that just keeps running you should Confirm that the computer is actually still calculating something by checking the lower left hand corner of the MATLAB window for the “busy indicator” Make sure the active window is the command window and exit the calculation manually with ctrl c
This program prompts the user 10 times to enter a value, and computes the natural log. If a negative value is entered, the break command causes MATLAB to exit the loop
Notice that n had a value of 3 when the program terminated – if it had run to completion it would have had a value of 10
The continue command is similar to break, however instead of terminating the loop, the program just skips to the next pass.
Notice that n had a value of 10 when the program terminated
9.4 Midpoint Break Loops Implemented with either a for or a while loop The loop is Entered Calculations processed Decision is made at an arbitrary point whether or not to continue
Results in a continuous loop Causes the loop to terminate
Limits the number of repetitions
9.5 Nested Loops It is often useful to nest loops inside of other loops To illustrate, consider code to mimic the built-in max function
9.6 Improving the Efficiency of Loops In general, using a for loop (or a while loop) is less efficient in MATLAB than using array operations.
These two lines of code start a timer to measure the elapsed time required to run the lines of MATLAB code between them Here’s an example. This code creates a 4,000,000 element matrix of ones, then multiplies each element in the matrix by pi The amount of time it takes to run this code will depend on your computer, and how much else you have installed on it
This code accomplishes the same thing with a for loop
If we predefine the B matrix and then replace each element one at a time with the new value, the run time is significantly reduced Most of the time required to run the previous problem was used to create the B matrix one element at a time
An alternate syntax for timing uses the tic and toc functions
Hint Be sure to suppress intermediate calculations when you use a loop. Printing those values to the screen will greatly increase the amount of execution time. If you are brave, repeat the example above, but delete the semicolons inside the loop just to check out this claim. Don’t forget that you can stop the execution of the program with ctrl c. (But the command window needs to be active)