CS99, Fall 2002 Mon 10/21 Lecture 8 ------------------------------------------------------------------------------- (0) Announcements: + reading: finish Chapter 4 + future reading: reread 2.1-2.8, 6.2, 6.3, 7.3 last month will be Chapter 5 + HW8 due Nov 4 Topics: + summary + $for$ + designing loops ------------------------------------------------------------------------------- (1) Summary: + repetition statements + using $while$ to repeat instructions + syntax: while (c), s, end + pseudocode: if c is true, do statements, repeat; otherwise, stop ------------------------------------------------------------------------------- (2) for-loop FOR variable = expr, statement, ..., statement END ex) for ii=1:4 ii end Output? ------------------------------------------------------------------------------- (3) Types of loops + how to choose between FOR and WHILE? + DEFINITE: + INDEFINITE: ------------------------------------------------------------------------------- (4) Definite loops ex) Print out all integers between 1 and 4. sol) Use FOR or WHILE, though ________ is better: for ii=1:4,ii,end ii=1; while ii < 5, ii, ii=ii+1, end ex) The user enters a positive integer. Print out all integers from 1 to that value. sol) Use FOR or WHILE, though ________ is better: % for-loop with break: flag=input('Enter a positive integer [1,100]: '); for ii=1:100 disp(['Current value: ',num2str(ii)]); if ii==flag break end end % while-loop: flag=input('Enter a positive integer [1,100]: '); ii=0; while flag <= ii ii=ii+1; disp(['Current value: ',num2str(ii)]); end ------------------------------------------------------------------------------- (5) Indefinite loops ex) Simple guessing game: % Initialize variables: target = floor( ___________ ) + ____ ; % computer picked-number guess = input('Enter a value: '); % user guesses % Prompt user to guess correct number: while _____________________________ disp('Wrong!'); __________________________________ ; % user guesses again end % Congratulate user: disp('Right!'); ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- (6) "Mixed loops" ex) Simple guessing game, but don't allow the user to guess too many times! % Initialize variables: max = 2; % max guess min = 1; % min guess target = floor(rand*(max-min+1)) + 1; % computer picked-number guess = input('Enter a value: '); % user guess count = 1; % number of guesses so far maxcount = 2; % max allowed guesses % Prompt user to guess correct number: while _______________________________________ disp('Wrong!'); guess = input('Enter another value: '); % user guesses again count = ______________________________ ; end % Congratulate user: if __________________________ disp('Right!'); disp(['It took ',num2str( ___________ ),' guesses.']); else disp('You took too many guesses!'); end ------------------------------------------------------------------------------- (7) Designing a loop + General programming: - Brainstorm and research phase - Write algorithm - Determine classification of loop + Pattern to follow: - initialize variables (from research/brainstorm phases) + boundary conditions? + what are LIMITS to the data you need to work with? - get values to test - if test conditions are true + account for the values you obtained + update new values + get next value(s) to test - repeat - aftermath: + account for never entering the loop or other "boundary conditions" + account for data that did get processed in loop ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- (8) Examples of common patterns for indefinite loops % REPETITION: v = input('enter value: ') while v ~= stop disp(v); g = input('enter value: '); end % SUMMATION: sum = 0; v = input('enter value: '); while v ~= stop sum = sum+v; v = input('enter value: '); end if sum==0 disp('no values'); else sum end % CONDITIONAL UPDATE: max=-1; v = input('enter value: '); while v ~= stop if v > max max=v; end v = input('enter value: '); end if max == -1 disp('no values'), else max end -------------------------------------------------------------------------------