Jump to content

Case structure: Difference between revisions

From LabVIEW Wiki
m Best practice: Link to actual page free label
 
(44 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{TOCnestright}}
{{TOCright}}
==Introduction==
{{LabVIEW Palette Object Information|palette=Functions Palette/Programming/Structures{{!}}Structures palette|type=structure|icon=Functions Palette - Programming - Structures - Case Structure.png}}
A case structure is a LabVIEW primitive that dynamically selects which parts of code should execute.
[[File:Case Structure - Components.png|thumb|Components of a Case Structure]]
[[Image:CaseStructure.PNG]]
==General Items==
On compile time the compiler assures that at least one case will run when the code executes, if this cannot be determined the containing VI will be [[broken]].
To provide a case for values of the selector that is general a case can be labeled ''default''.
One case can be used to handle several selector values. To enter these provide a '','' between the values. A range of these values can be set by providing ''..'' between the values.


==Selection mechanism==
A '''Case Structure''' is a primitive [[Functions Palette/Programming/Structures|Structure]] that can have multiple [[Subdiagram|Subdiagrams]] (also known as "Cases"), one of which is selectively executed at runtime. A selector value determines which case is executed at runtime. The Structure autmatically adapts to the [[:Category:Data types|data type]] connected to the Case selector and must have exactly one Subdiagram for each possible value of the connected data type, or a ''Default'' case to handle undefined values.


The selection decision of the case structure can be done by several datatypes:
== Usage ==
*Boolean
=== Supported data types ===
*Integer
*Enum
*String
*Error Cluster


===Boolean===
The Case Structure automatically adapts to following data types:
The boolean case structure is the default state of the case structure. It has a case for the ''True'' and the ''False'' state.


[[Image:BooleanCase.PNG]]
* [[Boolean data type|Boolean]]
===Integer===
* [[Integer data type|Integer]]
The integer case structure uses the integer value of the selector data type to determine  which case is executed
* [[Enumerated type|Enum]]
* [[String data type|String]]
* [[Error Cluster data type|Error Cluster]]


[[Image:IntegerCase.PNG]]
Some features are only available for specific data types.
====Floats====
<br clear="all">
Non-integer numerics (floats) are rounded according IEEE 754-1985 where a non-integer is rounded to the nearest even integer.
==== Boolean Case Structure ====


====Tricks====
[[File:Case Structure - Boolean.png|thumb|Case Structure with Boolean value]]
* Ranges
An integer case structure accept ranges to limit the number of cases. These ranges can be closed (3..10) or open (3..) where every value above 2 is executed.


===Enum===
The Boolean Case Structure is the default Structure when placed from the [[Functions Palette]]. It has exactly two cases: ''True'' and ''False''.
The [[enum]] case structure uses the string interpretation of the value to label the cases.


[[Image:EnumCase.PNG]]
==== Integer Case Structure ====
====Tricks====
*To have a case for every enum value right click on the selector label of the case structure and select ''add case for every value''.
*Don't add a ''default'' case, if the enum datatype changes the code will break and you'll be triggered that a case should be added


===String===
[[File:Case Structure - Integer.png|thumb|Case Structure with Integer value]]
The string case structure uses the string value to select which case should execute.


[[Image:StringCase.PNG]]
The Integer Case Structure uses an [[Integer]] as selector value and can have as many cases as the specific Integer type supports.
====Tricks====
Non-integer values ([[Fixed-point]], [[Single Precision]], [[Double Precision]], [[Extended Precision]]) are [[Rounding|rounded]] according to [[wikipedia:IEEE 754-1985|IEEE 754-1985]].
*Selection is by default done ''case sensitive'', to make the selection ''case insensitive'' right click on the selector label and select ''Case insensitive match''
Complex numbers ([[Complex Single]], [[Complex Double]], [[Complex Extended]]) are not supported and will break the VI.
*A default case is obliged


===Error===
===== Range selector =====
The [[Error_Case_Structure|error case structure]] is like the boolean case structure but can be controlled by the ''error state'' of an [[error cluster]]. The border of the ''error case'' is red and the ''non-error case'' is green.


[[Image:ErrorCase.PNG]]
[[File:Case Structure - Integer - Range Selectors.png|thumb|Using range selectors on an Integer Case Structure]]
Integer cases can have ranges.


[[category:structures]]
{| class="wikitable"
! Type
! Selector
! Description
|-
| Range (open)
| <tt>3..</tt>
| Executes if the value is greater than or equal to <tt>3</tt>
|-
| Range (closed)
| <tt>3..10</tt>
| Executes if the value is greater than or equal to <tt>3</tt> and lower than or equal to <tt>10</tt>
|-
| List
| <tt>3, 5, 7</tt>
| Executes if the value is either <tt>3</tt>, <tt>5</tt> or <tt>7</tt>
|}
 
Ranges are automatically sorted in ascending order by LabVIEW.
 
===== Radix =====
 
[[File:Case Structure - Integer - Change Radix.png|thumb|Change Radix on an Integer Case Structure]]
The default representation for Integer values is Decimal. This representation can be changed to ''Decimal'', ''Hex'', ''Octal'' or ''Binary'' from the right-click menu option ''Radix''.
 
==== Enum Case Structure ====
 
[[File:Case Structure - Enum.png|thumb|Case Structure with Enum value]]
 
The [[Enum]] Case Structure uses the string interpretation of the value to label cases. Internally, however, LabVIEW uses the Integer representation to select cases. Renaming Enum values has no effect on the Case Structure.
 
===== Range selector =====
 
[[File:Case Structure - Enum - Range Selector.png|thumb|Use range selectors with Enum Case Structures]]
Enum Case Structures support range selectors similar to the [[#Integer Case Structure|Integer Case Structure]]:
 
{| class="wikitable"
! Type
! Selector
! Description
|-
| Range (open)
| <tt>"First"..</tt>
| Executes if the value is greater than or equal to <tt>"First"</tt>.
|-
| Range (closed)
| <tt>"First".."Third"</tt>
| Executes if the value is greater than or equal to <tt>"First"</tt> and lower than or equal to <tt>"Third"</tt>.
|-
| List
| <tt>"First", "Third"</tt>
| Executes if the value is <tt>"First"</tt> or <tt>"Third"</tt>.
|}
 
Values are automatically sorted by their Integer representation and not by name.
 
===== Add Case for Every Value =====
 
[[File:Case Structure - Enum - Add Case for Every Value.png|thumb|Add Case for Every Value]]
 
Because Enums have a finite amount of values, LabVIEW provides a simple way to add cases for every value via the right-click menu option ''Add Case for Every Value''. This automatically adds cases for all missing values.
 
==== String Case Structure ====
 
[[File:Case Structure - String.png|thumb|Case Structure with String value]]
 
The string Case Structure uses a case-sensitive string as selector value.
 
===== Case Insensitive Match =====
 
[[File:Case Structure - String - Case Insensitive Match.png|thumb|Enable Case Insensitive Match]]
 
By default the Case Structure does a case sensitive match. This behavior can be changed by using the '''Case Insensitive Match''' option from the right-click menu.
 
From [[LabVIEW 2015]] onward, the Case Structure shows "<tt>A=a</tt>" at the bottom left corner if Case Insensitive Match is enabled.
 
[[File:Case Structure - String - Case Insensitive Match - Indication.png|frameless|border]]
 
===== Range selector =====
 
[[File:Case Structure - String - Range Selectors.png|thumb|Using range selectors on string type Case Structures]]
String Case Structures support ranges similar to [[#Integer Case Structure|Integer Case Structures]], but with added complexity due to the nature of strings. Note that ranges are matched using the [[Wikipedia:ASCII|ASCII]] character set.
 
{| class="wikitable"
! Type
! Selector
! Description (standard)
! Description (Case Insensitive Match)
|-
| Exact match
| <tt>"a"</tt>
| Executes if the value is exactly <tt>"a"</tt>
| Executes if the value is either <tt>"a"</tt> or <tt>"A"</tt>
|-
| Range (open)
| <tt>"a"..</tt>
| Executes if the value is greater than or equal to <tt>"a"</tt>
| Executes if the value is greater than or equal to <tt>"A"</tt> (lower on the ASCII table)
|-
| Range (closed)
| <tt>"a".."z"</tt>
| Executes if the value is greater than or equal to <tt>"a"</tt> and lower than <tt>"z"</tt> (not including <tt>"z"</tt>)
| Executes if the value is greater than or equal to <tt>"A"</tt> and lower than <tt>"z"</tt> (not including <tt>"z"</tt>)
|-
| List
| <tt>"a", "o", "z"</tt>
| Executes if the value is either <tt>"a"</tt>, <tt>"o"</tt> or <tt>"z"</tt>
| Executes if the value is either <tt>"A"</tt>, <tt>"a"</tt>, <tt>"O"</tt>, <tt>"o"</tt>, <tt>"Z"</tt> or <tt>"z"</tt>
|}
 
Ranges are automatically sorted in ascending order by LabVIEW.
 
==== Error Case Structure ====
 
[[File:Case Structure - Error Cluster.png|thumb|Case Structure with Error Cluster]]
 
The [[Error Case Structure|error case structure]] is like the Boolean case structure but can be controlled by the <tt>error state</tt> of an [[Error cluster]]. The border of the <tt>error case</tt> is red and the <tt>non-error case</tt> is green.
 
===== Multiple error cases =====
 
[[File:Case Structure - Error Cluster - Multiple Error Cases.png|thumb|Multiple error cases]]
 
Since [[LabVIEW 2019]] it is possible to have multiple error cases for specific error codes. Previously this was only possible by unbundling the <tt>code</tt> element from the [[Error Cluster]] and connect it to an Integer Case Structure inside the error case. Error codes are handled like Integer values and therefore can specify range selectors:
 
{| class="wikitable"
! Type
! Selector
! Description
|-
| All Errors
| <tt>Error ..</tt>
| Executes for any error
|-
| Range (open)
| <tt>Error 3..</tt>
| Executes if the error code is greater than or equal to <tt>3</tt>
|-
| Range (closed)
| <tt>Error 3..10</tt>
| Executes if the error code is greater than or equal to <tt>3</tt> and lower than or equal to <tt>10</tt>
|-
| List
| <tt>Error 3, 5, 7</tt>
| Executes if the error code is either <tt>3</tt>, <tt>5</tt> or <tt>7</tt>
|}
 
A ''Default'' case is required when using error ranges.
 
=== Subdiagram Label ===
 
[[File:Case Structure - Show Subdiagram Label.png|thumb|Show Subdiagram Label]]
 
The Subdiagram Label adds a label at top of each Subdiagram. It is hidden by default and can be shown via the right-click menu option ''Visible Items >> Subdiagram Label''.
 
[[File:Case Structure - Subdiagrams with Subdiagram Label.png|frameless|border]]
 
While the position of the label is fixed, its height can be adjusted for each Subdiagram.
 
=== Keyboard shortcuts ===
 
* <tt>&lt;Shift-Enter&gt;</tt> '''creates''' a new case '''after''' the current case.
* <tt>&lt;Ctrl-Shift-Enter&gt;</tt> '''duplicates''' the current case.
 
== Best practice ==
 
* Use [[Lookup Table|Lookup Tables]] instead of Case Structures to return values from a constant non-consecutive list of items (i.e. error code => error message).
* If two cases perform the same operation, remove one case and use the [[#Range_selector|range selector]] instead.
* Use [[#Subdiagram_Label|Subdiagram labels]] instead of [[free label]]s to document Subdiagrams.
* Use [[Linked Input Tunnel|Linked Input Tunnels]] for data wires that '''must''' be wired through all cases (changed or unchanged).
* Use the [[Match Pattern function]] or [[Match Regular Expression function]] instead of range selectors for string Case Structures.
* Set output terminals to [[Use Default Value|'''Use Default Value''']] if only some cases need to return values.
* Do not connect data wires if they are not used inside the Case Structure. Those wires should be wired ''around'' the Structure.
* Do not add a ''Default'' case for Enum values if the Structure is supposed to handle all values at all times.
* Do not use Case Structures to disable code (i.e. by wiring a False constant), use the [[Functions Palette/Programming/Structures/Diagram Disable Structure|Diagram Disable Structure]] instead.
 
== Tips and tricks ==
 
* Wire an error cluster to a Case Structure to turn it into an [[#Error Case Structure|Error Case Structure]].
 
== History ==
 
{| class="wikitable"
! Version
! Change(s)
! Comment
|-
| [[File:LV2019.png|frameless|border|64x64px|LabVIEW 2019|link=LabVIEW 2019]]
| Added support for multiple <tt>error cases</tt> in [[#Error Case Structure|Error Case Structures]].
| This feature was requested on the [[LabVIEW Idea Exchange]] by ''Hueter'' on 19.01.2010 and later confirmed by ''Darren'' to be included in LabVIEW 2019 on 30.05.2019. [https://forums.ni.com/t5/LabVIEW-Idea-Exchange/Allow-Error-Case-Structure-to-Handle-Specific-Error-Numbers/idi-p/1056452]
|-
| [[File:LV2015.png|frameless|border|64x64px|LabVIEW 2015|link=LabVIEW 2015]]
| Added an indicator for the Case Structure if [[#Case Insensitive Match|Case Insensitive Match]] is enabled.
| This feature was requested on the [[LabVIEW Idea Exchange]] by ''Matthias_H'' on ‎08.08.2014 and later confirmed by ''Darren'' to be included in LabVIEW 2015 on 17.08.2015. [https://forums.ni.com/t5/LabVIEW-Idea-Exchange/Case-structure-with-case-sensitive-insensitive-indicator/idc-p/3165756]
|}
 
== See also ==
 
* [[CaseStructure class]]
* [[State Machine]]
 
== External links ==
 
* [http://www.ni.com/getting-started/labview-basics/execution-structures#Case%20Structures National Instruments: Execution Structures in LabVIEW]
 
[[Category:Case Structure]]
[[Category:Structures Palette]]

Latest revision as of 17:03, 6 May 2023

Object information
Owning palette(s) Structures palette
Type Structure
Requires Basic Development Environment
Icon
Components of a Case Structure

A Case Structure is a primitive Structure that can have multiple Subdiagrams (also known as "Cases"), one of which is selectively executed at runtime. A selector value determines which case is executed at runtime. The Structure autmatically adapts to the data type connected to the Case selector and must have exactly one Subdiagram for each possible value of the connected data type, or a Default case to handle undefined values.

Usage

Supported data types

The Case Structure automatically adapts to following data types:

Some features are only available for specific data types.

Boolean Case Structure

Case Structure with Boolean value

The Boolean Case Structure is the default Structure when placed from the Functions Palette. It has exactly two cases: True and False.

Integer Case Structure

Case Structure with Integer value

The Integer Case Structure uses an Integer as selector value and can have as many cases as the specific Integer type supports. Non-integer values (Fixed-point, Single Precision, Double Precision, Extended Precision) are rounded according to IEEE 754-1985. Complex numbers (Complex Single, Complex Double, Complex Extended) are not supported and will break the VI.

Range selector
Using range selectors on an Integer Case Structure

Integer cases can have ranges.

Type Selector Description
Range (open) 3.. Executes if the value is greater than or equal to 3
Range (closed) 3..10 Executes if the value is greater than or equal to 3 and lower than or equal to 10
List 3, 5, 7 Executes if the value is either 3, 5 or 7

Ranges are automatically sorted in ascending order by LabVIEW.

Radix
Change Radix on an Integer Case Structure

The default representation for Integer values is Decimal. This representation can be changed to Decimal, Hex, Octal or Binary from the right-click menu option Radix.

Enum Case Structure

Case Structure with Enum value

The Enum Case Structure uses the string interpretation of the value to label cases. Internally, however, LabVIEW uses the Integer representation to select cases. Renaming Enum values has no effect on the Case Structure.

Range selector
Use range selectors with Enum Case Structures

Enum Case Structures support range selectors similar to the Integer Case Structure:

Type Selector Description
Range (open) "First".. Executes if the value is greater than or equal to "First".
Range (closed) "First".."Third" Executes if the value is greater than or equal to "First" and lower than or equal to "Third".
List "First", "Third" Executes if the value is "First" or "Third".

Values are automatically sorted by their Integer representation and not by name.

Add Case for Every Value
Add Case for Every Value

Because Enums have a finite amount of values, LabVIEW provides a simple way to add cases for every value via the right-click menu option Add Case for Every Value. This automatically adds cases for all missing values.

String Case Structure

Case Structure with String value

The string Case Structure uses a case-sensitive string as selector value.

Case Insensitive Match
Enable Case Insensitive Match

By default the Case Structure does a case sensitive match. This behavior can be changed by using the Case Insensitive Match option from the right-click menu.

From LabVIEW 2015 onward, the Case Structure shows "A=a" at the bottom left corner if Case Insensitive Match is enabled.

Range selector
Using range selectors on string type Case Structures

String Case Structures support ranges similar to Integer Case Structures, but with added complexity due to the nature of strings. Note that ranges are matched using the ASCII character set.

Type Selector Description (standard) Description (Case Insensitive Match)
Exact match "a" Executes if the value is exactly "a" Executes if the value is either "a" or "A"
Range (open) "a".. Executes if the value is greater than or equal to "a" Executes if the value is greater than or equal to "A" (lower on the ASCII table)
Range (closed) "a".."z" Executes if the value is greater than or equal to "a" and lower than "z" (not including "z") Executes if the value is greater than or equal to "A" and lower than "z" (not including "z")
List "a", "o", "z" Executes if the value is either "a", "o" or "z" Executes if the value is either "A", "a", "O", "o", "Z" or "z"

Ranges are automatically sorted in ascending order by LabVIEW.

Error Case Structure

Case Structure with Error Cluster

The error case structure is like the Boolean case structure but can be controlled by the error state of an Error cluster. The border of the error case is red and the non-error case is green.

Multiple error cases
Multiple error cases

Since LabVIEW 2019 it is possible to have multiple error cases for specific error codes. Previously this was only possible by unbundling the code element from the Error Cluster and connect it to an Integer Case Structure inside the error case. Error codes are handled like Integer values and therefore can specify range selectors:

Type Selector Description
All Errors Error .. Executes for any error
Range (open) Error 3.. Executes if the error code is greater than or equal to 3
Range (closed) Error 3..10 Executes if the error code is greater than or equal to 3 and lower than or equal to 10
List Error 3, 5, 7 Executes if the error code is either 3, 5 or 7

A Default case is required when using error ranges.

Subdiagram Label

Show Subdiagram Label

The Subdiagram Label adds a label at top of each Subdiagram. It is hidden by default and can be shown via the right-click menu option Visible Items >> Subdiagram Label.

While the position of the label is fixed, its height can be adjusted for each Subdiagram.

Keyboard shortcuts

  • <Shift-Enter> creates a new case after the current case.
  • <Ctrl-Shift-Enter> duplicates the current case.

Best practice

  • Use Lookup Tables instead of Case Structures to return values from a constant non-consecutive list of items (i.e. error code => error message).
  • If two cases perform the same operation, remove one case and use the range selector instead.
  • Use Subdiagram labels instead of free labels to document Subdiagrams.
  • Use Linked Input Tunnels for data wires that must be wired through all cases (changed or unchanged).
  • Use the Match Pattern function or Match Regular Expression function instead of range selectors for string Case Structures.
  • Set output terminals to Use Default Value if only some cases need to return values.
  • Do not connect data wires if they are not used inside the Case Structure. Those wires should be wired around the Structure.
  • Do not add a Default case for Enum values if the Structure is supposed to handle all values at all times.
  • Do not use Case Structures to disable code (i.e. by wiring a False constant), use the Diagram Disable Structure instead.

Tips and tricks

History

Version Change(s) Comment
LabVIEW 2019 Added support for multiple error cases in Error Case Structures. This feature was requested on the LabVIEW Idea Exchange by Hueter on 19.01.2010 and later confirmed by Darren to be included in LabVIEW 2019 on 30.05.2019. [1]
LabVIEW 2015 Added an indicator for the Case Structure if Case Insensitive Match is enabled. This feature was requested on the LabVIEW Idea Exchange by Matthias_H on ‎08.08.2014 and later confirmed by Darren to be included in LabVIEW 2015 on 17.08.2015. [2]

See also

External links