ligo/gitlab-pages/docs/language-basics/boolean-if-else.md

192 lines
3.6 KiB
Markdown
Raw Normal View History

---
id: boolean-if-else
title: Boolean, If, Else
---
## Boolean
The type of a Boolean is `bool` and the possible values are `True` and `False`.
Here's how to define a boolean:
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
2019-12-26 02:38:37 +04:00
```pascaligo group=a
const a: bool = True;
const b: bool = False;
```
<!--CameLIGO-->
2019-12-26 02:38:37 +04:00
```cameligo group=a
let a: bool = true
let b: bool = false
```
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=a
2019-12-10 17:47:31 +04:00
let a: bool = true;
let b: bool = false;
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Comparing two values
In LIGO, only values of the same type can be compared. We call these "comparable types." Comparable types include e.g. `int`, `nat`, `string`, `tez`, `timestamp`, `address`, ...
### Comparing strings
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
2019-12-26 02:38:37 +04:00
```pascaligo group=b
const a: string = "Alice";
const b: string = "Alice";
// True
const c: bool = (a = b);
```
<!--CameLIGO-->
2019-12-26 02:38:37 +04:00
```cameligo group=b
let a: string = "Alice"
let b: string = "Alice"
// true
let c: bool = (a = b)
```
2019-12-11 13:34:08 +04:00
<!--ReasonLIGO-->
2019-12-26 02:38:37 +04:00
```reasonligo group=b
2019-12-10 17:47:31 +04:00
let a: string = "Alice";
let b: string = "Alice";
2019-12-26 02:38:37 +04:00
(* true *)
2019-12-10 17:47:31 +04:00
let c: bool = (a == b);
```
<!--END_DOCUSAURUS_CODE_TABS-->
### Comparing numbers
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
2019-12-26 02:38:37 +04:00
```pascaligo group=c
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);
```
<!--CameLIGO-->
2019-12-26 02:38:37 +04:00
```cameligo group=c
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)
2019-12-26 02:38:37 +04:00
let h: bool = (a <> b)
```
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
2019-12-10 17:47:31 +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);
```
<!--END_DOCUSAURUS_CODE_TABS-->
### Comparing tez
> 💡 Comparing `tez` values is especially useful when dealing with an `amount` sent in a transaction.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
2019-12-26 02:38:37 +04:00
```pascaligo group=d
2019-11-09 18:40:53 +04:00
const a: tez = 5mutez;
const b: tez = 10mutez;
const c: bool = (a = b);
```
<!--CameLIGO-->
2019-12-26 02:38:37 +04:00
```cameligo group=d
2019-11-09 18:40:53 +04:00
let a: tez = 5mutez
let b: tez = 10mutez
// false
let c: bool = (a = b)
```
2019-12-11 13:34:08 +04:00
<!--ReasonLIGO-->
2019-12-26 02:38:37 +04:00
```reasonligo group=d
2019-12-10 17:47:31 +04:00
let a: tez = 5mutez;
let b: tez = 10mutez;
2019-12-26 02:38:37 +04:00
(* false *)
2019-12-10 17:47:31 +04:00
let c: bool = (a == b);
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Conditionals, if staments, and more
Conditional logic is an important part of every real world program.
### If/else statements
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
2019-12-26 02:38:37 +04:00
```pascaligo group=e
const min_age: nat = 16n;
function is_adult(const age: nat): bool is
2020-01-29 20:15:03 +04:00
if (age > min_age) then True else False
```
> You can run the function above with
> ```
> ligo run-function -s pascaligo src/if-else.ligo is_adult 21n
> ```
<!--CameLIGO-->
2019-12-26 02:38:37 +04:00
```cameligo group=e
let min_age: nat = 16n
(**
This function is really obnoxious, but it showcases
2020-01-22 19:16:08 +04:00
how the if statement and its syntax can be used.
Normally, you'd use `with (age > min_age)` instead.
*)
let is_adult (age: nat) : bool =
if (age > min_age) then true else false
```
2019-12-11 13:34:08 +04:00
<!--ReasonLIGO-->
2019-12-26 02:38:37 +04:00
```reasonligo group=e
2019-12-10 17:47:31 +04:00
let min_age: nat = 16n;
2019-12-26 02:38:37 +04:00
(**
2019-12-10 17:47:31 +04:00
This function is really obnoxious, but it showcases
2020-01-22 19:16:08 +04:00
how the if statement and its syntax can be used.
2019-12-10 17:47:31 +04:00
Normally, you'd use `with (age > min_age)` instead.
2019-12-26 02:38:37 +04:00
*)
2019-12-10 17:47:31 +04:00
let is_adult = (age: nat): bool =>
if (age > min_age) {
true;
} else {
false;
};
```
> You can run the function above with
> ```
2019-12-11 13:34:08 +04:00
> ligo run-function -s reasonligo src/if-else.religo is_adult 21n
> ```
<!--END_DOCUSAURUS_CODE_TABS-->