Top Banner
Week 7 - Programming I • Relational Operators • A > B • Logical Operators • A | B • For Loops • for n = 1:10 – commands • end
28

Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Week 7 - Programming I

• Relational Operators• A > B

• Logical Operators• A | B

• For Loops• for n = 1:10

– commands

• end

Page 2: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Week 7 - Programming I

• Introduce programming:– “scripts” and “functions” are programs of sequentially

evaluated commands

• Today, extend features to:• additional operators

• branches to control operations

• loops to repeat operations

• Textbook chapter 7, (sections 7.1, 7.2.1, 7.2.2, 7.4.1, 7.6)

Page 3: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Relational Operators

Used to compare A and B: A op B• A and B can be:

– Variables or constants or expressions to compute

– Scalars or arrays (watch the sizes on arrays!)

– Numeric or string

• Result is true (1) or false (0) – perhaps an array• Operators: > > = = =

< < = ~ =

Page 4: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

• Examples:

expression result

5 < 7 1

[ 3 5 2 ] > = [ 1 0 12 ] 1 1 0

max( 1:6 ) < = 7 1

[3 pi -12 ] > 1 1 1 0

'Tom' = = 'Bob' 0 1 0

'Tom' = = 'm' 0 0 1 Note – arrays and strings need to be the same size

Page 5: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Notes:

• Can compute using the result: e.g. – “how many of a

particular letter in a state name?

Page 6: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

• Don’t confuse = = and =

• Round off errors can impact ~ = sin(0) = = 0 1

sin(pi) = = 0 0

instead, test size

abs(sin(pi)) < = eps 1

0 2 4 6 8-1

-0.5

0

0.5

1

Page 7: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Logical Operators

Typically, used to combine A and B: A op B• A and B can be:

– Variables or constants or expressions to compute

– Scalars or arrays, numeric or string

• A and B are interpreted as binary: – Numeric 0 is interpreted as false

– All else is interpreted as true (equal to 1)

• Result is true (1) or false (0) – perhaps an array

Page 8: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

• Basic operators: and & or |

xor not ~

A B A&B A|B xor(A,B) ~A

0 0 0 0 0 1

0 1 0 1 1 1

1 0 0 1 1 0

1 1 1 1 0 0

“truth table” “unary” operator

Page 9: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

• Examples: expression result(5<7)&(11>9) 1 'Tom'= ='m' | 'Tom'= ='o' 0 1 1

• Also consider: name = input('enter name','s');name = = 'Tom' | name = = 'Bob'

orroll = sum(ceil(6*rand(1,2)));roll = = 7 | roll = = 11

Page 10: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Operator Precedence (left to right)

1. Parentheses ( )2. Transpose(') and power(.^)3. Negation (-) and logical negation (~)4. Multiplication (.*) and division (./), 5. Addition (+) and subtraction (-)6. Colon operator (:)7. Relational operators (<, <=, >, >=, = =, ~=)8. Logical AND (&)9. Logical OR (|)

Page 11: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Branches Conditional Statements

Commands to select and execute certain blocks of code, skipping other blocks.

• Three types in Matlab:– if/else– switch– try/catch

this week

Page 12: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

“If/Else”

Use relational and logical operators to determine what commands to execute:

if expression{commands if true }

else{commands if false }

end

evaluate this

use of blue in editor;also, auto indentation

Page 13: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Example 1 – output whether a variable x is positive or not:

x = … { computed somehow };

if x > 0

disp('the value is positive')

else

disp('the value is negative or zero')

end

Page 14: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Example 2 – output a warning if the variable x is negative (note that there is no “else” portion in this test):

x = … { computed somehow };

if x < 0

disp(‘Warning: negative value’)

end

no else component

Page 15: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Example 3 – ask if a plot should be drawn:

x = input(‘Plot now? ‘,’s’);

if x = = ‘yes’ | x = = ‘YES’

plot( ….. )

end more complicated expression

Page 16: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Loops

Commands to repeatedly execute certain blocks of code

• Two types in Matlab:– for– while this week

Page 17: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

The “for” Loop

Used for a specific number of repetitions of agroup of commands:

for index = array{ commands to be repeated go

here }end

Rules:• One repetition per column of array• index takes on the corresponding column’s values

Page 18: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Homework revisited – collect 7 digits of a telephone number and store in an array:

for digit = 1:7

number(digit) = input('enter value ');

end

7 repetitions since the array is [ 1 2 3 4 5 6 7 ]

digit cycles through the 7 valuesto create the 1 by 7 array “number”

Page 19: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Page 20: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Example 1 – calculating interest for 10 years:

value = 1000;

for year = 1:10

value = value * 1.08;

fprintf('$ %6.2f after %2d years\n', value,year)

end

no need for a counter variable!year takes on the values 1 through 10

Page 21: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Example 2 – implement a count down timer

(in seconds):

time = input(‘how long? ‘);

for count = time:-1:1

pause(1)

disp([ num2str(count),’ seconds left’])

end

disp(‘done’)

Page 22: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Example 3 – a general vector for array:

for x = [ 8 7 4 5 8 0 2 ] disp(['dial ', num2str(x) ]) pause(1)end

Page 23: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Example 4 – a matrix for array:

board =(2*(rand(3,3)>.2)–1).*(rand(3,3)>.5)for x = board xend

Page 24: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Example 5 – even a string array:

for x = 'EGR106' disp(x)end

Page 25: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Early Termination of Loops

Use the break command:

for variable = {array of length n} …..

breakend

Terminates the loop

Page 26: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Example – Hi-Lo: a guessing game with feedback

select hidden number

input guess

correct?

yes

noprovide hi/lo feedback

5 tries?

yesno

win

lose

Page 27: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

% HI-LO numb = round ( 100 * rand (1) ); for count = 1:5 guess = input('guess my number '); if guess = = numb

disp( 'You got it !!!' ) break

end if guess > numb disp('you are too high') else disp('you are too low') end if count = = 5

disp('Sorry, you lose! ') endend

Page 28: Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Example – calculating interest until the amount doubles using a for loop:

value = 1000;

for year = 1:1000

value = value * 1.08;

fprintf('$ %6.2f after %2d years\n', value,year)

if value >= 2000

break

end

end

will calculate up to 1000years, if necessary

if condition decideswhen to terminate loop