match as alternative to if and unless (#11407)

* match as alternative to if and unless

* Update docs.md

* Update docs.md

* Update docs.md
pull/11426/head
cindRoberta 2021-09-07 00:38:39 -03:00 committed by GitHub
parent 905c292a81
commit f2f7abe2f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 0 deletions

View File

@ -1572,6 +1572,30 @@ s := match number {
} }
``` ```
A match statement can also to be used as an `if - else if - else` alternative:
```v
match true {
2 > 4 { println('if') }
3 == 4 { println('else if') }
2 == 2 { println('else if2') }
else { println('else') }
}
// 'else if2' should be printed
```
or as an `unless` alternative: [unless Ruby](https://www.tutorialspoint.com/ruby/ruby_if_else.htm)
```v
match false {
2 > 4 { println('if') }
3 == 4 { println('else if') }
2 == 2 { println('else if2') }
else { println('else') }
}
// 'if' should be printed
```
A match expression returns the value of the final expression from the matching branch. A match expression returns the value of the final expression from the matching branch.
```v ```v