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.
augend = 5
addend = 10
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.
minuend = 15
subtrahend = 10
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.
multiplicand = 5
multiplier = 10
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.
dividend = 10
divisor = 5
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.
dividend = 10
divisor = 5
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
bodyandappendage:- body is the string to be appended to.
- appendage is the string to be appended.
body = "Hello "
appendage = "World"
result = body + appendage
the multiplication operator, *
- String multiplication operator has two operands:
- multiplicand is the base value, and it must of
strtype. - 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.
multiplicand = "abc"
multiplier = 3
product = multiplicand * multiplier # `product` is "abcabcabc"
the 6 Logical Operators
notconditional negationandconditional andorconditional 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.
rightOperand = true
outcome = not rightOperand # outcome is `false`
rightOperand = false
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
andoperator is applied. - The following code blocks have identical behavior in all cases
doorIsUnlocked = door.isUnlocked()
doorIsOpen = door.isOpen()
canWalkThroughDoor = doorIsUnlocked and doorIsOpen
- if
dooris not unlocked, then continue to evaluate if door is open
the binary && operator
- Expression resolves to
trueif both of the operands evaluate totrue. - if the operand on the left returns
false, the operation resolves tofalsewithout evaluating the operand on the right.
Example
- The following code blocks have identical behavior in all cases
canWalkThroughDoor = door.isUnlocked() and door.isOpen()
canWalkThroughDoor = false
if(door.isUnlocked()):
if(door.isOpen()):
canWalkThroughDoor = true
the binary or operator
- Returns
trueif at least one of the operands evaluates to true. - Both operands are evaluated before the
oroperator is applied.
doorIsUnlocked = door.isUnlocked()
doorIsOpen = door.isOpen()
canWalkThroughDoor = doorIsUnlocked or doorIsOpen
- if
dooris not unlocked, then continue to evaluate if door is open
the binary or operator
- Expression resolves to
trueif one of the operands evaluate totrue. - if left operand is
true, the operation resolves totruewithout evaluating the operand on the right.
the binary or operator
Example
- The following code blocks have identical behavior in all cases
canWalkThroughDoor = door.isUnlocked() or door.isOpen()
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
trueif 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
trueif 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
trueif 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
trueif 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
trueif 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)