Operators

“Data is the new oil.” — Clive Humby

Control structures require arithmetic, logical or also boolean operators which test simple relationships between two or more variables. Depending on the test results, control structures can be used to decide how the program should go on. Common simple examples include arithmetic (e.g. addition), comparison (e.g. “greater than” with >), and logical operations (e.g. AND, also written && in some languages).

Operator Description Example
Arithmetic Operators    
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
^ or ** Exponentiation x^y
x %% y Modulus (x mod y) x %% y
Relational Operators    
< test if x is smaller than y x < y
> test if x is greater than y x > y
== test if x is exactly equal to y x == y
>= test if x is greater or equal than y x >= y
<= test if x is smaller or equal than y x <= y
!= test if x is not equal to y x != y
Logical Operators    
! logical NOT !x
& element-wise logical AND x & y
&& logical AND x && y
| element-wise logical OR y | y
|| logical OR x || y

Information

  • Modulo (mod) is a mathematical function that names the remainder of a division of two integers.

    Example: 10 %% 3 = 1 (i.e. "ten modulo three equals one")
    because 10 / 3 = 3, remainder 1 (3 * 3 + 1 = 10)

  • Relational operators are used to compare between values.

  • Logical operators are used to carry out Boolean operations like AND, OR etc. Such operators compare two variables and return true or false depending on the result of the comparison.

  • Operators & and | perform element-wise operation producing result having length of the longer operand.

    But && and || examines only the first element of the operands resulting into a single length logical vector.

Updated: