How do I Implement a Multi-Conditional Statement Using a Formula Node?

Updated Jan 5, 2022

Reported In

Software

  • LabVIEW Base
  • LabVIEW Full
  • LabVIEW Professional

Issue Details

How do I implement a multi-conditional statement using a Formula Node, such as the following?

if (x > 0 && <= 10)  y = 1
else if (x > 10 && <= 20)  y = 2
else if (x > 20 && <= 30)  y = 3
else if (x > 30 && <= 40)  y = 4
else y = 0
 

Solution

The solution depends on the version of LabVIEW you are using. All versions of LabVIEW support the solution outlined in the Older Formula Node Syntax section. LabVIEW 6.x and greater adds the use of C syntax. Both solutions are outlined below. For additional help, please refer to the VI, Function, & How-To Help by pressing <Ctrl-Shift-?>.
 

C Syntax (LabVIEW 6.x and After)

if (x>0 && x<=10)
y=1;
else if (x > 10 && x<= 20)
y = 2;
else if (x > 20 && x<= 30)
y = 3;
else if (x > 30 && x<= 40)
y = 4;
else
y = 0; 
LabVIEW 6.x above.png

 

Older Formula Node Syntax (All versions of LabVIEW)

Use the following syntax to implement a multi-conditional statement:

variable = condition ? true case : false case;

For this example:

y = ((x > 0) && (x <= 10)) ? 1:((x > 10) && (x <= 20)) ? 2:((x > 20)
&& (x <= 30)) ? 3:((x > 30) && (x <= 40)) ? 4:0;
LabVIEW 6.x below.png