Operators
Overview
- Operators
- Arithmetic Operators
- String Operators
- Logical Operators
- Relational Operators
- Parenthesis and Operator Hierarchy
Operators
- An operator is a symbol which denotes a function to be done.
- An operator is followed by at least one operand.
- operands are the value (literal or variable) being operated on.
the 5 Arithmetic Operators
=
assignment+
addition-
subtraction*
multiplication/
division%
modulo
Assignment Operator =
- The assignment operator has two operands:
- The variable name
- The variable value
Addition Operator +
- The addition operator has two operands:
- augend is the base value
- addend is the value to add to the base value
- By adding values, we obtain a sum.
let augend = 5;
let addend = 10;
let sum = augend + addend; // `sum` is 15
Subtraction Operator -
- The subtraction operator has two operands:
- minuend is the base value
- subtrahend is the value to remove from the base value
- By subtracting values, we obtain a difference.
let minuend = 15;
let subtrahend = 10;
let difference = minuend - subtrahend // `difference` is 5
Multiplication Operator *
- The multiplication operator has two operands:
- multiplicand is the base value
- multiplier is the number of times we increment the base value.
- By multiplying values, we obtain a product.
let multiplicand = 5;
let multiplier = 10;
let product = multiplicand * multiplier // `product` is 50
Division Operator /
- The divison operator has two operands:
- dividend is the base value
- divisor is the value to remove from the base value
- By dividing values, we obtain a quotient.
let dividend = 10
let divisor = 5
let quotient = dividend + divisor // `quotient` is 2
Modulo Operator %
- The modulo operator has two operands:
- dividend is the base value
- divisor is the value to remove from the base value
- By modding values, we obtain a remainder.
let dividend = 10
let divisor = 5
let remainder = dividend + divisor // `quotient` is 2
the 2 string operators
the concatenation operator, +
- The concatenation operator has two operands which we will refer to as
body
andappendage
:- body is the string to be appended to.
- appendage is the string to be appended.
let body = "Hello ";
let appendage = "World";
let result = body + appendage;
the multiplication operator, *
- String multiplication operator has two operands:
- multiplicand is the base value, and it must of
str
type. - multiplier is the number of times we increment the base value, and it must a numeric type.
- multiplicand is the base value, and it must of
- By multiplying values, we obtain a product.
let multiplicand = "abc";
let multiplier = 3;
let product = multiplicand * multiplier # `product` is "abcabcabc";
the 6 Logical Operators
not
conditional negationand
conditional andor
conditional or
the unary not
operator
- Returns true if the operand to the right evaluates to false.
- Returns false if the operand to the right is true.
let rightOperand = true;
let outcome = not rightOperand # outcome is `false`;
let rightOperand = false;
let outcome = not rightOperand # outcome is `true`;
the binary and
operator
- Returns true if both of the operands evaluate to true.
- Both operands are evaluated before the
and
operator is applied. - The following code blocks have identical behavior in all cases
let doorIsUnlocked = door.isUnlocked();
let doorIsOpen = door.isOpen();
let canWalkThroughDoor = doorIsUnlocked and doorIsOpen;
- if
door
is not unlocked, then continue to evaluate if door is open
the binary &&
operator
- Expression resolves to
true
if both of the operands evaluate totrue
. - if the operand on the left returns
false
, the operation resolves tofalse
without evaluating the operand on the right.
Example
- The following code blocks have identical behavior in all cases
let canWalkThroughDoor = door.isUnlocked() and door.isOpen()
let canWalkThroughDoor = false
if(door.isUnlocked()){
if(door.isOpen()) {
canWalkThroughDoor = true
}
}
the binary or
operator
- Returns
true
if at least one of the operands evaluates to true. - Both operands are evaluated before the
or
operator is applied.
let doorIsUnlocked = door.isUnlocked();
let doorIsOpen = door.isOpen();
let canWalkThroughDoor = doorIsUnlocked or doorIsOpen;
- if
door
is not unlocked, then continue to evaluate if door is open
the binary or
operator
- Expression resolves to
true
if one of the operands evaluate totrue
. - if left operand is
true
, the operation resolves totrue
without evaluating the operand on the right.
the binary ||
operator
Example
- The following code blocks have identical behavior in all cases
let canWalkThroughDoor = door.isUnlocked() || door.isOpen();
let canWalkThroughDoor = false;
if(door.isUnlocked()){
canWalkThroughDoor = true;
}
if(door.isOpen()){
canWalkThroughDoor = true;
}
Relational Operators
- there are 6 relational operators that compare two numbers and return a value.
the 6 Relational Operators
<
less than>
greater than<=
less than or equal to>=
greater than or equal to==
equal to!=
not equal to
Relational Operators
- Each operand is a numeric or boolean value.
- Boolean values are either true or false.
- The result of a relational operation is a boolean value.
<
Less than
true
if left operand is less than right operand, otherwisefalse
.
operand1 = 0
operand2 = 1
outcome = operand1 < operand2 # outcome is `true`
operand1 = 1
operand2 = 0
outcome = operand1 < operand2 # outcome is `false`
>
Greater than
true
if left operand is greater than right operand, otherwisefalse
.
operand1 = 1
operand2 = 0
outcome = operand1 > operand2 # outcome is `true`
operand1 = 0
operand2 = 1
outcome = operand1 > operand2 # outcome is `false`
<=
Less than or equal to
true
if left operand is less than or equal to right operand, otherwisefalse
.
operand1 = 0
operand2 = 1
outcome = operand1 <= operand2 # outcome is `true`
operand1 = 1
operand2 = 0
outcome = operand1 <= operand2 # outcome is `false`
operand1 = 0
operand2 = 0
outcome = operand1 <= operand2 # outcome is `true`
>=
Greater than or equal to
true
if left operand is greatre than or equal to right operand, otherwisefalse
.
operand1 = 0
operand2 = 1
outcome = operand1 >= operand2 # outcome is `false`
operand1 = 1
operand2 = 0
outcome = operand1 >= operand2 # outcome is `true`
operand1 = 0
operand2 = 0
outcome = operand1 >= operand2 # outcome is `true`
==
Equal to
true
if left operand is equal to right operand, otherwisefalse
.
operand1 = 0
operand2 = 1
outcome = operand1 == operand2 # outcome is `false`
operand1 = 1
operand2 = 0
outcome = operand1 == operand2 # outcome is `false`
operand1 = 0
operand2 = 0
outcome = operand1 == operand2 # outcome is `true`
Increment / Decrement Operators
+=
Increment Operator
+=
modifies the operand to the left to by adding the operand to the right to it.
startingValue = 10
startingValue += 5 # startingValue is `15`
startingValue = 10
startingValue = startingValue + 5 # startingValue is `15`
-=
, Decrement Operator
-=
modifies the operand to the left to by subtracting the operand to the right from it.- the following blocks of code are behaviorally equivalent
startingValue = 10
startingValue -= 5 # startingValue is `5`
startingValue = 10
startingValue = startingValue - 5 # startingValue is `5`
Parentheses and Operator Hierarchy
a and b or c
- means
(a and b) or c
4 + 5 * 8 == 44
(4 + 5) * 8 == 72
*
,/
have higher precedence than+
,-
Since += associates right to left, the expression
a += b += c
means
a += (b += c)