Message Boards Message Boards

ELSEIF or ELIF in mathematica?

Posted 10 years ago

Hello all, I am having confusion with the IF statement because I have seen that in other languages there is ELSEIF or ELIF, and when searching for something similar in Mathematica didn't find it, it is for this reason that I am turning to you to ask them if there is something similar to ELSEIF or ELIF or definitely should i use WHICH, I share with you some images of what I am talking about. By its aid, a thousand thanks,greetings

elifmaple

HERE IN MATLAB

elseifmatlab

POSTED BY: Luis Ledesma
3 Replies
Posted 10 years ago

Many thanks to Daniel Lichtblau and Jesse Friedman for their replies, the problem i have is that I do not know how to move from what is done in Modula-2 to Mathematica, i.e. i have problems to separate it into parts to do so using WHICH the other is made and verified. If someone can help me appreciate greatly.

Annex in a document the code that i have Modula-2 and also the translation attempt to Mathematica that i made. What causes me conflict is the ELSIF , because I do not know how to have fun to Which

Thanks in advance.

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

Group Abstract Group Abstract