# Expressions & operators

Expressions can be used as input arguments in blocks. For example, use the Expression true block to check if an expression evaluate to true.

To write an expression, switch to “expression mode” by tapping the strike through “fx” icon within an input argument field, tap it again to return to “constant mode”.

## Expressions

An expression is any valid unit of code that resolves to a value. Automate has the following expression categories:

- Arithmetic: evaluates to a number, for example 3.14159. See arithmetic operators.
- Text: evaluates to text, for example, "Hello World" or "234". See special operators.
- Logical: evaluates to true or false. See logical operators.

## Operators

Automate has the following types of operators:

- Arithmetic operators
- Bitwise operators
- Comparison operators
- Logical operators
- Special operators

### Arithmetic operators

Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. Any non-numerical operand is first converted to a number.

#### Addition +

A binary operator; `1 + 2` returns 3.

#### Subtraction -

A binary operator; `2 - 1` returns 1.

#### Multiplication, *

A binary operator; `2 * 2` returns 4.

#### Division /

A binary operator; `10 / 3` returns 3.333. `10 / 0` returns Infinity.

#### Integer division //

A binary operator; `10 // 3` returns 3. `10 // 0` returns Infinity.

#### Modulus (remainder) %

A binary operator that returns the remainder of dividing the two operands; `12 % 5` returns 2.

#### Negate -

A unary prefix operator that returns the negation of its operand; `-(-5)` returns 5.

### Bitwise operators

Bitwise operators treat their operands as a set of 32 bits.

#### Bitwise AND &

A binary operator that returns a one in each bit position for which the corresponding bits of both operands are ones; `0b0101 & 0b0011` returns 0xb0001.

#### Bitwise OR |

A binary operator that returns a one in each bit position for which the corresponding bits of either or both operands are ones; `0b0101 | 0b0011` returns 0b0111.

#### Bitwise XOR ^

A binary operator that returns a one in each bit position for which the corresponding bits of either but not both operands are ones; `0b0101 ^ 0b0011` returns 0x0110.

#### Bitwise NOT ~

A unary operator that inverts the bits of its operand; `~0b1` returns 0xFFFFFFFE.

#### Left bit shift <<

A binary operator that shifts the bits to the left; `0b10010111 << 1` returns 0b00101110.

#### Right bit shift >>

A binary operator that shifts the bits to the right, keeping the sign of a negative number; `0b10010111 >> 1` returns 0b11001011.

#### Zero-fill right bit shift >>>

A binary operator that shifts the bits to the right, without keeping the sign of a negative number; `0b10010111 >>> 1` returns 0b01001011.

### Comparison operators

A comparison operator compares its operands and returns a logical value based on whether the comparison is true. The operands can be numbers or texts. If the value type of operands differ `0` is returned. Texts are compared on case-sensitive lexicographical order.

#### Equal =

A binary operator that returns 1 if the operands are equal; `3 = 3` returns 1.

#### Not equal !=

A binary operator that returns 1 if the operands are not equal; `3 != 3` returns 0.

#### Greater than >

A binary operator that returns 1 if the left operand is greater than the right operand; `2 > 2` returns 0.

#### Greater or equal >=

A binary operator that returns 1 if the left operand is greater than or equal to the right operand; `2 >= 2` returns 1.

#### Less than <

A binary operator that returns 1 if the left operand is less than the right operand; `2 < 2` returns 0.

#### Less or equals <=

A binary operator that returns 1 if the left operand is less than or equal to the right operand; `2 <= 2` returns 1.

### Logical operators

Automate does not have a value type nor literals for true or false, instead the following are considered false:

- A null value.
- The number zero.
- A NaN (Not-a-number).
- An empty, zero length text.
- An empty array.
- An empty dictionary.

All other values are considered true.

#### Logical AND &&

A binary operator that returns left operand if it evaluate to false, otherwise right operand is returned; `2 && 3` returns 3.

#### Logical OR ||

A binary operator that returns left operand if it evaluate to true, otherwise right operand is returned; `2 || 3` returns 2. `null || 0 || "Hi"` returns "Hi".

#### Logical NOT !

A unary prefix operator that returns 1 if the operand evaluate to false, otherwise 0 is returned; `!3` returns 0.

### Special operators

#### To number +

An unary prefix operator that returns the operand converted to a number; `+"2"` returns 2, `+"0xFF"` returns 255.

#### To text ++

An unary prefix operator that returns the operand converted to text; `++2` returns "2".

#### Text concatenation ++

A binary operator that returns the two operands concatenated into a text; `"Good"++"bye"` returns "Goodbye", `2++2` returns "22".

#### Conditional ? :

A ternary operator, operand ? operand : operand; If the first operand evaluate to true the seconds operand is returned, otherwise the third operand is returned; `2=2 ? "two" : "other"` returns "two".

#### Length #

An unary prefix operator;

- If the operand is a text, returns the number of characters; `#"Hi"` returns 2.
- If the operand is an array, returns the number of elements; `#[1,2]` returns 2.
- If the operand is a dictionary, returns the number of entries; `#{"a":1,"b":2}` returns 2.
- Otherwise zero is returned.

#### Subscript [ ]

A binary operator, operand[operand];

- If the first operand is a text, returns the Unicode character at the index of the seconds operand; `"Hi"[1]` returns 105.
- If the first operand is an array, returns the element at the index of the second operand; `["a","b","c"][1]` returns "b".
- If the first operand is a dictionary, returns the value mapped to the key of the second operand; `{"a":1,"b":2}["b"]` returns 2.
- Otherwise null is returned.

## Constant expression

A constant expression can only use null and literals for number, text, array and dictionary.