I don't know if I understand what that expression is doing, but I'll try to help anyway. What I suggest in my previous post is that for every 'for' statement, you add an 'if' statement to see if you want to continue. So, if you built it for a maximum n of 3, it might look something like this:

for e1 = 0:1

if n > 1

for e2 = 0:1

if n > 2

for e3 = 0:1

P=X*P(Y=e1)P(Y=e2)P(Y=e3);

end;

else

P=X*P(Y=e1)P(Y=e2);

end;

end;

else

P=X*P(Y=e1);

end;

end;

At level 1, the expression checks to see if n is greater, if so, it moves on up a level, if not, it runs the expression.

Depending on what exactly you're trying to do with your expression, though, there might be another way. For example, if you must have all of your 'for' loops inside of other 'for' loops, and each goes from 0 to 1, you are running through 2^n cycles. Knowing this, you might be able to have a loop that runs from 1 to 2^n to do what you need instead. If, however, you just need to run n independent loops that go from 0 to 1, you can use the eval command.