cmps 1371 introduction to computing for engineers iteration (loops)

14
CMPS 1371 Introduction to Computing for Engineers ITERATION (LOOPS)

Upload: ashley-glenn

Post on 16-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

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

CMPS 1371Introduction to

Computing for Engineers

ITERATION (LOOPS)

Page 2: 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

Page 3: CMPS 1371 Introduction to Computing for Engineers ITERATION (LOOPS)

“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

Page 4: CMPS 1371 Introduction to Computing for Engineers ITERATION (LOOPS)

Check to see if the index has been exceeded

Calculations

You’ve run out of values in the index matrix

Flow chart for a for loop

Flow Chart

Page 5: CMPS 1371 Introduction to Computing for Engineers ITERATION (LOOPS)

Here’s a simple example

the index can be defined using any of the techniques we’ve learned

Page 6: CMPS 1371 Introduction to Computing for Engineers ITERATION (LOOPS)

Here’s a simple example

the index can be defined using any of the techniques we’ve learned

Page 7: CMPS 1371 Introduction to Computing for Engineers ITERATION (LOOPS)

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('Count is %d', count))

Page 8: CMPS 1371 Introduction to Computing for Engineers ITERATION (LOOPS)

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 executedend

Page 9: CMPS 1371 Introduction to Computing for Engineers ITERATION (LOOPS)

Check to see if the criterion is still true

Calculations

The criterion is no longer true and the program exits the loop

Flow Chart for a while loop

Flow Chart

Page 10: CMPS 1371 Introduction to Computing for Engineers ITERATION (LOOPS)

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

Page 11: CMPS 1371 Introduction to Computing for Engineers ITERATION (LOOPS)

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

Page 12: CMPS 1371 Introduction to Computing for Engineers ITERATION (LOOPS)

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

Page 13: CMPS 1371 Introduction to Computing for Engineers ITERATION (LOOPS)

Hint

If you accidentally create a loop that just keeps running you should

1. Confirm that the computer is actually still calculating something by checking the lower left hand corner of the MATLAB window for the “busy indicator”

2. Exit the calculation manually with ctrl c

Page 14: CMPS 1371 Introduction to Computing for Engineers ITERATION (LOOPS)

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