true
and false
). We have seen in Datatypes 1 that boolean literals can only be of two kinds : true
and false
. There are 3 major operations concerning boolean datatype :true
to false
and vice versa. The action associated with this operation is the not
function. It accepts a single boolean argument and returns its opposite boolean value.
Example : {{$x := true}} {{$y := not $x}}
In this code snippet initially true
is stored in variable $x
. The not
function then performs a not operation in variable $x
(which contains true
) returning false
. Thus eventually false
is stored in $y.and
operation is another boolean operation involving two boolean values which results in true
only if both the operands ( values on which it operates) are true
and otherwise results in false
. The following table captures the working of the boolean and
operation.and
function for performing boolean and
operation. It accepts more than two arguments. The evaluated result follows the following logic: Consider three boolean values passed to the and
function. It finds the and
of first and second value. Then it finds the and
of the result from the first two values and the third value. Similar logic applies for more than 3 arguments passed to the and
function.
Example :
{{$x := true}} {{$y := true}} {{$z := false}}
{{$Result := and $x $y $z}}
In the above example, first the and
of variables $x
and $y
results in true
. Then the and
of the result and $z
produces false
. Hence, finally false
is stored in variable Result.or
operation similar to and
operation operates on two boolean literals. It results in false
when both the operands are false
and otherwise results in true
. The following table captures the working of the or
operation.or
function is used to perform or
operation. Similar to and
operator, if more than two values are passed to the or
function, if first evaluates the result of the first two operands. Then it performs or
operation on the result from first two operands and the third operand and so on.
Example :
{{$x := false}} {{$y := false}} {{$z := true}}
{{$Result := or $x $y $z}}
In the above example, first the or
of variable $x
and $y
results in false
. Then the or
of the result and $z
produces true
. Hence, finally true
is stored in variable Result.eq
: This function checks for equality and returns true
if arg1 == arg2
, that is if both of them are equal. It is worth nothing that for equality to hold both value as well as data type must be same.ne
: This function is the reverse of the equality function and returns true
if arg1 != arg2
, that is if both of them are unequal, the function returns true
.gt
: This function returns true
if arg1 > arg2
, that is if first argument is strictly greater than second argument.ge
: This function returns true
if arg1 >= arg2
, that is if first argument is greater than or equal to second argument.lt
: This function returns true
if arg1 < arg2
, that is if first argument is strictly less than second argument.le
: This function returns true
if arg1 <= arg2
, that is if first argument is less than or equal to second argument.eq
and ne
functions can additionally also compare boolean values.$a
contains a number that is more than 0
. Since this condition is satisfied gt returns true
. Since the condition is true, the block of code/statements following the if action is executed. In the above example, Number is more than 0
is printed as output by the bot.
It is important to note here that the else action along with the code to be executed if condition is false (can be referred to as the else block) is not compulsory. However, the {{end}}
statement is compulsory and marks the end of the if-else conditional template-structure.{{if (condition)}}
and before {{else}}
(or {{end}}
if there is no else action) consists of the a single block which can be called the if block. Similarly the statements following {{else}}
and before {{end}}
consists of the else block.{{if eq 1 1}}
{{$a := 1}}
{{end}}
{{$a}}
This code will generate an error because the variable $a was defined inside the if block and ceases to exist after the {{end}}
statement. It is very important to keep a track on a variable's scope while writing codes to avoid such errors.{{$a := 1}}{{$b:= 2}}
{{if eq $b 2}}
{{$a := 3}}{{$b = 5}}{{$a}},{{$b}}
{{end}}
{{$a}},{{$b}}
The above code will Output:
3,5
``
1,5
{{end}}
statement is necessary to mark the end of the branching action.
Example :You Passed with distinction
will be printed as output by bot.-
at use in the internal if-else if-else action. It is used to trim spaces to the left and right of the action to aid with formatting. It is discussed in more detail here.This message has an attachment
if triggering message has an attachment. Notice how in this example we are using .Message.Attachments which is not of boolean data type as the if statement's condition. This is possible because non boolean variables are automatically converted to boolean when used in a condition according to the following logic :
If the the data represents the empty value [zero value ( nil
, 0
, ""
or false
depending on data type) or empty slices/maps/channels ] of the associated data type, it is evaluated as false
and otherwise as true
. This makes determining if a certain value is nil or empty (or number is zero) very efficient.\true
or false
depending on their stored value and datatype.
Example :
{{if or .Message.Attachments .Message.Embeds}}
This is not a simple text message
{{end}}
The above example prints This is not a simple text message
if the triggering message consists any attachments or embeds.