Message Boards Message Boards

0
|
21126 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:
GROUPS:

how can i use "else" in mathematica?

Posted 10 years ago

hi i have a conditional expression in matlab like -- for i=1:10; if i==5 print('five') else print('others')end; end --how can i define "else" expression in mathematica.

POSTED BY: esi fn
4 Replies

Mathematica does not name it else, see If please, the else is a distinct position in the If statement:

For[i = 1, i < 10, i++,
 If[i == 5, Print["Five"], Print["Others"]]
 ]

Others
Others
Others
Others
Five
Others
Others
Others
Others
POSTED BY: Udo Krause
Posted 10 years ago
POSTED BY: esi fn

this is not a good way because

... you took the wrong construct: if an If with else is specified, it works - what else do you expect? Possibly you want something like

 In[2]:= xx = 3;
    For[i = 1, i < 2, i++,
     qq = Which[xx == 4, 54, xx == 5, 55, xx == 3, 57, xx == 7, 58, True, 59]
    ]

 In[4]:= qq
 Out[4]= 57
POSTED BY: Udo Krause
Posted 10 years ago

thank you Udo Krause. very good answer.

POSTED BY: esi fn
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