ClearAll[x, xx, z]
xx = Array[x, 2];
y = If[z == 0, 0, xx[[2]]]
gives for y:
If[z == 0, 0, xx[[2]]]
I want to see
If[z == 0, 0, x[2]]
I can't figure out how to do this. Any help would be appreciated.
ClearAll[x, xx, z]
xx = Array[x, 2];
y = If[z == 0, 0, xx[[2]]]
gives for y:
If[z == 0, 0, xx[[2]]]
I want to see
If[z == 0, 0, x[2]]
I can't figure out how to do this. Any help would be appreciated.
You can wrap the third argument of If with Evaluate to force evaluation of that expression:
y = If[z == 0, 0, Evaluate@xx[[2]]]
If[z == 0, 0, x[2]]
If[condition,t,f] is left unevaluated if condition evaluates to neither True nor False.
For example:
w = 3;
If[z == 0, 2 w, w^2]
If[z == 0, 2 w, w^2]
whereas
If[True, 2 w, w^2]
6
If[False, 2 w, w^2]
9
Since If has the attibute HoldRest the second and third arguments remain unevaluated until the first argument is determined to be True or False(in the former case only the second argument is evaluated and in the latter only the third argument). So, when the first argument evaluates to False you do get x[2] as the output from If without the need to wrap the third argument with Evaluate:
If[False, 0, xx[[2]]]
x[2]