Presentation is loading. Please wait.

Presentation is loading. Please wait.

“Operators” (i.e. “symbols”)

Similar presentations


Presentation on theme: "“Operators” (i.e. “symbols”)"— Presentation transcript:

1 “Operators” (i.e. “symbols”)
Overview: Specific Symbols that Represent Specific Actions Arithmetic Relational Boolean Output values

2 Overview: most Operators
There are 3 primary groups of operators One programming operator is very different from its use in math: 1. ARITHMETIC + Addition - Subtraction * Multiplication /,\ Division ^ Exponentiation, i.e. “To the power of” 2. RELATIONAL < strictly less than > strictly greater than <= less than or equal to >= greater than or equal to == is equal to ~= is not equal to 3. BOOLEAN && “AND” || “OR” ~ “NOT” = “the assignment operator”

3 4 * 5 -5 Overview, cont. Operators work on operands. Binary Operator
Requires two operands to work 4 * 5 operands Multiplication operator -5 Unary Operator Requires one operand to work operand Negative operator

4 kineticEnergy = 1 / 2 * mass * vel ^ 2
Overview, cont. There are 2 types of operands: Numerical 1, 3.5, -47 Logical true, false Arithmetic (+, -, /, *, ^, =) and relational (<, <=, >, >= ,==, ~=) operators work with numerical operands Numerical Operands kineticEnergy = 1 / 2 * mass * vel ^ 2 Arithmetic operators Assign operator: “place one or more values into memory”

5 Overview, cont. There are 2 types of operands:
Numerical 1, 3.5, -47 Logical true, false Boolean (&&,||,~) operators work on logical operands “ if this is true and this is false… do something” if (it's raining outside) and (you have an umbrella) go, you won't get wet else stay inside! end

6 True, False, 1, and 0?! True False 1

7 3. Relational Operators Relational operators allow a comparison to be evaluated. Is thrust_a greater than thrust_b?  True / False Is surface1 equal to surface2?  True / False? Is load1 less than or equal to load2?  True / False? Examples: thrust_a > thrust_b Is thrust_a strictly greater than thrust_b? radius <=0 Is radius negative or zero? nb_attempts<= 3 Is the number of attempts less than or equal to 3? 3 >= nb_attempts Is 3 greater than or equal to the number of attempts? value ~= 2 Is value not equal to 2?

8 Relational Operators, cont.
***COMPARISON*** == y == 5 % “Does y hold the value 5?” % “Is y equal to 5?” Example: menuChosen == 1 % did user choose menu #1 ?

9 Relational Operators, cont.
***COMPARISON*** == y == 5 % “Does y hold the value 5?” % “Is y equal to 5?” Example: menuChosen == 1 % did user choose menu #1 ? Assignment = % A numerical operator y = 5; % “Store the value 5 in the % variable y” Note that == and = are DIFFERENT!

10 Spaces or not? When one relational operator is made up of 2 symbols (<=, >=, ~=, ==): KEEP THEM GLUED TOGETHER

11 Spaces or not? When one relational operator is made up of 2 symbols (<=, >=, ~=, ==): KEEP THEM GLUED TOGETHER Regardless of which operator is used, a space can be used before and/or after. All these are identical to MATLAB: thrustA<=thrustB %no spaces anywhere thrustA <=thrustB %1 space before the operator thrustA<= thrustB %1 space after the operator thrustA <= thrustB %1 space before AND after

12 4. Boolean Operators These operators take logical scalar values and perform some operation on them to yield a logical value Two Boolean operators allow to COMBINE relational expressions && Logical AND || Logical OR One Boolean operator allows to NEGATE the result ~ Logical NOT “Negates”: turns true values into false, and false values into true

13 Boolean Operator #1: && “logical and”
Two & symbols (“Ampersand”), glued together && Both relational expressions must be true for the combined expression to be true X && Y yields true if and only if both X and Y are true e.g. (3 < 5) && (8 >= 8) ? (x < 1) && (x > 5) ? x = 52.1; (5.5 < x) && (x < 100.2) ?

14 &&, continued Use of parenthesis e.g. (3<5) && (8>=8) true
same as 3<5 && 8>=8 true (x<3) && (x>5) false same as x<3 && x>5 false For sanity, at least use spaces before/after the operator!

15 True/False What is the result of the following statement?
(2 > 3) && (3 < 29.3) True False Impossible to determine (22 > 3) && (3 > 29.3) (22 > x) && (x > 29.3) True False Impossible to determine (x<2) && (y>0)

16 True/False In other words, there are 4 options: F && T T && F F && F

17 Boolean Operator #2: || “logical or”
Two | (“pipe”) symbols, glued together || At least ONE relational expressions must be true for the combined expression to be true X || Y yields true if either X or Y (or both) are true e.g. (3<5) || (5>=8) ? x = 4.2; (x< 3) || (x > 5) ?

18 True/False What is the result of the following statement?
(2 > 3) || (3 < 29.3) True False Impossible to determine (22 > 3) || (3 > 29.3) (22 > x) || (x > 29.3) True False Impossible to determine (x<2) || (y>0)

19 True/False Again, there are 4 options: F || T T || F F || F T || T

20 Priorities between Boolean Operators
Which operator has priority in the following? * 1 Just like * has priority over + , && has priority over || What is the result of this statement? x = 44.5; y = 55; (x<=50) || (0<y) && (y<40) ? ((x<=50) || (0<y)) && (y<40) ? (x<=50) || ((0<y) && (y<40)) ?

21 Boolean Operator #3: NOT
One ~ symbol (“tilde”) “NOT” : negates a value Example: x = true; %keyword is known to MATLAB y = ~x; %y now has the value false The value y entered by the user should NOT be between 4 and 9 cm, inclusive: % Suppose the user enters 7.4 as a value for y ~(4<=y && y<=9) ?

22 5. Operators: Result values
Type Operand type Result type Arithmetic: Numbers Numbers e.g. 5 * 3 15 Relational: Numbers Logical e.g. 5 < 3 false Boolean: Logical Logical e.g. ~true false true & false

23 Order of Operations Operator Priority Parenthesis () Highest
Exponentiation ^ Unary: -, NOT ~ Multiplication/division: * / \ Addition/subtraction: Relational: <, <=, >, >=, ==, ~= AND: && OR: || Assignment: = Lowest


Download ppt "“Operators” (i.e. “symbols”)"

Similar presentations


Ads by Google