Switch 语句
switch 语句类似于 if-then-else-endif 语句,您可以通过该语句基于元素特性控制设计脚本程序流。switch 语句的语法如下所示:
switch (expression) case value1 ? 1 or more statements case value2 ? 1 or more statements ... default ? 1 or more statements endswitch
对 switch 表达式求值,接着将其与 case 关键字所关联的值进行比较。值(例如 value1、value2 等)必须为常数、常数表达式或数值范围。如果表达式与值相等,或者如果表达式在值范围内,则会执行 case 语句与下一个 case 语句之间的语句。如果表达式与任意 case 值都不匹配,将执行可选的缺省 case。下面是两个 switch 语句示例:
示例 1:
switch (level) case 20 ? color = "blue" thickness = 0.25 case 30-40 ? color = "red" thickness = 0.3 endswitch
示例 2:
switch (weight) case 0-5 ? thickness = 0.25 case 6-10 ? thickness = 0.5 default ? thickness = 0.75 endswitch