Boolean Operators, AND.

Operators
Specific Symbols that Represent
Specific Actions
1.
2.
3.
4.
5.
Arithmetic
Relational
Boolean
Unary vs. Binary
Output values
1
Overview: all Operators

There are 3 groups of operators
1. ARITHMETIC
2. RELATIONAL
3. BOOLEAN
+ Addition
< strictly less than
&& “AND”
- Subtraction
> strictly greater than
|| “OR”
* Multiplication
<= less than or equal to
/ Division
>= greater than or equal to
^ Exponentiation,
i.e. “To the power of”
== is equal to
~
“NOT”
~= is not equal to
2
Overview, cont.

Operators work on operands. There are 2 types of
operands:
1.
2.
Numerical
Logical
1, 3.5, -47
true, false
3
Overview, cont.

Operators work on operands. There are 2 types of
operands:
1.
2.


Numerical
Logical
1, 3.5, -47
true, false
Arithmetic (+,-,/,*,^) and relational (<,<=,>,>=,==,~=)
operators work with numerical operands
Boolean (&&,||,~) operators work on logical operands
4
1. Arithmetic Operators

Arithmetic equations:
variableName = equation ;

MATLAB executes the equation FIRST, then stores the result in
the variable on the left.
5
1. Arithmetic Operators

Arithmetic equations:
variableName = equation ;


MATLAB executes the equation FIRST, 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
6
1. Arithmetic Operators

Arithmetic equations:
variableName = equation ;


MATLAB executes the equation FIRST, 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);
7
Arithmetic Operators, cont.
COMMON ERRORS

The multiplication is NOT implied.
The following will not work:
valueD + 2valueA(valueB(valueC)) %BAD

Type:
valueD + 2*valueA*valueB*valueC instead.

8
Arithmetic Operators, cont.
COMMON ERRORS

The multiplication is NOT implied.
The following will not work:
valueD + 2valueA(valueB(valueC)) %BAD

Type:
valueD + 2*valueA*valueB*valueC instead.


There is no reason to put additional () at the very
beginning or end of the equation. Keep it simple.

result = (2+(3*5)); %  too redundant…
9
2. Relational Operators

Relational operators allow a comparison to be evaluated.
Is thrust_a greater than thrust_b?
Is surface1 equal to surface2?
10
2. Relational Operators

Relational operators allow a comparison to be evaluated.
Is thrust_a greater than thrust_b?
Is surface1 equal to surface2?

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?
11
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 ?

12
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 ?

Note that == and = are DIFFERENT!

Assignment =
y = 5;
%NOT A RELATIONAL OP.
%“Store the value 5 in the
% variable y”
13
Spaces or not?

When one relational operator is made up of 2 symbols
(<=, >=, ~=, ==):

KEEP THEM GLUED TOGETHER
14
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
thrustA <=thrustB
thrustA<= thrustB
thrustA <= thrustB
%no spaces anywhere
%1 space before the operator
%1 space after the operator
%1 space before AND after
15
3. Boolean Operators

These operators take logical values and perform some
operation on them to yield a logical value
16
3. 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
17
3. 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
18
Boolean Operators, AND.

Two & symbols (“Ampersand”), glued together

Both relational expressions must be true for the
combined expression to be true

X && Y yields true iff both X and Y are true
19
Boolean Operators, AND.

Two & symbols (“Ampersand”), glued together

Both relational expressions must be true for the
combined expression to be true

X && Y yields true iff both X and Y are true
e.g.
(3<5) && (8>=8)
?
20
Boolean Operators, AND.

Two & symbols (“Ampersand”), glued together

Both relational expressions must be true for the
combined expression to be true

X && Y yields true iff both X and Y are true
e.g.
(3<5) && (8>=8)
true
(x< 3) && (x > 5)
?
21
Boolean Operators, AND.

Two & symbols (“Ampersand”), glued together

Both relational expressions must be true for the
combined expression to be true

X && Y yields true iff both X and Y are true
e.g.
(3<5) && (8>=8)
true
(x< 3) && (x > 5)
false
x = 52.1;
(5.5<x) && (x<100.2)
?
22
Boolean Operators, OR.

Two | symbols (“pipe”), 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
23
Boolean Operators, OR.

Two | symbols (“pipe”), 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)
?

24
Boolean Operators, OR.

Two | symbols (“pipe”), 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)
true

x = 4.2;
(x< 3) || (x > 5)
?
25
Boolean Operators, NOT.


One ~ symbol (“tilde”)
Example:
x = true; %keyword is known to MATLAB
y = ~x;
%y now has the value false
26
Boolean Operators, NOT.

One ~ symbol (“tilde”)

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
result = ~(4<=y && y<=9);
?

27
4. Operators: unary vs. binary

Operators can be unary – taking only one operand:
y = -x;
opposite = ~result;

Or binary operators – taking two operands:
z = x * y;
z = x + y;
z = x – y; %both unary and binary!
z = x / y;
z = x ^ y;
x >= 7
(x<3) && (3>y)
28
5. Operators: Output values
Type
Input values
Output values
Arithmetic:
e.g.
Numbers
5 * 3
Numbers
15
Relational:
e.g.
Numbers
5 < 3
Logical
false
Boolean:
e.g.
Logical
~true
Logical
false
29
Wrapping Up








Vocabulary: operators, arithmetic, relational, boolean,
unary, binary, operands, numerical, logical
Assignment vs. “is equal to” operator
Find the & symbol on the keyboard
Find the | symbol 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 the equation
30