Lec5 - 6. More on Iteration For-Loop Problems Introduce...

Info iconThis preview shows pages 1–17. Sign up to view the full content.

View Full Document Right Arrow Icon
6. More on Iteration For-Loop Problems Introduce While-Loops
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Problem Solving With the For-Loop The calculation to be repeated. end for count variable = expression for starting value : expression for ending value :
Background image of page 2
Question Time How many lines of output are produced by the following script. A. 2 B . 50 C. 51 D . 101 for k=100:200 if rem(k,2)~=0 disp(‘k’); end end
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
There is a line of output for every Odd number between 100 and 200. Answer = 50
Background image of page 4
for count variable = expression for starting value expression for ending value : For-Loop Shortcoming When you use a for-loop, you need to know the exact extent of the repetition.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
OK for Many Situations Here is a typical for-loop Problem: Simulate the tossing of a fair coin 100 times and print the number of “Heads”
Background image of page 6
% Running sum… H = 0; for tosses = 1:100 r = rand; if r < .5 % Agree that this means “heads” H = H + 1; end end disp(sprintf(‘H = %2d’,H))
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Not OK in Other Situations Simulate the game of “Gap10”: Toss a fair coin until | #Heads – #Tails | = 10 Score = number of required tosses The number of required tosses is not known in advance.
Background image of page 8
What We Need A loop that “shuts down’’ as soon as |H-T| = =10.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
H = 0; T = 0; tosses = 0; while abs(H-T)<10 r = rand; tosses = tosses + 1; if r < .5 H = H + 1; else T = T + 1; end end disp(sprintf( … ))
Background image of page 10
How a While-Loop Works Warm-up exercise: Any for-loop can be written as a while-loop.
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A Simple For-loop s = 0; for k=1:5 s = s + k; disp(sprintf(‘%2d %2d’,k,s)) end 1 1 2 3 3 6 4 10 5 15
Background image of page 12
The While-loop Equivalent k = 0; s = 0; while k < 5 k = k + 1; s = s + k; disp(sprintf(‘%2d %2d’,k,s)) end 1 1 2 3 3 6 4 10 5 15
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
How it Works in Detail k = 0; s = 0; while k < 5 k = k + 1; s = s + k; disp(sprintf(‘%2d %2d’,k,s)) end k s k and s are initialized
Background image of page 14
How it Works in Detail k = 0; s = 0; while k < 5 k = k + 1; s = s + k; disp(sprintf(‘%2d %2d’,k,s)) end 0 k 0 s Is k < 5 true? Yes. Execute the loop body.
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
k = 0; s = 0; while k < 5 k = k + 1; s = s + k; disp(sprintf(‘%2d %2d’,k,s)) end 1 k 1 s Is k < 5 true? Yes. Execute the loop body. 1
Background image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/28/2008 for the course CS 100 taught by Professor Fan/vanloan during the Fall '07 term at Cornell University (Engineering School).

Page1 / 54

Lec5 - 6. More on Iteration For-Loop Problems Introduce...

This preview shows document pages 1 - 17. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online