1 / 17

CS 170 – Intro to Scientific and engineering Programming

CS 170 – Intro to Scientific and engineering Programming . for, end. Execute collection of statements a fixed number of times: for x=expression statements end The expression is evaluated once before the loop starts. The value is called the controlvalue . .

drago
Download Presentation

CS 170 – Intro to Scientific and engineering Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 170 – Intro to Scientific and engineering Programming

  2. for, end • Execute collection of statements a fixed number of times: for x=expression statements end • The expression is evaluated once before the loop starts. • The value is called the controlvalue. Execution continues with any statements beyond the end

  3. for loops, most common use(!) • The most common value for expression is a row vector of integers, starting at 1, and increasing to a limit n for x=1:n statements end • The controlvalue is simply the row vector[ 1 2 3 4 … n ] • Hence, the statements are executed n times. • The first time through, the value of x is set equal to 1; • the k’th time through, the value of x is set equal to k.

  4. for loops, most common use for x=1:n statements end The expression can be created before the loop itself, so • xValues = 1:n; • for x=xValues • statements • end is the same as

  5. for loop example • Suppose you have been given a set of data in an array, x. x = [-2 4 6 -7 3 5 -45 1 -0.5 2.6] • You have been asked to write a program that will print the positive numbers in x. • A solution is to write a simple for loop.

  6. while Executing commands an undetermined number of times. while expression statements end repeat while expression statements end Evaluate expression If TRUE, execute statements If FALSE, jump past end

  7. Example using tic/toc • tic is a built-in Matlab function that starts a timer. • Every subsequent call to toc (also Matlab built-in) returns the elapsed time (in seconds) since the originating call to tic. • The code below will cause the program to “pause” for one second before proceeding. • tic • while toc<1 • end • It would be clumsy to do this without a while-loop

  8. Common mistakes n = 1; total = 0; while n <= 10 total = total + n; end

  9. Filtering Data With a While Loop • Suppose the user has been asked to enter an array of data values, x, and you have been asked to write a program that will retrieve those values and print out only the positive numbers.

  10. Programming with loops • Good practices: • Always indent the statements in the body of your loop by 2 – 5 spaces • Avoid modifying the value of a loop index variable inside the body of a for loop

  11. For vs While

  12. Nested loops for j = 1:12 for k = 1:12 fprintf(‘%d ‘, j*k); end fprintf(‘\n’); end

  13. Break statement x = load(‘sample_data.dat’); for ii = 1:length(x) if x(ii) <= 0 fprintf(‘The data contains invalid values.’) break; end end

  14. Preallocation • Exercise • A factorial is defined for a number, N, such that N! = 1 * 2 * 3 * . . . * N-2 * N-1 * N • For example, 10! = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 = 3,628,800 • Write a program that calculates N!, where N is provided by the user.

  15. try/catch Try to execute these commands try statements1 catch statements2 end If a run-time error occurs while executing statements1, stop, and… Execute these commands If no error occurs executing statements1, jump beyond end. Do not execute statements2

  16. Questions??

  17. Resources • “Introduction to Programming with Matlab”, J. Michael Fitzpatrick and John D. Crocetti • Lecture slides E77, Andy Packard, http://jagger.me.berkeley.edu/~pack/e77

More Related