Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loop Applications and Review CS 103 February 27, 2004.

Similar presentations


Presentation on theme: "Loop Applications and Review CS 103 February 27, 2004."— Presentation transcript:

1 Loop Applications and Review CS 103 February 27, 2004

2 Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Before the loop, the index, ii, doesn’t exist

3 Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end First time through, ii = 1

4 Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end First time through, ii = 1 Now we print Count: 1

5 Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end First time through, ii = 1 Now we print Count: 1 The loop ends, so we’ll return to the top of the loop

6 Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Second time through, so ii is incremented ii = 2

7 Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Second time through, ii = 2 Now we print Count: 2

8 Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Second time through, ii = 2 Now we print Count: 2 The loop ends, so we’ll return to the top of the loop

9 Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Third time through, ii = 3

10 Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Third time through, ii = 3 Now we print Count: 3 This will continue for awhile…

11 Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Imagine we’re now on the 9 th iteration Ninth time through, ii = 9 Now we print Count: 9 The loop ends, so we’ll return to the top of the loop

12 Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Tenth time through, ii = 10 This will be the last time we go through the loop!

13 Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Tenth time through, ii = 10 Now we print Count: 10

14 Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Tenth time through, ii = 10 Now we print Count: 10 We’ve run the loop 10 times….because that’s as far as we have been asked to go, the loop ends and we’ll go on to whatever code exists after the end statement.

15 Using The Index One of the most common uses of the index is to run through a series of numbers in an array One of the most common uses of the index is to run through a series of numbers in an array x = [ 2 4 5 -2 13 22 -5 8 -13 7 1] x = [ 2 4 5 -2 13 22 -5 8 -13 7 1] 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 We can reference each element in the array using the index…… x(1), x(2), etc. as x(ii)

16 Polling Question 1 Given an array x, write a FOR loop that prints out the negative numbers in x. Hint: this program can be written in 5 lines Given an array x, write a FOR loop that prints out the negative numbers in x. Hint: this program can be written in 5 lines

17 Solution Given an array x, write a FOR loop that prints out the negative numbers in x. Given an array x, write a FOR loop that prints out the negative numbers in x. for ii = 1:length(x) if x(ii) < 0 fprintf(‘%f \n’, x(ii)); endend

18 While Loops There are a couple key differences between FOR loops and WHILE loops There are a couple key differences between FOR loops and WHILE loops The syntax is different (obviously) The syntax is different (obviously) For loops run for a set number of iterations, but while loops run until a logical condition is no longer true For loops run for a set number of iterations, but while loops run until a logical condition is no longer true For loops have a built-in indexing system, while loops do not For loops have a built-in indexing system, while loops do not

19 Indexing With WHILE Loops If you’re going to do the type of array manipulation we just practiced using a WHILE loop, you have to set up your own index. If you’re going to do the type of array manipulation we just practiced using a WHILE loop, you have to set up your own index. This means: This means: Creating your index variable Creating your index variable Incrementing the index each time through the loop Incrementing the index each time through the loop

20 Comparing WHILE & FOR Loops FOR Loop FOR Loop for ii = 1:length(x) if x(ii) < 0 fprintf(‘%f \n’, x(ii)); endend WHILE Loop WHILE Loop n = 1; while n <= length(x) if x(n) < 0 fprintf(‘%f \n’, x(n)); end n = n + 1; end

21 Comparing WHILE & FOR Loops FOR Loop FOR Loop for ii = 1:length(x) if x(ii) < 0 fprintf(‘%f \n’, x(ii)); endend WHILE Loop WHILE Loop n = 1; while n <= length(x) if x(n) < 0 fprintf(‘%f \n’, x(n)); end n = n + 1; end Create Index

22 Comparing WHILE & FOR Loops FOR Loop FOR Loop for ii = 1:length(x) if x(ii) < 0 fprintf(‘%f \n’, x(ii)); endend WHILE Loop WHILE Loop n = 1; while n <= length(x) if x(n) < 0 fprintf(‘%f \n’, x(n)); end n = n + 1; end Set a logical condition

23 Comparing WHILE & FOR Loops FOR Loop FOR Loop for ii = 1:length(x) if x(ii) < 0 fprintf(‘%f \n’, x(ii)); endend WHILE Loop WHILE Loop n = 1; while n <= length(x) if x(n) < 0 fprintf(‘%f \n’, x(n)); end n = n + 1; end Increment the index

24 Polling Question 2 Given an array x, write a WHILE loop that will print all the numbers in x that are greater than 5. Hint: this can be written in 7 lines Given an array x, write a WHILE loop that will print all the numbers in x that are greater than 5. Hint: this can be written in 7 lines

25 Solution Given an array x, write a WHILE loop that will print all the numbers in x that are greater than 5. Given an array x, write a WHILE loop that will print all the numbers in x that are greater than 5. n = 1; while n <= length(x) if x(n) > 5 fprintf(‘%f \n’, x(n)); end n = n + 1; end

26 Replacing Values Given an array x, y = zeros(1, length(x)) for ii = 1:length(x) if x(ii) > 0 y(ii) = x(ii); endend

27 Polling Question 3 Given an array x, y = zeros(1, length(x)) for ii = 1:length(x) if x(ii) > 0 y(ii) = x(ii); endend Now, write the code that will do the same thing using a WHILE loop

28 Solution y = zeros(1, length(x)); n = 1; while n < length(x) if x(n) > 0 y(n) = x(n) end n = n + 1; end

29 Preallocation Setting up our array of zeros ahead of time is called pre-allocation. It makes the program more efficient by allocating the needed variable space in memory ahead of time Setting up our array of zeros ahead of time is called pre-allocation. It makes the program more efficient by allocating the needed variable space in memory ahead of time This is really important for large arrays, but for small ones, it’s not that big a deal This is really important for large arrays, but for small ones, it’s not that big a deal

30 Without Preallocation We can choose not to use preallocation and avoid having all those excess zeros We can choose not to use preallocation and avoid having all those excess zeros y = [ ]; n = 1 while n <= length(x) if x(n) > 0 y = [y, x(n)] end n = n + 1; end

31 Polling Question 4 Now, do the same thing using a FOR loop Now, do the same thing using a FOR loop

32 Solution y = [ ]; for ii = 1:length(x) if x(ii) > 0 y = [y, x(ii)]; endend

33 Watch the Questions Are you answering the right question? Are you answering the right question? Set k = the first element of x that is less than 0 Set k = the first element of x that is less than 0 Set k = the index of the first element of x that is less than 0 Set k = the index of the first element of x that is less than 0

34 Remember Short Circuiting Given an array x, k = length(x) + 1; for n = 1:length(x) if x(n) < 0 k = n; break;endend

35 Polling Question 5 Write a FOR loop that sets k = to the index of the first element in x that is larger than the following element in x. Write a FOR loop that sets k = to the index of the first element in x that is larger than the following element in x.

36 Solution for ii = 1:length(x) – 1 if x(ii) > x(ii+1) k = ii; endend Did you set up your indexing properly? Because we’re looking at the element in front of our active index, we have to be careful that we don’t exceed our array’s dimensions! x = [ 3 4 5 6 7 8 ] if x(6) > x(7)  Problem: there is no x(7)

37 Polling Question 6 Write a WHILE loop to determine whether or not a given vector x is in ascending order. Write a WHILE loop to determine whether or not a given vector x is in ascending order.

38 Solution n = 1; while n < length(x) if x(n) > x(n+1) fprintf(‘Out of Order’); end n = n + 1; end


Download ppt "Loop Applications and Review CS 103 February 27, 2004."

Similar presentations


Ads by Google