Operators

© 2017 Morgan C. Benton code4your.life

What is an operator?

Um, no, not this kind

 

In programming, an operator is something that takes one or more values or expressions and returns another value.

 

For example, in the expression:

5 + 7

The "+" (plus sign) is the operator.

© 2017 Morgan C. Benton code4your.life

Terminology

  • operand: expression that the operator operates on
  • unary operator: has only one operand
  • binary operator: has two operands
  • ternary operator: has three operands

© 2017 Morgan C. Benton code4your.life

Types of Operators

  • Arithmetic: for adding, subtracting, multiplying, etc.
  • Assignment: for assigning a value to a symbol
  • Comparison: for comparing two values
  • Logical: for reaching true or false determinations

© 2017 Morgan C. Benton code4your.life

Arithmetic Operators

© 2017 Morgan C. Benton code4your.life

Arithmetic Operators
in Javascript

// unary operators
+x           // unary plus; identity; converts value to numeric
+"3"   === 3 // true
+true  === 1 // true
+false === 0 // true
-x           // unary minus; negation; converts to numeric and inverts

// basic arithmetic
x + y  // addition
x - y  // subtraction
x * y  // multiplication
x / y  // division
x % y  // remainder/modulo; e.g. 5 % 2 == 1

// increment/decrement; very common but DO NOT USE
// Douglas Crockford considers them dangerous
x = 3; y = x++; // postfix; x == 4, y == 3
x = 3; y = ++x; // prefix;  x == 4, y == 4
x = 3; y = x--; // postfix; x == 2, y == 3
x = 3; y = --x; // prefix;  x == 2, y == 2

// experimental: use with CAUTION
x ** y // exponentiation; 2 ** 3 == 2 to the 3rd power

© 2017 Morgan C. Benton code4your.life

Arithmetic Operators
in Python

-x # unary minus; negation; inverts the value

# basic arithmetic
x + y  # addition
x - y  # subtraction
x * y  # multiplication
x / y  # division
x % y  # remainder/modulo; e.g. 5 % 2 == 1

x ** y # exponentiation; 2 ** 3 == 2 to the 3rd power
x // y # floor division; divides then rounds down to next
       # lowest integer; 5 // 4 == 1.0, -5 // 4 = -2.0

© 2017 Morgan C. Benton code4your.life

Arithmetic Operators
in R

+x # unary plus; only works on values with a
   # base type of numeric or logical; converts
   # logical to numeric
> x <- TRUE
> +x
[1] 1
> x <- c(TRUE, FALSE, TRUE)
> +x
[1] 1 0 1

-x # unary minus; negation; inverts the value
   # note: for logical values, converts to numeric
   # BEFORE inverting: -TRUE == -1, -TRUE != 0

# basic arithmetic
x + y  # addition
x - y  # subtraction
x * y  # multiplication
x / y  # division
x %% y # remainder/modulo; e.g. 5 %% 2 == 1

x ** y  # exponentiation; 2 ** 3 == 2 to the 3rd power
x ^ y   # exponentiation; 2 ^  3 == 2 to the 3rd power
x %/% y # integer division; divides then discards part
        # after decimal; 5 %/% 4 == 1

© 2017 Morgan C. Benton code4your.life

Arithmetic Operators
in Ruby

# unary operators defined ONLY for numeric types
+x # unary plus; identity; returns the value
-x # unary minus; negation; inverts the value

# basic arithmetic
x + y  # addition
x - y  # subtraction
x * y  # multiplication
x / y  # division
x % y  # remainder/modulo; e.g. 5 % 2 == 1
x ** y # exponentiation; 2 ** 3 == 2 to the 3rd power

© 2017 Morgan C. Benton code4your.life

Arithmetic Operators
in PHP

// unary operators
+$x           // unary plus; identity; converts value to numeric
+"3"   === 3  // true
+true  === 1  // true
+false === 0  // true
-$x           // unary minus; negation; converts to numeric and inverts
-true  === -1 // true

// basic arithmetic
$x + $y  // addition
$x - $y  // subtraction
$x * $y  // multiplication
$x / $y  // division
$x % $y  // remainder/modulo; e.g. 5 % 2 == 1

// increment/decrement
$x = 3; $y = $x++; // postfix; $x == 4, $y == 3
$x = 3; $y = ++$x; // prefix;  $x == 4, $y == 4
$x = 3; $y = $x--; // postfix; $x == 2, $y == 3
$x = 3; $y = --$x; // prefix;  $x == 2, $y == 2

$x ** $y // exponentiation; 2 ** 3 == 2 to the 3rd power

© 2017 Morgan C. Benton code4your.life

Arithmetic Operators
in Java

// Java operators ONLY work with numeric operands
int x = 5, y = 2;

// unary operators
+x           // unary plus; identity
-x           // unary minus; negation

// basic arithmetic
x + y  // addition
x - y  // subtraction
x * y  // multiplication
x / y  // division
x % y  // remainder/modulo; e.g. 5 % 2 == 1

// increment/decrement
x = 3; y = x++; // postfix; x == 4, y == 3
x = 3; y = ++x; // prefix;  x == 4, y == 4
x = 3; y = x--; // postfix; x == 2, y == 3
x = 3; y = --x; // prefix;  x == 2, y == 2

© 2017 Morgan C. Benton code4your.life

Arithmetic Operators
in Go

// Go operators ONLY work with numeric operands
x, y := 5, 2

// unary operators
+x  // unary plus; identity
-x  // unary minus; negation

// basic arithmetic
x + y  // addition
x - y  // subtraction
x * y  // multiplication
x / y  // division
x % y  // remainder/modulo; e.g. 5 % 2 == 1

// increment/decrement
x = 3; y = x++; // postfix; x == 4, y == 3
x = 3; y = ++x; // prefix;  x == 4, y == 4
x = 3; y = x--; // postfix; x == 2, y == 3
x = 3; y = --x; // prefix;  x == 2, y == 2

© 2017 Morgan C. Benton code4your.life

Assignment Operators

© 2017 Morgan C. Benton code4your.life

Assignment Operators
in Javascript

// primarily a single "=" sign
x = 3;

// shorthand for arithmetic assignments
x += 3; // same as x = x + 3;
x -= 3; // x = x - 3;
x *= 3; // x = x * 3;
x /= 3; // x = x / 3;
x %= 3; // x = x % 3;

// experimental; use with care
x **= 3; // x = x ** 3;

// NOTE: increment/decrement can (and should) be
// written as follows
x += 1; // equivalent to ++x (but NOT x++)
x -= 1; // equivalent to --x (but NOT x--)

// You should ALWAYS opt for x+=1 instead of ++x
// even though you'll see ++ frequently, it can
// cause bugs and lead to your software being 
// hacked

© 2017 Morgan C. Benton code4your.life

Assignment Operators
in Python

# primarily a single "=" sign
x = 3

# shorthand for arithmetic assignments
x += 3  # same as x = x + 3
x -= 3  # x = x - 3
x *= 3  # x = x * 3
x /= 3  # x = x / 3
x %= 3  # x = x % 3
x **= 3 # x = x ** 3
x //= 3 # x = x // 3

# NOTE: increment/decrement can (and should) be
# written as follows
x += 1 # equivalent to ++x (but NOT x++)
x -= 1 # equivalent to --x (but NOT x--)

# You should ALWAYS opt for x+=1 instead of ++x
# even though you'll see ++ frequently, it can
# cause bugs and lead to your software being 
# hacked

© 2017 Morgan C. Benton code4your.life

Assignment Operators
in R

# R uses different assignment operators than most
# every other language

# the following statements all have the same result,
# i.e. they assign the value 5 to the variable x
> x = 5
> x <- 5
> x <<- 5  # in practice, almost never used
> 5 -> x   # in practice, almost never used
> 5 ->> x  # in practice, almost never used

# this form is MOST common for statements on their own
> x <- 5

# "=" is most commonly use when assigning parameter values
# in a function call, and does 
> x <- median(y = 1:10)
> y
## Error: object 'y' not found

# if <- is used, modifies global scope
> x <- median(y <- 1:10)
> y
## [1]  1  2  3  4  5  6  7  8  9 10

© 2017 Morgan C. Benton code4your.life

Assignment Operators
in Ruby

# primarily a single "=" sign
x = 3

# shorthand for arithmetic assignments
x += 3  # same as x = x + 3
x -= 3  # x = x - 3
x *= 3  # x = x * 3
x /= 3  # x = x / 3
x %= 3  # x = x % 3
x **= 3 # x = x ** 3

# operators particular to ruby
x ||= 3 # assigns 3 to x only if x is nil or false
        # behaves like: x || x = 3
x &&= 3 # assigns 3 to x only if x is NOT nil or false
        # behaves like: x && x = 3

© 2017 Morgan C. Benton code4your.life

Assignment Operators
in PHP

// primarily a single "=" sign
$x = 3;

// shorthand for arithmetic assignments
$x += 3;  // same as $x = $x + 3;
$x -= 3;  // $x = $x - 3;
$x *= 3;  // $x = $x * 3;
$x /= 3;  // $x = $x / 3;
$x %= 3;  // $x = $x % 3;
$x **= 3; // $x = $x ** 3;

© 2017 Morgan C. Benton code4your.life

Assignment Operators
in Java

// primarily a single "=" sign
x = 3;

// shorthand for arithmetic assignments
x += 3; // same as x = x + 3;
x -= 3; // x = x - 3;
x *= 3; // x = x * 3;
x /= 3; // x = x / 3;
x %= 3; // x = x % 3;

© 2017 Morgan C. Benton code4your.life

Assignment Operators
in Go

// primarily a single "=" sign
x = 3

// Go is statically typed, so requires variable declaration, i.e.
var x int
x = 3

// := is a shorthand for declaration and assignment and is
// equivalent to the above code
// Note: this method gives the programmer less precise
// control over the actual data type of the variable
x := 3

// shorthand for arithmetic assignments
x += 3 // same as x = x + 3
x -= 3 // x = x - 3
x *= 3 // x = x * 3
x /= 3 // x = x / 3
x %= 3 // x = x % 3

© 2017 Morgan C. Benton code4your.life

Comparison Operators

© 2017 Morgan C. Benton code4your.life

Comparison Operators
in Javascript

// variables for use in the below examples
var x = 3;
var y = "3";
var z = 4;

// equality
x == y // x "equals" y; true
x != y // x "not equal to" y; false

// strict equality (checks value AND data type)
x === y // x has the same data type and value as y; false
x !== y // x has either a different value OR data type as y; true

// greater/less than
x < y  // false
x < z  // true
x <= y // true
x <= z // true
x > y  // false
x >= y // true

// there is no strict version of greater/less than
// programmers must be careful of data types

© 2017 Morgan C. Benton code4your.life

Comparison Operators
in Python

# variables for use in the below examples
x = 3
y = "3"
z = 4

# equality (includes type checking)
x == y # x "equals" y; false
x != y # x "not equal to" y; true

# greater/less than
x < y  # ERROR; variables are different types
x < z  # true
x <= y # ERROR
x <= z # true
x > y  # ERROR
x > z  # false
x >= y # ERROR
x >= z # false

© 2017 Morgan C. Benton code4your.life

Comparison Operators
in R

# variables for use in the below examples
> x = 3
> y = "3"

# equality (does NOT include type checking)
> x == y # x "equals" y
[1] TRUE
> x != y # x "not equal to" y
[1] FALSE

# there is NOT an equality operator that checks both
# value AND data type like === in JavaScript

# greater/less than
> x < y
[1] FALSE
> x <= y
[1] TRUE
> x > y
[1] FALSE
> x >= y
[1] TRUE

© 2017 Morgan C. Benton code4your.life

Comparison Operators
in Ruby

# variables for use in the below examples
x = 3
y = "3"
z = 3.0

# equality: pay careful attention to data type!
x == y    # false; x is numeric, y is string
x == z    # true; x and z are both numeric
x.eql?(y) # false
x.eql?(z) # false; x is integer, z is float
x != y    # true
x != z    # false

# greater/less than
x < z  # false
x <= z # true
x > z  # false
x >= z # true

# x === y means "does y belong to set x"
(1..5) === 3 # true
(1..5) === 6 # false

# combined comparison (aka "spaceship") operator
x <=> 3 # returns  0 if operands are equal
x <=> 2 # returns  1 if first operand > second
x <=> 4 # returns -1 if second operand > first 

© 2017 Morgan C. Benton code4your.life

Comparison Operators
in PHP

// variables for use in the below examples
$x = 3;
$y = "3";
$z = 4;

// equality
$x == $y // $x "equals" $y; true
$x != $y // $x "not equal to" $y; false
$x <> $y // same as $x != $y

// strict equality (checks value AND data type)
$x === $y // $x has the same data type and value as $y; false
$x !== $y // $x has either a different value OR data type as $y; true

// greater/less than
$x < $y  // false
$x < $z  // true
$x <= $y // true
$x <= $z // true
$x > $y  // false
$x >= $y // true

// combined comparison (aka "spaceship") operator; as of PHP 7.0
$x <=> 2 // returns  1 if first operand > second
$x <=> 3 // returns  0 if operands are equal
$x <=> 4 // returns -1 if second operand > first 

© 2017 Morgan C. Benton code4your.life

Comparison Operators
in Java

// variables for use in the below examples
int x = 3;
String y = "3";
int z = 4;

// equality (checks for type)
x == y // x "equals" y; false
x != y // x "not equal to" y; true

// greater/less than
x < y  // Error: incomparable types
x < z  // true
x <= y // Error
x <= z // true
x > y  // Error
x >= y // Error
x > z  // false
x >= z // false

© 2017 Morgan C. Benton code4your.life

Comparison Operators
in Go

// variables for use in the below examples
var x int = 3
var y rune = "3"
var z int = 4
var a float32 = 3.0

// equality (checks for type)
x == y // Error: incomparable types
x == z // false
x == a // Error: incomparable types (CAUTION!)
x != y // Error
x != z // true

// greater/less than
x < y  // Error: incomparable types
x < z  // true
x <= y // Error
x <= z // true
x > y  // Error
x >= y // Error
x > z  // false
x >= z // false

© 2017 Morgan C. Benton code4your.life

Logical Operators

© 2017 Morgan C. Benton code4your.life

Logical Operators
in Javascript

// AND
if (x && y) {
  // ...
} else {
  // ...
}

// OR
if (x || y) {
  // ...
} else {
  // ...
}

// NOT
if (!x) {
  // ...
} else {
  // ...
}

© 2017 Morgan C. Benton code4your.life

Logical Operators
in Python

# AND
if x and y:
  # ...
else:
  # ...

# OR
if x or y:
  # ...
else:
  # ...

# NOT
if not x:
  # ...
else:
  # ...

© 2017 Morgan C. Benton code4your.life

Logical Operators
in R

# Element-wise AND returns vector
# of comparisons between x and y
if (x & y) {
  # ...
} else {
  # ...
}

# Element-wise OR
if (x | y) {
  # ...
} else {
  # ...
}

# NOT
if (!x) {
  # ...
} else {
  # ...
}

© 2017 Morgan C. Benton code4your.life

# AND returns the result of AND
# of just first element in vectors
# x and y
if (x && y) {
  # ...
} else {
  # ...
}

# OR
if (x || y) {
  # ...
} else {
  # ...
}

# NOTE: using control structures
# (like if/else) is generally 
# discouraged in R in favor of 
# using built-in functions

Logical Operators
in Ruby

# AND
if x and y
  # ...
else
  # ...
end

# OR
if x or y
  # ...
else
  # ...
end

# NOT
if not x
  # ...
else
  # ...
end

© 2017 Morgan C. Benton code4your.life

# AND
if x && y
  # ...
else
  # ...
end

# OR
if x || y
  # ...
else
  # ...
end

# NOT
if !x
  # ...
else
  # ...
end

Logical Operators
in PHP

// AND
if ($x and $y) {
  // ...
} else {
  // ...
}

// OR
if ($x or $y) {
  // ...
} else {
  // ...
}

// XOR
if ($x xor $y) {
  // ...
} else {
  // ...
}

© 2017 Morgan C. Benton code4your.life

// AND
if ($x && $y) {
  // ...
} else {
  // ...
}

// OR
if ($x || $y) {
  // ...
} else {
  // ...
}

// NOT
if (!$x) {
  // ...
} else {
  // ...
}

Logical Operators
in Java

// AND
if (x && y) {
  // ...
} else {
  // ...
}

// OR
if (x || y) {
  // ...
} else {
  // ...
}

// NOT
if (!x) {
  // ...
} else {
  // ...
}

© 2017 Morgan C. Benton code4your.life

Logical Operators
in Go

// AND
if (x && y) {
  // ...
} else {
  // ...
}

// OR
if (x || y) {
  // ...
} else {
  // ...
}

// NOT
if (!x) {
  // ...
} else {
  // ...
}

© 2017 Morgan C. Benton code4your.life

Things to Watch Out For

  • Unexpected results based on types of operands
  • Improper use of increment/decrement operator

© 2017 Morgan C. Benton code4your.life

Summary

  • Operators are a fundamental part of programming
  • The four primary types of operators are arithmetic, assignment, comparison, and logical
  • There is a lot of similarity among different programming languages; you should be careful of subtle differences if switching between languages

© 2017 Morgan C. Benton code4your.life

Operators

By Morgan Benton

Operators

Introduction to the concept of operators in computer programming. Example code in a variety of languages is provided to demonstrate the major operator categories: arithmetic, comparison, logical, and assignment.

  • 1,416