CS99, Fall 2002 Lecture 7, Mon 10/7 ------------------------------------------------------------------------------- (0) Announcements + reading in chapter 4 continues + future reading: Chapter 5 + need to know about prelim conflicts (see Syllabus) + Prelim 1 coming up! (see Exams) + advice for studying... (see Exams) ------------------------------------------------------------------------------- (1) Topics: + repetition statements + while + for + tracing ------------------------------------------------------------------------------- (2) Summary To Date: + problems -> automate solution process + convert human solution to computer solution + words-> tokens, sentences-> statements + kinds of statements: expression, assignment, selection + what's next? ------------------------------------------------------------------------------- (3) Weakness of selection Use example: + problem: find the biggest value in an array + algorithm: pick the first number, assume it is the max, compare it with the next number,swap if necessary, repeat the comparison until done processing + solution: x = [1 3 2 4 5] max = x(1) % use $if$ now...: Pretty bad, huh? Excessively redundant code is generally BAD + more problems - how to reprompt the user? - how to find a max of millions of numbers? - how to run the simulation from H1? + solution: - need another statement! - this statement will control flow by repeating other statements - called a REPETITION STATEMENT or LOOP (sometimes also ITERATION) ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- (4) Repetition Statements: WHILE Syntax of $while$ while condition statements end + condition: boolean expression (see selection statement notes) + while statement is compound! + shortcut syntax: while condition,statements,end ------------------------------------------------------------------------------- (5) How WHILE works + encounter the WHILE statement +--> + if the condition is true | - do each statement top-down, left-to-right | - stop when reaching the end statement | - go back to the "top" of while statement +<-- and repeat + if the condition is false - do not evaluate the body of the while statement - go to the statement just after the ENTIRE while statement + continue with rest of statements ------------------------------------------------------------------------------- (6) Examples of WHILE (A) problem: write program that prints Hi! 10 times. algorithm: start count if count < max # of times print Hi! repeat code: count = _____________ % initialize counter while _______________ % start loop _______________ % statement(s) to repeat _______ % when to repeat loop (B) Write program that beeps "forever" while _____ % while true ________ ; % beep end % repeat ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- (7) Tracing + output your variables! - don't use semicolon - maybe use disp + example: x = 0 while x < 4 x x = x + 1 end x +---------+--------+--------+--------+--------+--------+ | initial | loop 1 | loop 2 | loop 3 | loop 4 | final | +--------+---------+--------+--------+--------+--------+--------+ | old x | 0 | 0 | | | | | +--------+---------+--------+--------+--------+--------+--------+ | new x | - | 1 | | | | - | +--------+---------+--------+--------+--------+--------+--------+ ------------------------------------------------------------------------------- (8) Commenting and style + place comments about repetition statement + indent block underneath the while + don't forget the end statement! + use semicolon to suppress needless output + avoid using break to quit loop BAD: while x < 3 x=x+1 end BETTER: while x < 3 x = x + 1 ; end BEST? ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- (9) Longer example Write a program that prompts a user to enter a grade. The program quits if the user enters an out-of-bounds value. Assume that the user only enters numbers. If a grade is better than 90, alert the user. Count the number of VALID grades entered by the user. % Initialize data: highGrade = ____________ ; % highest possible grade lowGrade = ____________ ; % lowest possible grade count = _____ ; % count of VALID grades so far % Prompt for first grade: _____________ = input('Enter a grade: '); % Continue processing grades if necessary while _____________________________ % Increment count of valid grades: _______________________________ ; % Check if grade is good: if _________________________ disp('That is a good grade!'); end % Get next grade: ______________________________________________ ; end % Report the number of valid grades entered: if ___________________ disp('No valid grades entered!'); elseif _________________ disp(['You entered ',_________________,' grade.']); else disp(['You entered ',_________________,' grades.']); end -------------------------------------------------------------------------------