2019-11-08 03:19:27 +04:00
|
|
|
---
|
|
|
|
id: boolean-if-else
|
2020-02-05 19:28:40 +04:00
|
|
|
title: Booleans and Conditionals
|
2019-11-08 03:19:27 +04:00
|
|
|
---
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
## Booleans
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
The type of a boolean value is `bool`. Here is how to define a boolean
|
|
|
|
value:
|
2019-11-08 03:19:27 +04:00
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
2020-02-10 22:07:20 +04:00
|
|
|
<!--PascaLIGO-->
|
2019-12-26 02:38:37 +04:00
|
|
|
```pascaligo group=a
|
2020-02-05 19:28:40 +04:00
|
|
|
const a : bool = True // Notice the capital letter
|
|
|
|
const b : bool = False // Same.
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2019-12-12 17:35:07 +04:00
|
|
|
<!--CameLIGO-->
|
2019-12-26 02:38:37 +04:00
|
|
|
```cameligo group=a
|
2020-02-05 19:28:40 +04:00
|
|
|
let a : bool = true
|
|
|
|
let b : bool = false
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2019-12-11 13:34:08 +04:00
|
|
|
<!--ReasonLIGO-->
|
2019-12-26 02:38:37 +04:00
|
|
|
```reasonligo group=a
|
2020-02-05 19:28:40 +04:00
|
|
|
let a : bool = true;
|
|
|
|
let b : bool = false;
|
2019-12-10 17:47:31 +04:00
|
|
|
```
|
2019-11-08 03:19:27 +04:00
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
2020-02-10 22:07:20 +04:00
|
|
|
## Comparing Values
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
In LIGO, only values of the same type can be compared. Moreover, not
|
|
|
|
all values of the same type can be compared, only those with
|
|
|
|
*comparable types*, which is a concept lifted from
|
|
|
|
Michelson. Comparable types include, for instance, `int`, `nat`,
|
2020-02-06 14:47:41 +04:00
|
|
|
`string`, `tez`, `timestamp`, `address`, etc. As an example of
|
|
|
|
non-comparable types: maps, sets or lists are not comparable: if you
|
|
|
|
wish to compare them, you will have to write your own comparison
|
|
|
|
function.
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
### Comparing Strings
|
2019-11-08 03:19:27 +04:00
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
2020-02-10 22:07:20 +04:00
|
|
|
<!--PascaLIGO-->
|
2019-12-26 02:38:37 +04:00
|
|
|
```pascaligo group=b
|
2020-02-05 19:28:40 +04:00
|
|
|
const a : string = "Alice"
|
|
|
|
const b : string = "Alice"
|
|
|
|
const c : bool = (a = b) // True
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2019-12-12 17:35:07 +04:00
|
|
|
<!--CameLIGO-->
|
2019-12-26 02:38:37 +04:00
|
|
|
```cameligo group=b
|
2020-02-05 19:28:40 +04:00
|
|
|
let a : string = "Alice"
|
|
|
|
let b : string = "Alice"
|
|
|
|
let c : bool = (a = b) // true
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2019-12-11 13:34:08 +04:00
|
|
|
<!--ReasonLIGO-->
|
2019-12-26 02:38:37 +04:00
|
|
|
```reasonligo group=b
|
2020-02-05 19:28:40 +04:00
|
|
|
let a : string = "Alice";
|
|
|
|
let b : string = "Alice";
|
|
|
|
let c : bool = (a == b); // true
|
2019-12-10 17:47:31 +04:00
|
|
|
```
|
2019-11-08 03:19:27 +04:00
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
|
|
|
### Comparing numbers
|
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
2020-02-10 22:07:20 +04:00
|
|
|
<!--PascaLIGO-->
|
2019-12-26 02:38:37 +04:00
|
|
|
```pascaligo group=c
|
2020-02-05 19:28:40 +04:00
|
|
|
const a : int = 5
|
|
|
|
const b : int = 4
|
|
|
|
const c : bool = (a = b)
|
|
|
|
const d : bool = (a > b)
|
|
|
|
const e : bool = (a < b)
|
|
|
|
const f : bool = (a <= b)
|
|
|
|
const g : bool = (a >= b)
|
|
|
|
const h : bool = (a =/= b)
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2019-12-12 17:35:07 +04:00
|
|
|
<!--CameLIGO-->
|
2019-12-26 02:38:37 +04:00
|
|
|
```cameligo group=c
|
2020-02-05 19:28:40 +04:00
|
|
|
let a : int = 5
|
|
|
|
let b : int = 4
|
|
|
|
let c : bool = (a = b)
|
|
|
|
let d : bool = (a > b)
|
|
|
|
let e : bool = (a < b)
|
|
|
|
let f : bool = (a <= b)
|
|
|
|
let g : bool = (a >= b)
|
|
|
|
let h : bool = (a <> b)
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2019-12-10 17:47:31 +04:00
|
|
|
|
2019-12-11 13:34:08 +04:00
|
|
|
<!--ReasonLIGO-->
|
2019-12-26 02:38:37 +04:00
|
|
|
```reasonligo group=c
|
2020-02-05 19:28:40 +04:00
|
|
|
let a : int = 5;
|
|
|
|
let b : int = 4;
|
|
|
|
let c : bool = (a == b);
|
|
|
|
let d : bool = (a > b);
|
|
|
|
let e : bool = (a < b);
|
|
|
|
let f : bool = (a <= b);
|
|
|
|
let g : bool = (a >= b);
|
|
|
|
let h : bool = (a != b);
|
2019-12-10 17:47:31 +04:00
|
|
|
```
|
2019-11-08 03:19:27 +04:00
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
|
|
|
### Comparing tez
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
> 💡 Comparing `tez` values is especially useful when dealing with an
|
|
|
|
> amount sent in a transaction.
|
2019-11-08 03:19:27 +04:00
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
2020-02-10 22:07:20 +04:00
|
|
|
<!--PascaLIGO-->
|
2019-12-26 02:38:37 +04:00
|
|
|
```pascaligo group=d
|
2020-02-05 19:28:40 +04:00
|
|
|
const a : tez = 5mutez
|
|
|
|
const b : tez = 10mutez
|
2020-02-06 14:47:41 +04:00
|
|
|
const c : bool = (a = b) // False
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2019-12-12 17:35:07 +04:00
|
|
|
<!--CameLIGO-->
|
2019-12-26 02:38:37 +04:00
|
|
|
```cameligo group=d
|
2020-02-05 19:28:40 +04:00
|
|
|
let a : tez = 5mutez
|
|
|
|
let b : tez = 10mutez
|
|
|
|
let c : bool = (a = b) // false
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2019-12-11 13:34:08 +04:00
|
|
|
<!--ReasonLIGO-->
|
2019-12-26 02:38:37 +04:00
|
|
|
```reasonligo group=d
|
2020-02-05 19:28:40 +04:00
|
|
|
let a : tez = 5mutez;
|
|
|
|
let b : tez = 10mutez;
|
|
|
|
let c : bool = (a == b); // false
|
2019-12-10 17:47:31 +04:00
|
|
|
```
|
2019-11-08 03:19:27 +04:00
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
## Conditionals
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-06 14:47:41 +04:00
|
|
|
Conditional logic enables forking the control flow depending on the
|
2020-02-05 19:28:40 +04:00
|
|
|
state.
|
2019-11-08 03:19:27 +04:00
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
2020-02-10 22:07:20 +04:00
|
|
|
<!--PascaLIGO-->
|
2019-12-26 02:38:37 +04:00
|
|
|
```pascaligo group=e
|
2020-02-05 19:28:40 +04:00
|
|
|
type magnitude is Small | Large // See variant types.
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
function compare (const n : nat) : magnitude is
|
|
|
|
if n < 10n then Small (Unit) else Large (Unit) // Unit is needed for now.
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
You can run the `compare` function defined above using the LIGO compiler
|
|
|
|
like this:
|
|
|
|
```shell
|
|
|
|
ligo run-function
|
|
|
|
gitlab-pages/docs/language-basics/boolean-if-else/cond.ligo compare 21n'
|
|
|
|
# Outputs: Large (Unit)
|
|
|
|
```
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-12 01:29:12 +04:00
|
|
|
When the branches of the conditional are not a single expression, as
|
|
|
|
above, we need a block:
|
|
|
|
|
|
|
|
```pascaligo skip
|
|
|
|
if x < y then
|
|
|
|
block {
|
|
|
|
const z : nat = x;
|
|
|
|
x := y; y := z
|
|
|
|
}
|
|
|
|
else skip;
|
|
|
|
```
|
|
|
|
|
|
|
|
As an exception to the rule, the blocks in a conditional branch do not
|
|
|
|
need to be introduced by the keywor `block`, so, we could have written
|
|
|
|
instead:
|
|
|
|
```pascaligo skip
|
|
|
|
if x < y then {
|
|
|
|
const z : nat = x;
|
|
|
|
x := y; y := z
|
|
|
|
}
|
|
|
|
else skip;
|
|
|
|
```
|
|
|
|
|
2019-12-12 17:35:07 +04:00
|
|
|
<!--CameLIGO-->
|
2019-12-26 02:38:37 +04:00
|
|
|
```cameligo group=e
|
2020-02-05 19:28:40 +04:00
|
|
|
type magnitude = Small | Large // See variant types.
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
let compare (n : nat) : magnitude =
|
|
|
|
if n < 10n then Small else Large
|
|
|
|
```
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
You can run the `compare` function defined above using the LIGO compiler
|
|
|
|
like this:
|
|
|
|
```shell
|
|
|
|
ligo run-function
|
|
|
|
gitlab-pages/docs/language-basics/boolean-if-else/cond.mligo compare 21n'
|
|
|
|
# Outputs: Large
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2020-02-05 19:28:40 +04:00
|
|
|
|
2019-12-11 13:34:08 +04:00
|
|
|
<!--ReasonLIGO-->
|
2019-12-26 02:38:37 +04:00
|
|
|
```reasonligo group=e
|
2020-02-05 19:28:40 +04:00
|
|
|
type magnitude = | Small | Large; // See variant types.
|
2019-12-10 17:47:31 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
let compare = (n : nat) : magnitude =>
|
|
|
|
if (n < 10n) { Small; } else { Large; };
|
2019-12-10 17:47:31 +04:00
|
|
|
```
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
You can run the `compare` function defined above using the LIGO compiler
|
|
|
|
like this:
|
|
|
|
```shell
|
|
|
|
ligo run-function
|
|
|
|
gitlab-pages/docs/language-basics/boolean-if-else/cond.religo compare 21n'
|
|
|
|
# Outputs: Large
|
|
|
|
```
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|