如果...否则,如果...否则,在REBOL中 [英] If...else if...else in REBOL
问题描述
我注意到REBOL没有内置的if...elsif...else
语法,就像这样:
I've noticed that REBOL doesn't have a built in if...elsif...else
syntax, like this one:
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!"
]
我找到了一种解决方法,但是它非常冗长:
I have found a workaround, but it's extremely verbose:
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!"
]
]
]
在REBOL中是否有更简洁的方法来实现if...else if...else
链?
Is there a more concise way to implement an if...else if...else
chain in REBOL?
推荐答案
您要查找的结构是CASE.它需要一系列条件和代码块进行评估,仅在条件为真时才评估这些块,并在第一个条件为真后停止.
The construct you would be looking for would be CASE. It takes a series of conditions and code blocks to evaluate, evaluating the blocks only if the condition is true and stopping after the first true condition is met.
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!"
]
]
如您所见,获得默认值就像满足TRUE条件一样简单.
As you see, getting a default is as simple as tacking on a TRUE condition.
也:如果您愿意,可以运行所有情况,而不会因CASE/ALL而短路.这样可以防止案件在第一个真实条件下停止;它将按顺序运行它们,评估任何块的任何真实条件.
Also: if you wish, you can have all of the cases run and not short circuit with CASE/ALL. That prevents case from stopping at the first true condition; it will run them all in sequence, evaluating any blocks for any true conditions.
这篇关于如果...否则,如果...否则,在REBOL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!