The Case Structure
分支结构

Case Selector Terminal
分支选择器端子
That's enough about loops for nowlet's move on to another powerful structure. A Case Structure is LabVIEW's method of executing conditional text, sort of like an "if-then-else" statement. You can find it in the Programming>>Structures subpalette of the Functions palette. The Case Structure, shown in Figure 6.26, has two or more subdiagrams, or cases; only one of them executes, depending on the value of the Boolean, numeric, or string value you wire to the selector terminal.
到现在为止讲述了大量关于循环的东西, 再让我们进入另一种强大的结构中. 分支结构(Case Structure)是LabVIEW中执行条件文本, 有几分类似于” if-then-else”的声明. 你能够从Functions 模块中的Programming>>Structures 子模板上找到该结构. 显示如图6.26, 分支结构有两个或多个的子程序框图, 或称之为分支(Case); 但只有一个分支会被执行, 依赖于你所连接到选择器端子上的布尔量, 数字, 或字符串等数值.
Figure 6.26. Three Case Structures with their subdiagrams cascaded beneath them
图6.26. 三个分支结构, 它们的子框图层叠显示

If a Boolean value is wired to the selector terminal, the structure has two cases: FALSE and TRUE.
如果是一个布尔量连接到选择器端子上, 这个结构将会有两种分支:FALSE或TRUE.
If a numeric or string data type is wired to the selector, the structure can have from one to almost unlimited cases. Initially only two cases are available, but you can easily add more. You can specify more than one value for a case, separated by commas, as shown in the previous diagram. In addition, you typically must select a "Default" case that will execute if the value wired to the selector terminal doesn't match any of the other cases; this is very handy when you can't think of every possible case but want to specify a "catch-all" case.
如果是一个数字或字符串数据类型被连接到了选择器上, 那么该结构将可以拥有从一个到几乎无限制个数的分支. 最初地, 仅有两个分支可用, 但是你可以很容易地添加更多. 你能够指定超过一个的数值给一个分支, 使用逗号将它们分开, 如在前面的图中所示的. 另外, 你必须代表性地选择一个”默认”的分支, 当连接到选择器上的数值无法匹配任何分支的时候, 将执行该分支; 当你不能考虑到每一种可能的情形或场合但又想指定一个包罗全部情形或场合的情况下, 这将成为便利的方案
A Case Structure must have cases that handle all possible values that might flow into the selector terminal, or the VI will be broken (not able to run). Having a "Default" case is an easy way to achieve this, but it is not the only way. Some data types, such as enums, have a small number of possible values. Also, frames for numerics can be configured to handle a range of values; for example, "3.." handles everything from 3 to the maximum integer value of the input data type and "2..2" handles everything in the range of 2 to 2.一个分支结构必须拥有可以处理流入到选择器端子的所有可能值的分支. 否则VI将会是断开的(无法运行). 拥有一个默认的分支将是达到这个目的一个简单的方法, 但这还不是唯一的方法. 一些数据类型, 如枚举(Enum), 拥有较少的可能值的数量. 或者, 数字的框架能够被配置成处理一定范围内的值; 例如, “3..”可处理从3到所输入的数据类型的最大整数的所有情形, 或者”2..2”可处理在2到2范围之间的所有情形
You can designate a case for all values in a range by using a .. notation (for example,12..9). You will also notice that LabVIEW will condense a comma delimited list of integers to this notation if there are three or more contiguous values in the list. The .. notation can be used for strings too, but the resulting logic is not very intuitive.你能够指派一个分支对应于一个范围内的所有值, 通过使用一个..符号(例如, 12..9). 你也将注意到LabVIEW精简了一个可界定整数列表的逗号而成为这个符号, 如果列表中有三个或更多的相邻的数值的话. 这里的..符号同样可以被用于字符串, 但是这种产生的逻辑却不是非常直观的
When you first place it on the panel, the Case Structure appears in its Boolean form; it assumes numeric values as soon as you wire a numeric data type to its selector terminal.
当你第一次放置Case Structure到面板, 它将表现为布尔量形态; 但当你连接一个数字类型的数值到选择器上时它将立即转变成数字形态.
![]()
Decrement Arrow
递减箭头
![]()
Increment Arrow
递增箭头
Case Structures can have multiple subdiagrams, but you can only see one case at a time, sort of like a stacked deck of cards (unlike it appears in the previous illustration, where we cheated and took several pictures). Clicking on the decrement (left) or increment (right) arrow at the top of the structure displays the previous or next subdiagram, respectively. You can also click on the display at the top of the structure for a pull-down menu listing all cases, and then highlight the one you want to go to. Yet another way to switch cases is to pop up on the structure border and select Show Case>>.
分支结构能够有多个子程序框, 但在一个时间内你只能看到一个分支, 有点像重叠的纸牌(而不象前面图示的, 那里是使用几张图片重叠起来的, 不要被欺骗了). 点击在分支结构框图顶部的递减(左边的)或递增(右边的)箭头, 就可以显示个别的分支子程序框. 或者点击结构顶部的显示栏小方框, 一个下拉菜单将列出所有的分支, 然后就可以加亮显示那个你想显示的分支名称.另外还有一个方法就是在结构边界的弹出菜单中选择Show Case>>来转换当前显示的分支
If you wire a floating-point number to the selector, LabVIEW converts it to I32 and normal case selection is applied to the converted number.
如果你连接一个浮点数到选择器端子上, LabVIEW将会转换它成为I32类型并且常态分支选择被施加于该被转换的数值上.
You can position the selector terminal anywhere along the left border. You must always wire something to the selector terminal, and when you do, the selector automatically assumes that data type. If you change the data type wired to the selector from a numeric to a Boolean, cases 0 and 1 change to FALSE and TRUE. If other cases exist (2 through n), LabVIEW does not discard them, in case the change in data type is accidental. However, you must delete these extra cases before the structure can execute.
你可以任意的放置选择器端子在结构左边界的任意位置. 你必须连接某个东西到选择器端子上, 并且当你连接时, 选择器将自动地转换数据类型. 如果你改变连接到选择器端子上的数值从数字型到布尔量, 分支0和1将变成FALSE和TRUE. 如果其它的分支存在(2到n), LabVIEW并不会丢弃它们, 以防万一数据类型的改变是意外的. 无论如何, 你要结构能够运行就必须删除这些多余的分支
For string data types wired to case selectors, you should always specify the case values as strings between quotes (" "). If you enter a string value without quotes, LabVIEW will add the quotes for you.
对于字符串类型的数据被连接到选择器端子, 你应该指定分支的值为字符串并以引号(" ")加以引用. 如果你输入一段字符串值没有引号, LabVIEW将会为你加上引号.
When specifying a case as the default frame, never use quotes around the keyword Default. The use of quotes signifies a string whose value is "Default," not the default frame.当指定一个分支作为默认框架, 不要使用引号括住关键字Default. 这里使用引号将表示一个字符串的值为”Default”, 而不是默认的框架
For Case Structures that have a string type wired to their case selector terminal, you can select Case Insensitive Match from the pop-up menu to change the Case Structure from case-sensitive mode (the default mode) to case-insensitive mode.对于有字符串连接到分支选择器端子上的分支结构, 你可以选择从弹出菜单中的Case Insensitive Match选项改变分支结构从区分大小写模式(默认的模式)到不区分大小写模式
Wiring Inputs and Outputs
连接输入和输出
The data at all Case Structure input terminals (tunnels and selector terminal) is available to all cases. Cases are not required to use input data or to supply output data, but if any one case outputs a value to a tunnel, all must output a value (unless Use Default If Unwired, which we will discuss in a moment, is enabled from the pop-up menu of the output tunnel). When you wire an output from one case, a little white tunnel appears in the same location on all cases. The run arrow will be broken until you wire data to this output tunnel from every case, at which time the tunnel will turn black and the run arrow will be whole again (provided you have no other errors). Make sure you wire directly to the existing output tunnel, or you might accidentally create more tunnels.
在所有的分支结构中的输入端子上的数据(隧道和选择器端子)是对所有的分支有效的. 分支亦不是必须要求使用输入数据或供给输出数据的, 但是如果任何一个分支输出一个数值给一个隧道, 那就要求所有的分支都要输出数值(除非激活了Use Default If Unwired, 这将在后面讨论, 从输出隧道的弹出菜单激活该功能). 当你从一个分支连接一个输出, 一个白色的隧道将出现在所有的分支中的相同位置. 运行箭头将会是断开的, 直到你从每个分支中连接数据到该输出隧道, 在每次时这个隧道将会变成黑色并且运行箭头又重新变成完整的(倘若已经没有其它的错误). 确保你的连线直接接到了存在的隧道上, 否则将会以外地创建另外的隧道
Why must you always assign outputs for each case, you ask? Because the Case Structure must supply a value to the next node regardless of which case executes. LabVIEW forces you to select the value you want rather than selecting one for you, as a matter of good programming practice.
你可能要问为什么总是必须要为每个分支指派输出? 因为分支结构必须供给数值给下一个节点而不管哪一个分支被执行了, LabVIEW强制你选择你需要的该数据而不是选择其中的一个, 作为一个良好的编程习惯做法.
![]()
When an output tunnel is set to Use Default If Unwired from the pop-up menu, the tunnel will output default data if the executing frame is not wired to it. This can be a useful feature but be very careful that the default data will not cause you problems. Having LabVIEW force you to wire data to the terminal in every frame of the Case Structure forces you to decide what data flows out of the terminal.
当一个输出隧道从弹出菜单中被设置成Use Default If Unwired的时候, 如果执行的框架没有连接到这个隧道时, 该隧道将输出默任的数值. 这将是一个有用的特性但需要小心这个默认的数据不会给你造成问题. LabVIEW强制你在分支结构的每一个框架中连接数据到端子, 就要决定什么数据会流出该端子
Adding Cases
增加分支
If you pop up on the Case Structure border, the resulting menu gives you options to Add Case After and Add Case Before the current case (see Figure 6.27). You can also choose to copy the currently shown case by selecting Duplicate Case. You can delete the current case (and everything in it) by selecting Remove Case.
如果你从分支结构的边界上弹出菜单, 这个菜单给你选项可增加一个分支或在当前的分支之前(Add Case Before),或可以是在当前的分支之后(Add Case After)(见图6.27). 你也可以通过选择Duplicate Case选定复制当前显示的分支. 你也能够通过Remove Case来删除当前的分支(并包括里面的所有节点)
Figure 6.27. Adding a case to a Case Structure from its pop-up menu
图6.27. 从快捷菜单上给分支结构增加一个分支


If you would like to see some examples of how to use various structures in LabVIEW, select the Examples option from the structure's pop-up menu. This will take you to the help page, where there is a link to examples.
如果你愿意看一些关于如何在LabVIEW中使用不同的结构实例 , 可从结构的弹出式菜单中选择Examples选项. 这将带你进入帮助页面, 那儿有关于实例的链接.



