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

## Presentation on theme: "Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare."— Presentation transcript:

Fall 2006AE6382 Design Computing1 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

Fall 2006AE6382 Design Computing2 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.

Fall 2006AE6382 Design Computing3 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

Fall 2006AE6382 Design Computing4 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)

Fall 2006AE6382 Design Computing5 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.

Fall 2006AE6382 Design Computing6 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.

Fall 2006AE6382 Design Computing7 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.

Fall 2006AE6382 Design Computing8 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!

Fall 2006AE6382 Design Computing9 More Examples: a=6; b=10; c=-2; Try the following examples without the use of Matlab: –X1 = abs(c)>3 & c 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.

Fall 2006AE6382 Design Computing10 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 ( ), greater than or equal to (>=), equal to (==), not equal to (~=) 7. logical AND (&) 8. logical OR (|)

Fall 2006AE6382 Design Computing11 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 10 –Three = d.*c > a.*b –Four = b.*3<= 100 & d<10 & a.*d==b

Fall 2006AE6382 Design Computing12 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;

Download ppt "Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare."

Similar presentations