Re: If without else

*To*: mathgroup at smc.vnet.net*Subject*: [mg113032] Re: If without else*From*: Ingolf Dahl <Ingolf.Dahl at physics.gu.se>*Date*: Tue, 12 Oct 2010 04:22:31 -0400 (EDT)

Sebastian! You could (and should) omit the last comma in If[b > 2, x == 10,] One thing to think about when using If clauses, is what happens if the system cannot decide if the condition cannot be evaluated. Consider a case when b has not obtained any value yet. If we define a simple function f0[x_] :== x^2; then f0[b] will evaluate to b^2, and obtain the correct value once b is defined. Also f1[x_] :== If[x >== 0, x^2, -x^2] will behave this way. However f2[x_] :== Module[{y}, y == 5; If[x >== 0, y == x^2, y == -x^2]; y] will not, and f[b] will evaluate to 5, since the If clause will remain unevaluated. One might add a fourth argument to If, telling what to do in situations when the condition cannot be evaluated. But this is an issue, easily forgotten, to consider every time when you write an If clause: what happens if the condition does not evaluate to True or False? Best regards Ingolf Dahl -----Ursprungligt meddelande----- Fr=E5n: Sebastian Schmitt [mailto:sschmitt at physi.uni-heidelberg.de] Skickat: den 11 oktober 2010 11:17 Till: mathgroup at smc.vnet.net =C4mne: [mg113022] If without else Dear all! (I recycle my disclaimer.) I'm new to Mathematica with a background mostly in C++. Many times I have the impression that my style is not natural-Mathematica (Mathematicaesque so to say). I have a question to the usage of the If condition. In[72] :== x == 2; b == 3; In[73] :== If[b > 2, x == 10,]; x Out[73] == 10 In[74] :== If[b > 3, x == 20,]; x Out[74] == 10 Is it good-practice to leave the "else" empty? Is x :== 10 /; b > 2 what I'm looking for? Thanks in advance, Sebastian