mirror of
https://github.com/bearlanguageorg/bear.git
synced 2026-08-26 14:57:18 +00:00
Else if chains
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
// match.vr — else-if chains and match statements.
|
||||
// vr run examples/match.vr
|
||||
|
||||
// classify returns a label using an else-if chain.
|
||||
fn classify(n) {
|
||||
if n < 0 {
|
||||
return "negative"
|
||||
} else if n == 0 {
|
||||
return "zero"
|
||||
} else if n < 10 {
|
||||
return "small"
|
||||
} else {
|
||||
return "big"
|
||||
}
|
||||
}
|
||||
|
||||
// describe names a number with a match statement.
|
||||
fn describe(n) {
|
||||
match n {
|
||||
0 {
|
||||
println("nothing")
|
||||
}
|
||||
1 {
|
||||
println("lonely")
|
||||
}
|
||||
2 {
|
||||
println("a pair")
|
||||
}
|
||||
7 {
|
||||
println("lucky")
|
||||
}
|
||||
else {
|
||||
println("just " + n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// else-if chain
|
||||
println(classify(-5))
|
||||
println(classify(0))
|
||||
println(classify(7))
|
||||
println(classify(100))
|
||||
|
||||
// match with else
|
||||
describe(0)
|
||||
describe(1)
|
||||
describe(2)
|
||||
describe(7)
|
||||
describe(42)
|
||||
|
||||
// match on strings
|
||||
let day = "fri"
|
||||
match day {
|
||||
"sat" {
|
||||
println("weekend!")
|
||||
}
|
||||
"sun" {
|
||||
println("weekend!")
|
||||
}
|
||||
else {
|
||||
println("workday")
|
||||
}
|
||||
}
|
||||
|
||||
// match inside a loop (break/continue still work)
|
||||
let found = 0
|
||||
for i in 0..10 {
|
||||
match i {
|
||||
4 {
|
||||
found = i
|
||||
break
|
||||
}
|
||||
else {
|
||||
// keep looking
|
||||
}
|
||||
}
|
||||
}
|
||||
println("found " + found)
|
||||
assert found == 4
|
||||
}
|
||||
@@ -125,6 +125,66 @@ fn test_break_continue() {
|
||||
assert odds[2] == 5
|
||||
}
|
||||
|
||||
fn test_else_if() {
|
||||
let score = 85
|
||||
let grade = ""
|
||||
if score >= 90 {
|
||||
grade = "A"
|
||||
} else if score >= 80 {
|
||||
grade = "B"
|
||||
} else if score >= 70 {
|
||||
grade = "C"
|
||||
} else {
|
||||
grade = "F"
|
||||
}
|
||||
assert grade == "B"
|
||||
assert 10 > 5
|
||||
}
|
||||
|
||||
fn test_match() {
|
||||
let total = 0
|
||||
match 2 {
|
||||
1 {
|
||||
total = total + 1
|
||||
}
|
||||
2 {
|
||||
total = total + 10
|
||||
}
|
||||
else {
|
||||
total = total + 100
|
||||
}
|
||||
}
|
||||
assert total == 10 // matched the 2 arm
|
||||
|
||||
// match on strings
|
||||
let word = "cat"
|
||||
let sound = ""
|
||||
match word {
|
||||
"dog" {
|
||||
sound = "woof"
|
||||
}
|
||||
"cat" {
|
||||
sound = "meow"
|
||||
}
|
||||
else {
|
||||
sound = "?"
|
||||
}
|
||||
}
|
||||
assert sound == "meow"
|
||||
|
||||
// no else arm: nothing runs when nothing matches
|
||||
let hit = 0
|
||||
match 99 {
|
||||
1 {
|
||||
hit = 1
|
||||
}
|
||||
2 {
|
||||
hit = 2
|
||||
}
|
||||
}
|
||||
assert hit == 0
|
||||
}
|
||||
|
||||
fn test_failing() {
|
||||
// this one is meant to fail — shows up in `vr test` output
|
||||
assert 1 == 2
|
||||
|
||||
Reference in New Issue
Block a user