Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops CS 103 February 13, 2009 Author: Nate Hamm.

Similar presentations


Presentation on theme: "Loops CS 103 February 13, 2009 Author: Nate Hamm."— Presentation transcript:

1 Loops CS 103 February 13, 2009 Author: Nate Hamm

2 What is a loop? A control construct that allows for a block of code to be repeated A control construct that allows for a block of code to be repeated In Matlab, loops come in two forms: In Matlab, loops come in two forms: FOR loopsFOR loops WHILE loopsWHILE loops

3 FOR Loops Syntax: Syntax: FOR variable = expression statementsEND

4 FOR Loops FOR variable = expression statementsEND The variable is known as an index, and can be treated as a variable by the program. Traditionally, the letter i is used as the index, but in Matlab, i also is used to represent imaginary numbers, so we use ii or another letter.

5 FOR Loops FOR variable = expression statementsEND In FOR loops, the expression is a counting mechanism. We typically use the colon operator to define how many times the loop should run. For example, 1:10 means to run the loop 10 times (from one to ten)

6 FOR Loops FOR variable = expression statementsEND The statements are the set of code which is to be executed over and over again during the loop. Every loop needs to conclude with an END

7 Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END

8 Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END Before the loop, ii doesn’t exist

9 Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END First time through, ii = 1

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

11 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

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

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

14 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

15 Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END Third time through, ii = 3

16 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…

17 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

18 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!

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

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

21 Notes About FOR Loops Used when the required values of your index variable are known before the loop begins Though not necessarily before the program begins. Used when the required values of your index variable are known before the loop begins Though not necessarily before the program begins. Example: count to 10, add 12 numbers, calculate pi to 30 decimal places, etc. Example: count to 10, add 12 numbers, calculate pi to 30 decimal places, etc.

22 FOR Loop Example Suppose we wanted to write a program that prints out all the positive numbers in an array, x Suppose we wanted to write a program that prints 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:10 if x(ii) > 0 fprintf(‘%f \n’, x(ii)) endend But what happens if instead, we ask the user to input x first? Will we know how many elements are in x? But what happens if instead, we ask the user to input x first? Will we know how many elements are in x?

23 Working Around the Number Limitation When working with an array of numbers that may change, you can use the length function. FOR ii = 1:length(x) When working with an array of numbers that may change, you can use the length function. FOR ii = 1:length(x)

24 FOR Loop Example Ask the user to input an array, x, and then print out all the positive numbers in the array. Ask the user to input an array, x, and then print out all the positive numbers in the array. X = input(‘Please give me an array: ‘) FOR ii = 1:length(x) if x(ii) > 0 fprintf(‘%f \n’, x(ii)) endEND

25 Questions?


Download ppt "Loops CS 103 February 13, 2009 Author: Nate Hamm."

Similar presentations


Ads by Google