mirror of
https://github.com/erg-lang/erg.git
synced 2025-10-02 13:41:10 +00:00
52 lines
1.5 KiB
Markdown
52 lines
1.5 KiB
Markdown
# Built-in functions
|
|
|
|
[
|
|
](https://gezf7g7pd5.execute-api.ap-northeast-1.amazonaws.com/default/source_up_to_date?owner=erg-lang&repos=erg&ref=main&path=doc/EN/syntax/05_buildtin_funcs.md&commit_hash=21e8145e83fb54ed77e7631deeee8a7e39b028a3)
|
|
|
|
## if
|
|
|
|
`if` is a function that changes processing depending on a condition.
|
|
|
|
```erg
|
|
result: Option Int = if! Bool.sample!(), do:
|
|
log "True was chosen"
|
|
1
|
|
print! result # None (or 1)
|
|
```
|
|
|
|
`.sample!()` returns a random set of values. If the return value is true, `print! "True"` is executed.
|
|
You can also specify what to do if the condition is false; the second do block is called the else block.
|
|
|
|
```erg
|
|
result: Nat = if Bool.sample!():
|
|
do:
|
|
log "True was chosen"
|
|
1
|
|
do:
|
|
log "False was chosen"
|
|
0
|
|
print! result # 1 (or 0)
|
|
```
|
|
|
|
If the process is a single line, you can omit indentation.
|
|
|
|
```erg
|
|
result = if Bool.sample!():
|
|
do 1
|
|
do 0
|
|
```
|
|
|
|
## for
|
|
|
|
You can use `for` to write a repeating process.
|
|
|
|
```erg
|
|
match_s(ss: Iterator(Str), pat: Pattern): Option Str =
|
|
for ss, s ->
|
|
if pat.match(s).is_some():
|
|
break s
|
|
```
|
|
|
|
<p align='center'>
|
|
<a href='./04_function.md'>Previous</a> | <a href='./06_operator.md'>Next</a>
|
|
</p>
|