L6post - Previous Lecture: Iteration using for Todays...

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

View Full Document Right Arrow Icon
± Previous Lecture: ± Iteration using for ± Today’s Lecture: ± Detail on the for -loop ± Iteration using while ± Review loops, conditionals using graphics ± Announcements: ± Project 2 posted, due Thursday, 9/24 ± We do not use break in this course ± Discussion section takes place in the classrooms , not lab, this week
Background image of page 1

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

View Full DocumentRight Arrow Icon
September 15, 2009 Lecture 6 3 Monte Carlo π with N darts on L-by-L board hits = 0; for k = 1:N % Throw kth dart x = rand(1)*L – L/2; y = rand(1)*L – L/2; % Count it if it is in the circle if sqrt(x^2+y^2) <= L/2 hits = hits + 1; end end myPi = 4*hits/N;
Background image of page 2
September 15, 2009 Lecture 6 4 for loop examples for k= 2:0.5:3 k takes on the values 2,2.5,3 disp(k) Non-integer increment is OK end for k= 1:4 k takes on the values 1,2,3,4 disp(k) Default increment is 1 end for k= 0:-2:-6 k takes on the values 0,-2,-4,-6 disp(k) “Increment” may be negative end for k= 0:-2:-7 k takes on the values 0,-2,-4,-6 disp(k) Colon expression specifies a bound end for k= 5:2:1 The set of values for k is the empty disp(k) set: the loop body won’t execute 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
September 15, 2009 Lecture 6 6 for loop examples for k= 2:0.5:3 k takes on the values 2,2.5,3 disp(k) Non-integer increment is OK end for k= 1:4 k takes on the values 1,2,3,4 disp(k) Default increment is 1 end for k= 0:-2:-6 k takes on the values 0,-2,-4,-6 disp(k) “Increment” may be negative end for k= 0:-2:-7 k takes on the values 0,-2,-4,-6 disp(k) Colon expression specifies a bound end for k= 5:2:1 The set of values for k is the empty disp(k) set: the loop body won’t execute end
Background image of page 4
Lecture 6 7 % What will be printed? for k= 10:-1:14
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 38

L6post - Previous Lecture: Iteration using for Todays...

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

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