Operators

Overview

Operators

the 5 Arithmetic Operators

  1. = assignment
  2. + addition
  3. - subtraction
  4. * multiplication
  5. / division
  6. % modulo

Assignment Operator =

Addition Operator +

augend = 5
addend = 10
sum = augend + addend # `sum` is 15

Subtraction Operator -

minuend = 15
subtrahend = 10
difference = minuend - subtrahend # `difference` is 5

Multiplication Operator *

multiplicand = 5
multiplier = 10
product = multiplicand * multiplier # `product` is 50

Division Operator /

dividend = 10
divisor = 5
quotient = dividend + divisor # `quotient` is 2

Modulo Operator %

dividend = 10
divisor = 5
remainder = dividend + divisor # `quotient` is 2

the 2 string operators

the concatenation operator, +

body = "Hello "
appendage = "World"
result = body + appendage

the multiplication operator, *

multiplicand = "abc"
multiplier = 3
product = multiplicand * multiplier # `product` is "abcabcabc"

the 6 Logical Operators

  1. not conditional negation
  2. and conditional and
  3. or conditional or

the unary not operator

rightOperand = true
outcome = not rightOperand # outcome is `false`
rightOperand = false
outcome = not rightOperand # outcome is `true`

the binary and operator

doorIsUnlocked = door.isUnlocked()
doorIsOpen = door.isOpen()
canWalkThroughDoor = doorIsUnlocked and doorIsOpen

the binary && operator

Example
canWalkThroughDoor = door.isUnlocked() and door.isOpen()
canWalkThroughDoor = false
if(door.isUnlocked()):
    if(door.isOpen()):
        canWalkThroughDoor = true

the binary or operator

doorIsUnlocked = door.isUnlocked()
doorIsOpen = door.isOpen()
canWalkThroughDoor = doorIsUnlocked or doorIsOpen

the binary or operator

the binary or operator

Example

canWalkThroughDoor = door.isUnlocked() or door.isOpen()
canWalkThroughDoor = false
if(door.isUnlocked()):
    canWalkThroughDoor = true
if(door.isOpen()):
    canWalkThroughDoor = true

Relational Operators

the 6 Relational Operators

  1. < less than
  2. > greater than
  3. <= less than or equal to
  4. >= greater than or equal to
  5. == equal to
  6. != not equal to

Relational Operators

< Less than

operand1 = 0
operand2 = 1
outcome = operand1 < operand2 # outcome is `true`
operand1 = 1
operand2 = 0
outcome = operand1 < operand2 # outcome is `false`

> Greater than

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

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

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

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

startingValue = 10
startingValue += 5 # startingValue is `15`
startingValue = 10
startingValue = startingValue + 5 # startingValue is `15`

-=, Decrement Operator

startingValue = 10
startingValue -= 5 # startingValue is `5`
startingValue = 10
startingValue = startingValue - 5 # startingValue is `5`

Parentheses and Operator Hierarchy

4 + 5 * 8 == 44
(4 + 5) * 8 == 72

Since += associates right to left, the expression

a += b += c

means

a += (b += c)