If and summation: why do I have the index of the summation in the final result?
The other answer by Andrew technically answers your question, but, maybe what you really wanted was
Sum[If[b =!= 0, a[m], 0], {m, 1, 4}]
which returns
a[1] + a[2] + a[3] + a[4]
The difference between the !=
and =!=
operators is very important here. Because the truth value of b != 0
could not be determined, therefore the
If[]
expressions were returned unevaluated.
I suggest moving the If[]
expression out of the summation. That is use
If[b =!= 0, Sum[a[m], {m, 1, 4}], 0]
instead. This simplifies the code and makes it easier to understand.
This works:
Sum[If[b != 0, a[m] // Evaluate, 0], {m, 1, 4}]
From the documentation:
If evaluates only the argument determined by the value of the condition.
$ $
You can use Evaluate to override HoldFirst etc. attributes of built-in functions.