And*_*een 8 rebol switch-statement rebol3
我注意到REBOL没有内置if...elsif...else语法,就像这样:
theVar: 60
{This won't work}
if theVar > 60 [
print "Greater than 60!"
]
elsif theVar == 3 [
print "It's 3!"
]
elsif theVar < 3 [
print "It's less than 3!"
]
else [
print "It's something else!"
]
Run Code Online (Sandbox Code Playgroud)
我找到了一个解决方法,但它非常冗长:
theVar: 60
either theVar > 60 [
print "Greater than 60!"
][
either theVar == 3 [
print "It's 3!"
][
either theVar < 3 [
print "It's less than 3!"
][
print "It's something else!"
]
]
]
Run Code Online (Sandbox Code Playgroud)
有没有更简洁的方法if...else if...else在REBOL中实现链?
您要寻找的构造将是CASE.它需要一系列条件和代码块来评估,仅在条件为真时评估块,并在满足第一个真条件后停止.
theVar: 60
case [
theVar > 60 [
print "Greater than 60!"
]
theVar == 3 [
print "It's 3!"
]
theVar < 3 [
print "It's less than 3!"
]
true [
print "It's something else!"
]
]
Run Code Online (Sandbox Code Playgroud)
如您所见,获取默认值就像处理TRUE条件一样简单.
另外:如果您愿意,您可以使用CASE/ALL运行所有情况而不是短路.这可以防止案件在第一个真实条件下停止; 它将按顺序运行它们,为任何真实条件评估任何块.
小智 7
另一种选择是使用所有
all [
expression1
expression2
expression3
]
Run Code Online (Sandbox Code Playgroud)
只要每个表达式返回一个真值,它们就会继续被评估.
所以,
if all [ .. ][
... do this if all of the above evaluate to true.
... even if not all true, we got some work done :)
]
Run Code Online (Sandbox Code Playgroud)
我们也有
if any [
expression1
expression2
expression3
][ this evaluates if any of the expressions is true ]
Run Code Online (Sandbox Code Playgroud)