R12.1 JA-2025July15
Control Logic Block: Switch-case
NI also supports the switch-case logic, which can serve as a simplified version of ‘if-then-else’, and offer a more concise expression, better performance, and a reduced likelihood of errors.
When you need to execute different code blocks based on multiple possible values of a single variable, switch-case logic can make the code simpler and more readable.
For the ‘switch-case’ statement:
- The variable type of the ‘switch-case’ can only be int, float, or string.
- It can contain multiple case branches, 0 or 1 default branch. If there is a default branch, it can only be placed at the end.
- ‘Case’ can only be followed by a constant, and its type must be consistent with the switch variable type. It cannot be a variable or an expression.
- All cases will be analyzed in turn to find the matching ones. If a case branch matches, the diagnosis logic in this branch will be executed. If no case branches match, the diagnosis logic in the default branch will be executed.