1 / 32

6. More on the For-Loop

6. More on the For-Loop. Using the Count Variable Developing For-Loop Solutions. Syntax of the for loop. for < var > = < start value > : < incr > : < end bound > statements to be executed repeatedly end. Loop body. Syntax of the for loop.

kane
Download Presentation

6. More on the For-Loop

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions

  2. Syntax of the for loop for <var> = <start value>:<incr>:<end bound> statements to be executed repeatedly end Loop body

  3. Syntax of the for loop for <var> = <start value>:<incr>:<end bound> statements to be executed repeatedly end Loop header specifies all the values that the index variable will take on, one for each pass of the loop. E.g, k= 3:1:7 means k will take on the values 3, 4, 5, 6, 7, one at a time.

  4. Pattern for doing something n times n= _____ for k= 1:n % code to do % that something end Definite iteration

  5. % What will be printed?for k= 1:2:6 fprintf(‘%d ’, k)end A: 1 2 3 4 5 6 B: 1 3 5 6 C: 1 3 5 D: error (incorrect bounds)

  6. forloop examples for k= 2:0.5:3 k takes on the values2,2.5,3 disp(k) Non-integer increment is OK end for k= 1:4 k takes on the values1,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

  7. % What will be printed?for k= 10:-1:14 fprintf(‘%d ’, k)endfprintf(‘!’) A: error (incorrect bounds) B: 10 (then error) C: 10 ! D: 14 ! E: !

  8. 4 9 4 4 Something else … or or What will be displayed when you run the following script? for k = 4:6 disp(k) k= 9; disp(k) end A B C

  9. 4 5 6 k With this loop header, k “promises” to be these values, 1 at a time for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window

  10. 4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 k

  11. 4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 4 k

  12. 4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 k

  13. 4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 9 k

  14. 4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 5 k

  15. 4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 5 5 k

  16. 4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 5 9 k

  17. 4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 5 9 9 k

  18. 4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 5 9 6 k

  19. 4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 5 9 6 6 k

  20. 4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 5 9 6 9 k

  21. 4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 5 9 6 9 9 k

  22. 4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 5 9 6 9 9 k

  23. 4 5 6 This is NOT a condition (boolean expression) that checks whether k<=6. It is an expression that specifies values: for k = 4:6 disp(k) k= 9; disp(k) end

  24. Developing For-Loop Solutions Illustrate the thinking associated with the design of for-loops The methodology of stepwise refinement. An example..

  25. A Game: TriStick Pick three sticks each having a random length between zero and one. You win if you can form a triangle whose sides are the sticks. Otherwise you lose.

  26. Win: Lose:

  27. Problem Estimate the probability of winning a game of TriStick by simulating a million games and counting the number of wins.

  28. Pseudocode Initialize running sum variable. Repeat 1,000,000 times: Play a game of TriStick by picking the three sticks. If you win increment the running sum Estimate the probability of winning

  29. Refine… % Initialize running sum variable Wins = 0; for n = 1:1000000 Play the nth game of TriStick by picking the three sticks. If you win increment the running sum. end % Estimate the prob of winning p = wins/1000000

  30. Refine the Loop Body Play the nth game of TriStick by picking the three sticks. If you win increment the running sum.

  31. Refine the Loop Body % Play the nth game of TriStick % by picking the three sticks. a = rand; b = rand; c = rand; if (a<b+c) && (b<a+c) && (c<a+b) % No stick is longer than the % sum of the other two. wins = wins+1; end

  32. Key Problem-Solving Strategy Progress from pseudocode to Matlab through a sequence of refinements. Comments have an essential role during the transitions. They “stay on” all the way to the finished fragment.

More Related