switch — 开始多路分支块。
switch( : : ControlExpression : )
switch 语句启动一个代码块,通过多路分支控制程序流程。参数 ControlExpression 必须返回整数值,该值决定执行跳转至哪个 case 标签。每个 case 语句包含一个整数常量。当 case 语句的整数常量等于 ControlExpression 参数的计算结果时,程序执行将在此处继续。此外,可在 switch 块末尾定义可选的 default 语句作为跳转标签。若所有 case 常量均与 ControlExpression 计算值不匹配,程序执行将跳转至该 default 标签。
与 C、C++ 和 C# 等编程语言类似,case
语句是一个跳转标签——不同于 elseif 语句——它并非封闭代码块的开头,该代码块不会在遇到下一个
case 或 default 语句时自动退出。
在 C 或 C++ 中,为使程序在执行完某个 case 分支的代码行后离开 switch 块,必须在该 case 分支末尾插入 break 语句。break 语句可在
switch 块内的任意位置使用,这将导致程序执行在闭合的
endswitch 语句之后继续进行。若分支末尾未放置 break 语句,程序执行将“穿透”至后续
case 分支或 default 分支的语句。
若需在不同情况(即针对多个控制值)下执行相同的语句,可将多个包含不同常量表达式的 case 语句依次排列。
ControlExpression (输入控制) integer → (integer)
整数表达式,用于确定程序执行应继续至哪个情况标签。
TestStr := ''
for Index := 1 to 8 by 1
TestStr := TestStr + '<'
switch (Index)
case 1:
TestStr := TestStr + '1'
break
case 2:
TestStr := TestStr + '2'
* intentionally fall through to 3
case 3:
TestStr := TestStr + '3'
* intentionally fall through to 4
case 4:
TestStr := TestStr + '4'
break
case 5:
case 6:
* common case branch for 5 and 5
TestStr := TestStr + '56'
break
case 7:
* continue for loop
TestStr := TestStr + '7'
continue
default:
TestStr := TestStr + 'd'
break
endswitch
TestStr := TestStr + '>'
endfor
如果条件正确,
switch(作为算子)返回 2 ( H_MSG_TRUE )。否则,将抛出异常并返回错误代码。
基础