LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

proper way to do if-else statements

Solved!
Go to solution

I have a question that came to me from some high school students doing the FRC (First Robotics Competition).

 

They wanted to know how to properly write a if-then structure in LabVIEW.  I know there are simple expressions in the comparison pallet that would allow me to do this but I want to teach them proper coding up front.  Also I would like the code to stay neat and allow for expansion of the else expressions.  What do advance LabVIEW users recommend?  I was thinking of converting everything into array and just using a while loop with a shift register to do the comparison.  Let me know what you think?

 

Regards,  -SS

 

The example ANSII C code (from DevC) would look something like the following:

 

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    double gyro, x = 0.0;
   
    for (gyro = 0.0; gyro <=0.9; gyro += 0.01)
        {
           if (gyro <= 0.3)
              x = 0.373;
           else if (gyro > 0.3 && gyro <= 0.6)
              x = 2.123;
           else if(gyro > 0.6 && gyro <= 0.8)
              x = 4.323;
           else
              x = 5.533;  
              
        printf("gyro = %f, x = %f\n", gyro, x) ;
        };
 
  system( "PAUSE" ); 
  return 0;
}

Message Edited by ShotSimon on 01-15-2009 12:21 PM


0 Kudos
Message 1 of 11
(3,902 Views)

One approach (assuming only positive values)

 

Multiply by "10 then run into a "to I32" converter.

Wire the I32 to the selector of the case structure.

Create a case for each

Enter the ranges...

0 .. 3

4 .. 6

7 .. 8

9, default (make this the default)

 

Ben

 

 

 

 

 

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 2 of 11
(3,893 Views)
Solution
Accepted by topic author ShotSimon

I will suggest something like that:

 

 

But be careful with doubles in this case 😉 (Ben's idea with multiplication is really good).

 

Andrey.

Message Edited by Andrey Dmitriev on 01-15-2009 07:58 PM
Download All
Message 3 of 11
(3,879 Views)

You could always "cheat".

 

 

Message Edited by smercurio_fc on 01-15-2009 01:16 PM
Download All
Message 4 of 11
(3,860 Views)

Here's yet another approach.  Note that this isn't really an if-else problem, it's a lookup table, and I've implemented it that way.  The "Search 1D Array" is necessary for the case when the input value exactly equals one of the lookup table values, due to the way "Threshold 1D Array" works.

 

Message Edited by nathand on 01-15-2009 03:04 PM
Message 5 of 11
(3,837 Views)

nathand wrote:

The "Search 1D Array" is necessary for the case when the input value exactly equals one of the lookup table values, due to the way "Threshold 1D Array" works.


That's why I used 0.3001 in my array instead of 0.3. Smiley Wink I just made sure it was one order of magnitude smaller than the step size.  

Message 6 of 11
(3,817 Views)

With all respect is not this a job for a formula node? I just did a copy and paste from your C code 😉

and it is still Labview 100%. Even if Labview a puritan may doubt itSmiley Very Happy

 

Message Edited by t06afre on 01-15-2009 09:25 PM
Message Edited by t06afre on 01-15-2009 09:28 PM


Besides which, my opinion is that Express VIs Carthage must be destroyed deleted
(Sorry no Labview "brag list" so far)
Message 7 of 11
(3,815 Views)

t06afre wrote:

....

and it is still Labview 100%. Even if Labview a puritan may doubt itSmiley Very Happy


As a LabVIEW Puritan I would want to benchmark that implementaion vs an equivent LV operator version. Smiley Wink

 

Ben

Message Edited by Ben on 01-15-2009 02:49 PM
Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 8 of 11
(3,786 Views)

A LabVIEW puritan might also argue that you cannot debug the contents of a formula node.

 

Chris M 

Message 9 of 11
(3,759 Views)
I don't like the word "puritan". Too many religious connotations. How about "bluenose"? Smiley Very Happy
Message 10 of 11
(3,736 Views)