- 73 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' “Operators” (i.e. “symbols”)' - fraley

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

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

Overview: Specific Symbols that Represent Specific Actions

Arithmetic

Relational

Boolean

Output values

1. Overview: most Operators

- There are 3 primary groups of operators
- One operator is very different in programming compared to math

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

Subtraction operator

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: “make a variable equal to something”

Overview, cont.

- There are 2 types of operands:
- Numerical 1, 3.5, -47
- Logicaltrue, 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

2. Arithmetic Operators

REVIEW

- Arithmetic equations:

variableName = equation ;

- MATLAB executes the equation FIRST (following the order of operation from left to right), then stores the result in the variable on the left.
- However, in the equation itself, MATLAB respects the Order of Operations:
- 2+3*5 is the same as 2+(3*5), but different than (2+3)*5
- val1*val2/val3^4 + val5/(val6+val7);
- Remember the multiplication operator isn’t implied

>>(2)(5)(5.5)+5(6/3) <enter> will crash MATLAB.

3. Relational Operators

- Relational operators allow a comparison to be evaluated.

Is thrust_a greater than thrust_b? True/false? 1/0?

Is surface1 equal to surface2? True/false? 1/0?

Isload1less than or equal toload2? True/false? 1/0?

- Examples:

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 ?

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 = %NOT A RELATIONAL OP.

y = 5; %“Store the value 5 in the % variable y”

Notethat == and = are DIFFERENT!

Spaces or not?

- When one relational operator is made up of 2 symbols (<=, >=, ~=, ==):
- KEEP THEM GLUED TOGETHER

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

4. Boolean Operators

- These operators take logical 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

Boolean Operator #1: && “and”

- Two & symbols (“Ampersand”), glued together

&&

- Both relational expressions must be truefor the combined expression to be true
- X && Y yields trueiff both XandY are true

e.g. (3<5) && (8>=8) ?

(x< 3) && (x > 5) ?

x = 52.1;

(5.5<x) && (x<100.2) ?

&&, 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!

True/False

(2 > 3) && (3 < 29.3)

- True (1)
- False (0)
- Impossible to determine

(22 > 3) && (3 > 29.3)

- True (1)
- False (0)
- Impossible to determine

(22 > x) && (x > 29.3)

- True (1)
- False (0)
- Impossible to determine

(x<2) && (y>0)

- True (1)
- False (0)
- Impossible to determine

- What is the result of the following statement?

True/False

F && T

- True (1)
- False (0)

T && F

- True (1)
- False (0)

F && F

- True (1)
- False (0)

T && T

- True (1)
- False (0)

- In other words, there are 4 options:

Boolean Operator #2: || “or”

- Two | symbols (“pipe”), glued together

||

- At least ONE relational expressions must be truefor the combined expression to be true
- X || Y yields true if eitherXorY (or both) are true

e.g. (3<5) || (5>=8) ?

x = 4.2;

(x< 3) || (x > 5) ?

True/False

(2 > 3) || (3 < 29.3)

- True (1)
- False (0)
- Impossible to determine

(22 > 3) || (3 > 29.3)

- True (1)
- False (0)
- Impossible to determine

(22 > x) || (x > 29.3)

- True (1)
- False (0)
- Impossible to determine

(x<2) || (y>0)

- True (1)
- False (0)
- Impossible to determine

- What is the result of the following statement?

True/False

F || T

- True (1)
- False (0)

T || F

- True (1)
- False (0)

F || F

- True (1)
- False (0)

T || T

- True (1)
- False (0)

- Again, there are 4 options:

Priorities between Boolean Operators

- Which operator has priority in the following?

1 + 1 + 0 * 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)) ?

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

- Example:
- the value y entered by the user should NOT be between 4 and 9 cm included:

%assume user enters 7.4 when asked for a value of y

~(4<=y && y<=9) ?

5. Operators: Output values

Type Input values Output values

Arithmetic: Numbers Numbers

e.g. 5 * 315

Relational: Numbers Logical

e.g. 5 < 3false

Boolean: Logical Logical

e.g. ~truefalse

true & false

Key Ideas

- Vocabulary: operators, operands, arithmetic, relational, boolean, unary, binary, numerical, logical
- Assignment vs. “is equal to” operator
- Find the &, |, and ~ symbols on the keyboard
- When does a && b && c evaluate the true?
- When does a || b || c evaluate to true?
- When does a && b || c && d evaluate to true?
- Order of operations is respected when MATLAB executes any expression

Download Presentation

Connecting to Server..