{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

6. More on Iteration For-Loop Problems Introduce While-Loops

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

View Full Document
Problem Solving With the For-Loop The calculation to be repeated. end for count variable = expression for starting value : expression for ending value :
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

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

View Full Document
There is a line of output for every Odd number between 100 and 200. Answer = 50
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.

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

View Full Document
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”
% 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))

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

View Full Document
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.
What We Need A loop that “shuts down’’ as soon as |H-T| = =10.

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

View Full Document
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( … ))
How a While-Loop Works Warm-up exercise: Any for-loop can be written as a while-loop.

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

View Full Document
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
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

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

View Full Document
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
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.

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

View Full Document
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 1 k 1 s Is k < 5 true? Yes. Execute the loop body.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online