Multiple Nested IF Statements

I'm trying to create a series of if commands which will return a value based on some initial conditions.  I have 5 equations, x1, x2, x3, x4 and x5, which contain functions containing parameters m and n.

 

If the initial condition is "yes", then x1, x2 and x3 could be used, if no, then only x4 and x5 can be used. 

 

For the "yes" case, if m = 0, then only x2 can be used, and likewise, if n=0, only x3 applies.  Should m > 0, n > 0, then x1 applies.

For the "no" case, if m = 0, no equation applies, for n=0, x5 is to be used.  If m>0, n>0, x4 should be used.

 

I have tried using the elif command within an if statement with no success.  Any help would be appreciated.

 

alec's picture

More needed

It is not clear from your post what the problem may be. It seems as if it should work pretty straightforward with ifs and elifs. If you posted your code instead of describing it, the problem would be seen more clearly.

Alec

Code

if mode=TM
then Q:=QTM1
elif n=0
then Q:=QTM2
end if:

if mode=TE
then Q:=QTE1
elif m=0
then Q:=QTE2
elif n=0
then Q:=QTE3
end if:

 

This is the basic idea of what I'm looking for. 

 

However, for mode=TE, and m=0, n>0, I receive Q=QTE1, when I should have Q=QTE2.

alec's picture

Order of statements

The statements are executed in the order they are written. In your code, if mode=TE then Q:=QTE1, so it is executed as it should. If you wrote

if mode=TE then if m=0 then Q:=QTE2 elif n=0 then QTE3 else Q:=QT1 fi fi

then for m=0 QTE2 would work, for m not equal 0 and n=0 QTE3 would work, and QT1 would work in other cases.

Alec

Doug Meade's picture

Nested "if" statements

I think I see the problem. You really need to have nested if statements. Try this:

if mode=TM then
  if n=0 then Q:=QTM2
         else Q:=QTM1
  end if;
elif mode=TE then
  if   m=0 then Q:=QTE2
  elif n=0 then Q:=QTE3
           else Q:=QTE1
  end if;
end if

There are many other ways to do this, including some that might be slightly more efficient. But, this is the general idea.

I hope this helps,

Doug

---------------------------------------------------------------------
Douglas B. Meade  <><
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu       
Phone:  (803) 777-6183         URL:    http://www.math.sc.ed

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}