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

Slides:



Advertisements
Similar presentations
Chapter 1 Discovery 1 Evaluate each expression, and compare the results obtained in the left column with the corresponding results in the right column.
Advertisements

Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
LECTURE 3: BASIC C OPERATORS. Objectives  In this chapter, you will learn about:  Arithmetic operators Unary operators Binary operators  Assignment.
Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
7.1Variable Notation.
Distributive Property
Copyright © Cengage Learning. All rights reserved. 6 Equations and Formulas.
SPAC Lab, Stevens Work Summary -- Matlab Ning Han, Hongbin Li Department of Electrical and Computer Engineering June 16,
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Chapter 4: Basic C Operators
Spreadsheets Objective 6.02
2 Explain advanced spreadsheet concepts and functions Advanced Calculations 1 Sabbir Saleh_Lecture_17_Computer Application_BBA.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
PRESENTATION 12 Basic Algebra. BASIC ALGEBRA DEFINITIONS A term of an algebraic expression is that part of the expression that is separated from the rest.
Algebra 2 Week #1A Review. It’s Friday! Week #1A – Section 4 Classwork – –He is decomposing –Buoy meets gull –Bushed Homework 1. x = 5 2. x =
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections.
ORDER OF OPERATIONS x 2 Evaluate the following arithmetic expression: x 2 Each student interpreted the problem differently, resulting in.
Algebraic Expressions & Polynomials
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences.
Section 2.2 More about Solving Equations. Objectives Use more than one property of equality to solve equations. Simplify expressions to solve equations.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
MM150 Unit 3 Seminar Agenda Seminar Topics Order of Operations Linear Equations in One Variable Formulas Applications of Linear Equations.
1-2 Order of Operations and Evaluating Expressions.
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Evaluating Integer Expressions Friday, December 25, 2015.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Order of Operations By Carl Stephen. Order of Operations  Parentheses  Exponents  Square roots  Multiplication  Division  Addition  Subtraction.
Operators & Expressions
Doing math In java.
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Chapter 1 Real Numbers and Introduction to Algebra.
Operators.
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
ORDER OF OPERATIONS LESSON 2.
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
Mental Math Everyone Take out a sheet of paper and Number your page = = = = = =
Logical Expressions ENGR 1181 MATLAB 6. Logical Expressions in Real Life Sorting objects in manufacturing processes can be accomplished automatically.
Array and Matrix Operations EE 201 Fall  Achieve Comprehension LOL of Array and Matrix Operations. Class Learning Objectives 2.
Warm-Up:. Evaluating Algebraic Expressions and Order of Operations.
Relational and Logical Operators EE 201 1C7-2 Spring 2012.
ECE 1304 Introduction to Electrical and Computer Engineering
Control Statements in Matlab
Sequence, Selection, Iteration The IF Statement
Arithmetic operations & assignment statement
Algebra 1 Section 2.3 Subtract real numbers
ORDER OF OPERATIONS BEMDAS. 1. Brackets - ( ) or [ ]
Logical Operators & Truth Tables.
Computers & Programming Languages
MATLAB Logical Expressions
Expressions.
Exponents and Order of Operations
Spreadsheets Objective 6.02
Section 6.1 Order of Operations
Matlab Basics.
Section 6.1 Order of Operations
Spreadsheets Objective 6.02
5.03 Apply operators and Boolean expressions
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 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;