Loop Applications and Review CS 103 February 27, 2004
Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Before the loop, the index, ii, doesn’t exist
Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end First time through, ii = 1
Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end First time through, ii = 1 Now we print Count: 1
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
Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Second time through, so ii is incremented ii = 2
Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Second time through, ii = 2 Now we print Count: 2
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
Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Third time through, ii = 3
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…
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
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!
Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Tenth time through, ii = 10 Now we print Count: 10
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.
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 = [ ] x = [ ] We can reference each element in the array using the index…… x(1), x(2), etc. as x(ii)
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Polling Question 4 Now, do the same thing using a FOR loop Now, do the same thing using a FOR loop
Solution y = [ ]; for ii = 1:length(x) if x(ii) > 0 y = [y, x(ii)]; endend
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
Remember Short Circuiting Given an array x, k = length(x) + 1; for n = 1:length(x) if x(n) < 0 k = n; break;endend
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.
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 = [ ] if x(6) > x(7) Problem: there is no x(7)
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.
Solution n = 1; while n < length(x) if x(n) > x(n+1) fprintf(‘Out of Order’); end n = n + 1; end