1 / 14

CMPS 1371 Introduction to Computing for Engineers

CMPS 1371 Introduction to Computing for Engineers. ITERATION (LOOPS). Repetition Structures - Loops. Loops are used when you need to repeat a set of instructions multiple times MATLAB supports two types of loops for While In general loops are best used with scalars. “For” Loops.

rayclark
Download Presentation

CMPS 1371 Introduction to Computing for Engineers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMPS 1371Introduction to Computing for Engineers ITERATION (LOOPS)

  2. Repetition Structures - Loops • Loops are used when you need to repeat a set of instructions multiple times • MATLAB supports two types of loops • for • While • In general loops are best used with scalars

  3. “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

  4. Flow Chart 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

  5. Here’s a simple example the index can be defined using any of the techniques we’ve learned

  6. Here’s a simple example the index can be defined using any of the techniques we’ve learned

  7. Another Example • Count the number of negative rainfall amounts • rain = [0 3 4 -2 -5 1 1 2] • count = 0 • for ind = 1:length(rain) • val = rain(ind) • if (val <= 0 ) count = count + 1 • end • disp(sprintf('Countis %d', count))

  8. 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

  9. Flow Chart The criterion is no longer true and the program exits the loop Check to see if the criterion is still true Calculations Flow Chart for a while loop

  10. We have to increment the counter (in this case k) every time through the loop – or the loop will never stop!!

  11. This loop creates the matrix a, one element at a time

  12. This program counts how many scores in the array are greater than 90, and displays the result in the command window

  13. 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” • Exit the calculation manually with ctrl c

  14. break and continue • break causes the loop to terminate prematurely • continue causes MATLAB to skip a pass through the loop, but continue on until the criteria for ending is met • both are used in conjunction with an if statement

More Related