0
|
19135 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
GROUPS:

# how can i use "else" in mathematica?

Posted 9 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.
4 Replies
Sort By:
Posted 9 years ago
 thank you Udo Krause. very good answer.
Posted 9 years ago
 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 9 years ago
 this is not a good way because in this code--- xx = 3; For[i = 1, i < 2, i++, If[xx == 4, qq = 54]; If[xx == 5, qq = 55]; If[xx == 3, qq = 57]; If[xx == 7, qq = 58, qq = 59] ] ---- you see third condition is correct but output is qq=59 because fourth condition change qq. in else you have for example 5 condition and 6-th condition is else and is very explicit.
Posted 9 years ago
 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 
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.