1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

match as alternative to if and unless (#11407)

* match as alternative to if and unless

* Update docs.md

* Update docs.md

* Update docs.md
This commit is contained in:
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

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.
```v