boolean
type boolean
A data type with two possible values: true
and false
.
Boolean logic can be performed with the and
, or
and not
operators, for example:
>>> true and false
false
>>> true or false
true
>>> not true
false
>>> not false
true
Content copied to clipboard
In addition, booleans are used in ternary expressions:
>>> if (true) 1 else 2
1
>>> if (false) 1 else 2
2
Content copied to clipboard
The in
operator returns a boolean:
>>> 1 in list<integer>()
false
>>> 1 in [1, 2]
true
Content copied to clipboard
Booleans are used in if
- and if/else
-statements:
>>> if (false) { print("hello"); } else { print("goodbye"); }
goodbye
>>> if (false) { print("hello"); }
>>> if (true) { print("hello"); }
hello
>>>
Content copied to clipboard
Boolean expressions are the basis of zero-argument when
-statements (each expression to the left of a '->
' symbol has boolean type (and in this context else
is equivalent to true
)):
when {
x == 1 -> return 'One';
x >= 2 and x <= 7 -> return 'Several';
x == 11, x == 111 -> return 'Magic number';
some_value 1000 -> return 'Special case';
else -> return 'Unknown';
}
Content copied to clipboard
Boolean conditions are used in while loops:
while (x < 10) {
print(x);
x = x + 1;
}
Content copied to clipboard
Functions can have boolean
return type (as can queries and operations), and indeed many functions and properties in the Rell standard library have boolean
type.
function foo(x: integer): boolean {
return x >= 10;
}
Content copied to clipboard
Since
0.6.0