Group Abstract Group Abstract

Message Boards Message Boards

ELSEIF or ELIF in mathematica?

Posted 10 years ago
POSTED BY: Luis Ledesma
3 Replies
Posted 10 years ago
Attachments:
POSTED BY: Luis Ledesma

If can be nested. That makes it semantically equivalent to "elseif", but there is the syntactic burden of matching more closing right brackets.

POSTED BY: Daniel Lichtblau

Which is essentially synonymous with an elseif construct in other languages. This should probably be clarified in the documentation. If you want to add a final else statement, set the last condition to True, like this:

In[1]:= x = 5; Which[x == 1, 10, x == 2, 20, x == 3, 30, True, 100]

Out[1]= 100

If you want to test different patterns against a single value, Switch might be more elegant. See below:

x=2;Which[x==1,10,x==2,20,x==3,30]

is the same as

Switch[x,1,10,2,20,3,30]

but the latter looks better.

POSTED BY: Jesse Friedman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard