loops cs 103 february 13, 2009 author: nate hamm
TRANSCRIPT
LoopsLoops
CS 103CS 103
February 13, 2009February 13, 2009
Author: Nate Hamm
What is a lWhat is a loooop?p?
A control construct that allows for a block A control construct that allows for a block of code to be repeatedof code to be repeated
In Matlab, loops come in two forms:In Matlab, loops come in two forms:• FOR loopsFOR loops• WHILE loopsWHILE loops
FOR LoopsFOR Loops
Syntax:Syntax:
FOR variable = expressionFOR variable = expression
statementsstatements
ENDEND
FOR LoopsFOR Loops
FOR FOR variablevariable = expression = expressionstatementsstatements
ENDEND
The The variablevariable is known as an is known as an indexindex, and can be , and can be treated as a variable by the program.treated as a variable by the program.
Traditionally, the letter i is used as the index, Traditionally, the letter i is used as the index, but in Matlab, i also is used to represent but in Matlab, i also is used to represent imaginary numbers, so we use ii or another imaginary numbers, so we use ii or another letter.letter.
FOR LoopsFOR Loops
FOR variable = FOR variable = expressionexpression
statementsstatements
ENDEND
In FOR loops, the In FOR loops, the expressionexpression is a counting is a counting mechanism. We typically use the colon mechanism. We typically use the colon operator to define how many times the operator to define how many times the loop should run. For example, 1:10 means loop should run. For example, 1:10 means to run the loop 10 times (from one to ten)to run the loop 10 times (from one to ten)
FOR LoopsFOR Loops
FOR variable = expressionFOR variable = expression
statementsstatements
ENDEND
The The statementsstatements are the set of code which is are the set of code which is to be executed over and over again during to be executed over and over again during the loop.the loop.
Every loop needs to conclude with an Every loop needs to conclude with an ENDEND
Example: Count to 10Example: Count to 10
FOR ii = 1:10FOR ii = 1:10
fprintf(‘\n Count: %d’, ii)fprintf(‘\n Count: %d’, ii)
ENDEND
Example: Count to 10Example: Count to 10
FOR ii = 1:10FOR ii = 1:10
fprintf(‘\n Count: %d’, ii)fprintf(‘\n Count: %d’, ii)
ENDEND
Before the loop, ii doesn’t exist
Example: Count to 10Example: Count to 10
FOR ii = 1:10FOR ii = 1:10
fprintf(‘\n Count: %d’, ii)fprintf(‘\n Count: %d’, ii)
ENDEND
First time through,
ii = 1
Example: Count to 10Example: Count to 10
FOR ii = 1:10FOR ii = 1:10
fprintf(‘\n Count: %d’, ii)fprintf(‘\n Count: %d’, ii)
ENDEND
First time through, ii = 1
Now we print Count: 1
Example: Count to 10Example: Count to 10
FOR ii = 1:10FOR ii = 1:10
fprintf(‘\n Count: %d’, ii)fprintf(‘\n Count: %d’, ii)
ENDEND
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 10Example: Count to 10
FOR ii = 1:10FOR ii = 1:10
fprintf(‘\n Count: %d’, ii)fprintf(‘\n Count: %d’, ii)
ENDEND
Second time through, so ii is incremented
ii = 2
Example: Count to 10Example: Count to 10
FOR ii = 1:10FOR ii = 1:10
fprintf(‘\n Count: %d’, ii)fprintf(‘\n Count: %d’, ii)
ENDEND
Second time through, ii = 2
Now we print Count: 2
Example: Count to 10Example: Count to 10
FOR ii = 1:10FOR ii = 1:10
fprintf(‘\n Count: %d’, ii)fprintf(‘\n Count: %d’, ii)
ENDEND
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 10Example: Count to 10
FOR ii = 1:10FOR ii = 1:10
fprintf(‘\n Count: %d’, ii)fprintf(‘\n Count: %d’, ii)
ENDEND
Third time through,
ii = 3
Example: Count to 10Example: Count to 10
FOR ii = 1:10FOR ii = 1:10
fprintf(‘\n Count: %d’, ii)fprintf(‘\n Count: %d’, ii)
ENDEND
Third time through, ii = 3
Now we print Count: 3
This will continue for awhile…
Example: Count to 10Example: Count to 10
FOR ii = 1:10FOR ii = 1:10
fprintf(‘\n Count: %d’, ii)fprintf(‘\n Count: %d’, ii)
ENDEND
Imagine we’re now on the 9th 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 10Example: Count to 10
FOR ii = 1:10FOR ii = 1:10
fprintf(‘\n Count: %d’, ii)fprintf(‘\n Count: %d’, ii)
ENDEND
Tenth time through,
ii = 10
This will be the last time we go through the loop!
Example: Count to 10Example: Count to 10
FOR ii = 1:10FOR ii = 1:10
fprintf(‘\n Count: %d’, ii)fprintf(‘\n Count: %d’, ii)
ENDEND
Tenth time through, ii = 10
Now we print Count: 10
Example: Count to 10Example: Count to 10
FOR ii = 1:10FOR ii = 1:10
fprintf(‘\n Count: %d’, ii)fprintf(‘\n Count: %d’, ii)
ENDEND
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.
Notes About FOR LoopsNotes About FOR Loops
Used when the required values of Used when the required values of your index variable are known before your index variable are known before the loop beginsthe loop begins
Though not necessarily before the Though not necessarily before the program begins.program begins.
Example: count to 10, add 12 Example: count to 10, add 12 numbers, calculate pi to 30 decimal numbers, calculate pi to 30 decimal places, etc.places, etc.
FOR Loop ExampleFOR Loop Example Suppose we wanted to write a program that Suppose we wanted to write a program that
prints out all the positive numbers in an array, xprints out all the positive numbers in an array, x X = [-2 4 6 -7 3 5 -45 1 -.5 2.6]X = [-2 4 6 -7 3 5 -45 1 -.5 2.6]
for ii = 1:10for ii = 1:10if x(ii) > 0if x(ii) > 0
fprintf(‘%f \n’, x(ii))fprintf(‘%f \n’, x(ii))endend
endend
But what happens if instead, we ask the user to But what happens if instead, we ask the user to input x first? Will we know how many elements input x first? Will we know how many elements are in x?are in x?
Working Around the Number Working Around the Number LimitationLimitation
When working with an array of When working with an array of numbers that may change, you can numbers that may change, you can use the length function.use the length function.
FOR ii = 1:length(x) FOR ii = 1:length(x)
FOR Loop ExampleFOR Loop Example
Ask the user to input an array, x, and then Ask the user to input an array, x, and then print out all the positive numbers in the print out all the positive numbers in the array.array.
X = input(‘Please give me an array: ‘)X = input(‘Please give me an array: ‘)FOR ii = 1:length(x)FOR ii = 1:length(x)
if x(ii) > 0if x(ii) > 0fprintf(‘%f \n’, x(ii))fprintf(‘%f \n’, x(ii))
endendENDEND
Questions?Questions?