fall 2006ae6382 design computing1 relational and logical operators use relational operators to test...

12
Fall 2006 AE6382 Design Computing 1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare relationships using logical operators Analyze the precedence of operators

Upload: maud-baker

Post on 21-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare

Fall 2006AE6382 Design Computing 1

Relational and Logical Operators

• Use relational operators to test two values• Work with values of true and false• Compare relationships using logical operators• Analyze the precedence of operators

Page 2: Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare

Fall 2006AE6382 Design Computing 2

Recall from Arithmetic Operations

• Arithmetic operators:– +, - , *, / , ^

• Rules of precedence for arithmetic operators:– 1. Exponents– 2. Multiplication / Division– 3. Addition/Subtraction

• We can have a combination of arithmetic operators for multiple values.

Page 3: Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare

Fall 2006AE6382 Design Computing 3

Relational Operators

• Used to compare two values

• Returns a value of true or false.

• In Matlab, – 1 = true (actually non-zero);

– 0 = false;

Relational Operators:< less than

<= less than or equal to

> greater than

>= greater than or equal to

== equals to

~ = not equal to

Page 4: Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare

Fall 2006AE6382 Design Computing 4

Order of Precedence

• When relational operators are present:– All arithmetic operations are performed first (in their particular

order)– The relational operators are evaluated after.

• Example 1(2*3) > (4+1); % solve by hand, then type into MATLAB- The multiplication and addition are first:

- 6 > 5

- The relational operator is evaluated:- 6 is greater than 5, so this returns 1 (true)

Page 5: Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare

Fall 2006AE6382 Design Computing 5

Logical Operators

• Logical Operators:– Evaluate the relationship between what relational

operators return.– Return a value of true or false.

• & AND• | OR• ~ NOT• Evaluated after all other operators have been

performed.

Page 6: Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare

Fall 2006AE6382 Design Computing 6

Logical Operators: AND, OR, NOT

• AND: &– Returns true if two expressions being compared are true.– Returns false if any of the two is false.

• OR: |– Returns true if any of the two expressions is true.– Returns false only if the two are false.

• NOT: ~– Returns true if the single expression is false.– Returns false if the single expression is true.

Page 7: Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare

Fall 2006AE6382 Design Computing 7

Examples:

• a=7; b=4; c=3;• ~(a==3*b)

– Evaluates 3*b = 12– Reads: is (a==12) not (from the ~) true?– Returns ans = 1 (true)

• a > 5 & b > 5– Evaluates (a>5) and (b>5) separately. – One returns true, the other returns false.– Since both are not true, the expression returns false.

Page 8: Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare

Fall 2006AE6382 Design Computing 8

Using Logical Values in Assignments

• True/False values can be assigned to variables.• The variables will be assigned the value that returns

from relational and/or logical operators.• The variables will have a value of 1 or 0.• Example:

– X = a > 2;• Then x = 1;

– Y = b==5;• Y will be equal to 0.

• This kind of arithmetic is VERY common in Matlab!

Page 9: Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare

Fall 2006AE6382 Design Computing 9

More Examples:

• a=6; b=10; c=-2;• Try the following examples without the use of Matlab:

– X1 = abs(c)>3 & c<(b-a) & b+a > 3– X2 = (b==10) | (a< 4)– X3 = a.*5 >= b.*3 & c < a

• Safety tip: use parentheses to make expression more readable, see example X2.

Page 10: Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare

Fall 2006AE6382 Design Computing 10

Precedence Expanded: See Section 9.3

1. transpose (.'), power (.^), complex conjugate, transpose ('), matrix power (^)

2. unary plus (+), unary minus (-), logical negation (~)

3. multiplication (.*), right division (./), left division (.\), matrix multiplication (*), matrix right division (/), matrix left division (\)

4. addition (+), subtraction (-)

5. colon operator (:)

6. less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to (~=)

7. logical AND (&)

8. logical OR (|)

Page 11: Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare

Fall 2006AE6382 Design Computing 11

Practice

• Evaluate the following without Matlab:– Practice first without the help of Matlab, and then test your

expected results using Matlab…

– a = 4; b = 20; c = 12; d = 5;– One = a>4 & b==20– Two = b<40 | c>10– Three = d.*c > a.*b– Four = b.*3<= 100 & d<10 & a.*d==b

Page 12: Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare

Fall 2006AE6382 Design Computing 12

More practice

• When comparing vectors, the operator (>, <=, ~, AND, etc.) is applied element-by-element:

a = [0,2,4,2]; b = [4,1,-2,3];• What is:

– C = a .* b;– C = b.^2-a.*b– C = a >= b;