LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Simple elseif statement

I want to write a simple if-else statement in the Formula node in Labview.

I want to write

if (condition)
execute
else if (condition)
execute
else
execute
end
end

but the "else if" generates error in statement. Is this way of writing not allowed? Can I do it in another way?
I want to avoid using a case structure since I already have 5 cases in one case structure and this if statement is just going to be in one of these.
/grogg
0 Kudos
Message 1 of 13
(4,565 Views)

Here is the syntax that I found in the help on if statements. Try looking at the help. Sorry i could not help more

 

if-statement:

if ( assignment ) statement

if-else-statement:

if-statement else statement




Joe.
"NOTHING IS EVER EASY"
0 Kudos
Message 2 of 13
(4,561 Views)
I thought that code in those nodes was based on C...  If my memory serves me correctly, an else if statement is C is "elsif" not "else if"
0 Kudos
Message 3 of 13
(4,553 Views)
I have look in the help. I'm sorry but it makes no sense writing
if statement else statement
did they forget to write (assignment). I thought it would be
if (assignment) else (assignment) statement
 
The help file does not say anything about the syntax for else if but I didn't think it would be a problem. Wrinting one if-statement inside another shouldn't be any diffrent then writing a while loop inside another while loop.
0 Kudos
Message 4 of 13
(4,550 Views)


@Novatron wrote:
I thought that code in those nodes was based on C...  If my memory serves me correctly, an else if statement is C is "elsif" not "else if"



I also thought it was base on C. I have tried elseiff too.
0 Kudos
Message 5 of 13
(4,546 Views)
Oops, I think I lied...  I was thinking of Perl...  (too many languages!!)
0 Kudos
Message 6 of 13
(4,541 Views)
Forgive me for sounding arrogant or foolish, but don't we use Labview to get away from text coding?  Why even bother with if-else statements in a formula node?  Why not do the whole construct in Labview graphically?  I have never used a formula node and I don't expect to ever use one.  All formulas can be created with Labview numerical functions.  I guess I could see constructing a complex formula of math functions (X=2a^2+4b+c-d) using a formula node, but why would anyone want to use a formula node to perform text based coding like if-else and such?  Use Labview graphics and your troubles are over.
- tbob

Inventor of the WORM Global
0 Kudos
Message 7 of 13
(4,524 Views)

@tbob wrote:
Forgive me for sounding arrogant or foolish, but don't we use Labview to get away from text coding?  Why even bother with if-else statements in a formula node?  Why not do the whole construct in Labview graphically?  I have never used a formula node and I don't expect to ever use one.  All formulas can be created with Labview numerical functions.  I guess I could see constructing a complex formula of math functions (X=2a^2+4b+c-d) using a formula node, but why would anyone want to use a formula node to perform text based coding like if-else and such?  Use Labview graphics and your troubles are over.


That answer was as helpful as when people ask for help with windows and the reply is use Linux. Sorry if I sounded arrogant.
0 Kudos
Message 8 of 13
(4,520 Views)
The answer is very simple. The code should be:
 
if (condition) {
    statement
    }else{ if (condition){
                  statement
               }else{
                        statement
               }
 
    }
Message 9 of 13
(4,509 Views)

I was just wondering why anyone would use a formula node for if-else.  But if that is your preference, so be it.  I have tried this bit of code in a formula node and it works fine:  (a is the input, x is the output)

if(a==0)
  {
     x=1;
     y=11;
  }
else if(a==1)
  {
      x=2;
      y=12;
  }
else if(a==2)
  {
      x=3;
      y=13;
  }
else
  {
      x=-1;
      y=-1;
  }

 

I also tried an exact copy of your construct:

if(a==0) {
     x=1;
     y=11;
     } else {if(a==1) {
                    x=2;
                    y=12;
                    } else {
                           x=-1;
                           y=-1;
                           }
                     }
 It worked fine.

- tbob

Inventor of the WORM Global
0 Kudos
Message 10 of 13
(4,503 Views)