Download presentation
Presentation is loading. Please wait.
Published byDiane Lamb Modified over 8 years ago
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 is the same as xValues = 1:n; for x=xValues statements end The expression can be created before the loop itself, so
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 while expression statements end Evaluate expression If TRUE, execute statements If FALSE, jump past end repeat Executing commands an undetermined number of times. while expression statements 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 ForWhile FiniteIndefinite Built-in indexUser-defined index
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
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 statements1 catch statements2 end Try to execute these commands 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 http://jagger.me.berkeley.edu/~pack/e77
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.