00. 目录
01. 概述
switch - 开始多路分支代码段。
02. 签名
switch( : : ControlExpression : )
03. 描述
switch开始一个允许通过多路分支来控制程序流的分段。 参数ControlExpression结果必须为一个整数值。 这个值决定了跳转到哪个标记。 每个case语句都包含一个整型常量。 如果case语句的整数常量等于参数ControlExpression的计算值,则程序从这儿运行。 另外,一个可选的默认语句可以被定义为一个switch分段中的最后一个跳转标记。 如果没有case常量与计算的ControlExpression值相匹配,则程序跳转到此default标记。
和编程语言C,C ++和C#一样,case语句也是一个跳转标记,和elseif不同,不是封闭分段的开始,而是在下一个case或default语句前自动跳出。
为了在运行一个case分支的代码行之后离开switch分段,就像在C或C ++中一样,必须在case分支的末尾插入一个break语句。 break语句可以在switch块的任何地方使用。 程序在endswitch语句后继续执行。 在分支结束时没有break语句,程序执行“落在”下面的case或默认分支语句。
如果需要在不同的情况下运行相同的语句,即对于多个控制值,具有不同常量表达式的几个case语句可以并排列出。
原文描述:
switch starts a block that allows to control the program flow via a multiway branch. The parameter ControlExpression must result in an integer value. This value determines to what case label the execution jumps. Every case statement includes one integer constant. If the integer constant of a case statement is equal to the calculated value of the parameter ControlExpression, the program execution continues there. In addition, an optional default statement can be defined as the last jump label within a switch block. The program execution jumps to this default label, if no case constant matches the calculated ControlExpression value.
As in the programming languages C, C++, and C#, the case statement is a jump label and—in contrast to elseif—not the begin of an enclosed block that is automatically left at the next case or default statement.
In order to leave the switch block after the execution of the code lines of a case branch, as in C or C++ a break statement must be inserted at the end of the case branch. break statements can be used anywhere within a switch block. This causes the program execution to continue after the closing endswitch statement. Without a break statement at the end of a branch the program execution “falls through” to the statements of the following case or default branch.
If the same statements have to be executed in different cases, i.e., for multiple control values, several case statements with different constant expressions can be listed one below the other.
04. 注意
无
05. 参数
ControlExpression (input_control) integer → (integer)
决定了在哪个case标记继续运行程序的整数表达式。
06. 结果
如果条件正确,则switch(作为算子)返回2(H_MSG_TRUE)。 否则,会引发异常并返回错误代码。
HDevelop例程
switch_case.hdev Use switch/case statement for a multiway branch
程序示例
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
07. 附录
7.1 机器视觉博客汇总
网址:https://dengjin.blog.csdn.net/article/details/116837497