Download presentation
Presentation is loading. Please wait.
1
6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions
2
Insight Through Computing Syntax of the for loop for = : : statements to be executed repeatedly end Loop body
3
Insight Through Computing Syntax of the for loop for = : : 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
Insight Through Computing Pattern for doing something n times n= _____ for k= 1:n % code to do % that something end Definite iteration
5
Insight Through Computing % 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
Insight Through Computing 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
7
Insight Through Computing % What will be printed? for k= 10:-1:14 fprintf(‘%d ’, k) end fprintf(‘!’) B: 10 (then error) C: 10 ! D: 14 ! A: error (incorrect bounds) E: !
8
Insight Through Computing What will be displayed when you run the following script? for k = 4:6 disp(k) k= 9; disp(k) end 4949 4444 Something else … or ABC
9
Insight Through Computing for k = 4:6 disp(k) k= 9; disp(k) end 456 k Output in Command Window With this loop header, k “promises” to be these values, 1 at a time
10
Insight Through Computing for k = 4:6 disp(k) k= 9; disp(k) end 456 4 k Output in Command Window
11
Insight Through Computing for k = 4:6 disp(k) k= 9; disp(k) end 456 4 k Output in Command Window 4
12
Insight Through Computing for k = 4:6 disp(k) k= 9; disp(k) end 456 9 k Output in Command Window 4
13
Insight Through Computing for k = 4:6 disp(k) k= 9; disp(k) end 456 9 k Output in Command Window 4949
14
Insight Through Computing for k = 4:6 disp(k) k= 9; disp(k) end 456 5 k Output in Command Window 4949
15
Insight Through Computing for k = 4:6 disp(k) k= 9; disp(k) end 456 5 k Output in Command Window 495495
16
Insight Through Computing for k = 4:6 disp(k) k= 9; disp(k) end 456 9 k Output in Command Window 495495
17
Insight Through Computing for k = 4:6 disp(k) k= 9; disp(k) end 456 9 k Output in Command Window 49594959
18
Insight Through Computing for k = 4:6 disp(k) k= 9; disp(k) end 456 6 k Output in Command Window 49594959
19
Insight Through Computing for k = 4:6 disp(k) k= 9; disp(k) end 456 6 k Output in Command Window 4959649596
20
Insight Through Computing for k = 4:6 disp(k) k= 9; disp(k) end 456 9 k Output in Command Window 4959649596
21
Insight Through Computing for k = 4:6 disp(k) k= 9; disp(k) end 456 9 k Output in Command Window 495969495969
22
Insight Through Computing for k = 4:6 disp(k) k= 9; disp(k) end 456 9 k Output in Command Window 495969495969
23
Insight Through Computing for k = 4:6 disp(k) k= 9; disp(k) end 456 This is NOT a condition (boolean expression) that checks whether k<=6. It is an expression that specifies values:
24
Insight Through Computing Developing For-Loop Solutions Illustrate the thinking associated with the design of for-loops The methodology of stepwise refinement. An example..
25
Insight Through Computing 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
Insight Through Computing Win: Lose:
27
Insight Through Computing Problem Estimate the probability of winning a game of TriStick by simulating a million games and counting the number of wins.
28
Insight Through Computing 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
Insight Through Computing 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
Insight Through Computing Refine the Loop Body Play the nth game of TriStick by picking the three sticks. If you win increment the running sum.
31
Insight Through Computing 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
Insight Through Computing 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.