# Getting unexpected results form If expressions [closed]

I'm mystified by this:

g[a_, b_, c_] :=
Module[{d},
d = If[(b < 1), b, b + 1];
If[a < b, d = 0.];
d*c]
g[a, 20, 40]


840

I expected to see an output involving symbolic argument a !?? Now I change the second If so that d is set as returned by If, rather than by above's If argument statement, and now things work as expected:

g[a_, b_, c_] :=
Module[{d},
d = If[(b < 1), b, b + 1];
d = If[a < b, 0., d];
d*c]
g[a, 20, 40]


40 If[a < 20, 0., d\$132874]

Just for a bit more evidence, here is a 3rd casting, which also behaves as expected, although evaluation differs as expected:

g[a_, b_, c_] :=
Module[{},
If[(b < 1), b, b + 1]*If[a < b, 0., 1.]*c]
g[a, 20, 40]


840 If[a < 20, 0., 1.]

How does one explain the result of the first expression form?

• I expected to see an output involving symbolic argument a !?? why? Also note that since a is a symbol, then If[a < b, d = 0.] does nothing since you do not have 3rd argument to If. Check the third argument to If in help for more info. – Nasser Sep 20 at 23:42

g[a_, b_, c_] :=

the 1st line of the body of the module sets d to 21. The 2nd line can not be evaluated since the argument symbol a is not bound to a number, so it is left unevaluated and has no effect on d. The 3rd line, therefore, evaluates to 21 * 40, so 840 is returned.