SELECT
SELECT [ CASE ] Expression
[ CASE Expression [ TO Expression #2 ] [ , ... ]
... ]
[ CASE Expression [ TO Expression #2 ] [ , ... ]
... ]
[ { CASE ELSE | DEFAULT }
... ]
END SELECT
依次选择表达式进行比较,并执行相应的表达式匹配的
CASE语句包含的代码。
如果没有匹配的
CASE语句,则执行
DEFAULT或者
CASE。
CASE语句的条件是一个单独值的列表或者用
TO关键字隔开的两个数值表示的范围。
Note
The
TO
keyword can also be used without a lower bound which makes the case like a
less than
range. For example.
CASE TO 0
will act as
CASE < 0
and since the cases are evaluated in succession
CASE TO -1
...
CASE TO 1
....
CASE ELSE
....
provides an effective range filter for -infinity ... -1 ... 1 ... infinity.
Examples
' 检查掷骰子的随机函数
' 所以重复随机函数1000次
' 统计掷出1, 2, 3, 4, 5或6的次数
DIM x AS Integer
DIM w AS Integer
DIM a AS Integer
DIM b AS Integer
DIM c AS Integer
DIM d AS Integer
DIM e AS Integer
DIM f AS Integer
FOR x = 1 TO 1000
w = Int(Rnd(6) + 1)
SELECT CASE w
CASE 1
a = a + 1
CASE 2
b = b + 1
CASE 3
c = c + 1
CASE 4
d = d + 1
CASE 5
e = e + 1
CASE 6
f = f + 1
CASE ELSE
PRINT "不可能!"
END SELECT
NEXT
PRINT a, b, c, d, e, f
Example using CASE LIKE
Select sFilename
CASE LIKE "*.{png,jpg,jpeg,bmp}"
RunForImages(sFilename)
CASE ELSE
RunForOtherFiles(sFilename)
End Select
参见