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

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

  • View
    221

  • Download
    2

Embed Size (px)

Citation preview

6. More on the For-Loop

Using the Count Variable

Developing For-Loop Solutions

Insight Through Computing

Syntax of the for loop

for <var> = <start value>:<incr>:<end bound>

statements to be executed repeatedly

end

Loop body

Insight Through Computing

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.

Insight Through Computing

Pattern for doing something n times

n= _____

for k= 1:n

% code to do

% that something

end

Definite iteration

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)

Insight Through Computing

for loop examples

for k= 2:0.5:3 k takes on the values 2,2.5,3disp(k) Non-integer increment is OK

endfor k= 1:4 k takes on the values 1,2,3,4

disp(k) Default increment is 1endfor k= 0:-2:-6 k takes on the values 0,-2,-4,-6

disp(k) “Increment” may be negativeendfor k= 0:-2:-7 k takes on the values 0,-2,-4,-6

disp(k) Colon expression specifies a boundendfor k= 5:2:1 The set of values for k is the empty

disp(k) set: the loop body won’t executeend

Insight Through Computing

% What will be printed?for k= 10:-1:14

fprintf(‘%d ’, k)endfprintf(‘!’)

B: 10 (then error)

C: 10 !

D: 14 !

A: error (incorrect bounds)

E: !

Insight Through Computing

What will be displayed when you run the following script?

for k = 4:6

disp(k)

k= 9;

disp(k)

end

4

9

4

4 Something else …or or

A B C

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

k

Output in Command Window

With this loop header, k “promises” to be these values, 1 at a time

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

4k

Output in Command Window

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

4k

Output in Command Window

4

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

9k

Output in Command Window

4

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

9k

Output in Command Window

49

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

5k

Output in Command Window

49

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

5k

Output in Command Window

495

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

9k

Output in Command Window

495

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

9k

Output in Command Window

4959

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

6k

Output in Command Window

4959

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

6k

Output in Command Window

49596

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

9k

Output in Command Window

49596

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

9k

Output in Command Window

495969

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

9k

Output in Command Window

495969

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

This is NOT a condition (boolean expression) that checks whether k<=6.

It is an expression that specifies values:

Insight Through Computing

Developing For-Loop Solutions

Illustrate the thinking associated with the design of for-loops

The methodology of stepwise refinement.

An example..

Insight Through Computing

A Game: TriStick

Pick three sticks each having a randomlength between zero and one.

You win if you can form a trianglewhose sides are the sticks. Otherwiseyou lose.

Insight Through Computing

Win:

Lose:

Insight Through Computing

Problem

Estimate the probability of winninga game of TriStick by simulating a million games and counting the numberof wins.

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 sumEstimate the probability of winning

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

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.

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

Insight Through Computing

Key Problem-Solving Strategy

Progress from pseudocode to Matlabthrough a sequence of refinements.

Comments have an essential role duringthe transitions. They “stay on” all theway to the finished fragment.