From f2f7abe2f4781f7f9e3f4847bf75c24b272abfad Mon Sep 17 00:00:00 2001 From: cindRoberta <32280512+cindRoberta@users.noreply.github.com> Date: Tue, 7 Sep 2021 00:38:39 -0300 Subject: [PATCH] match as alternative to if and unless (#11407) * match as alternative to if and unless * Update docs.md * Update docs.md * Update docs.md --- doc/docs.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/doc/docs.md b/doc/docs.md index f4de4958dc..78c27b7e04 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -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