330 likes | 501 Views
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.
E N D
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 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.
Pattern for doing something n times n= _____ for k= 1:n % code to do % that something end Definite iteration
% 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)
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
% 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: !
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
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
4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 k
4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 4 k
4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 k
4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 9 k
4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 5 k
4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 5 5 k
4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 5 9 k
4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 5 9 9 k
4 5 6 for k = 4:6 disp(k) k= 9; disp(k) end Output in Command Window 4 9 5 9 6 k
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
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
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
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
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
Developing For-Loop Solutions Illustrate the thinking associated with the design of for-loops The methodology of stepwise refinement. An example..
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.
Win: Lose:
Problem Estimate the probability of winning a game of TriStick by simulating a million games and counting the number of wins.
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
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
Refine the Loop Body Play the nth game of TriStick by picking the three sticks. If you win increment the running sum.
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
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.