Variable value in a Table

Here:

t = 7;
Table[t+1,{t,1,10}]

I expected t = 10 or t = 11, but t =7.
Why?

The variable t inside Table is protected from outside assignments.
You can force t to get the outside value 7 this way:

t = 7;
Table[Evaluate[t + 1], {t, 1, 10}]

Thanks.

It’s a strange (and confused) behavior.

In[1]:= t = 7

Out[1]= 7

In[2]:= Table[Evaluate[t + 1], {t, 1, 10}]

Out[2]= {8, 8, 8, 8, 8, 8, 8, 8, 8, 8}

In[3]:= t

Out[3]= 7

In[4]:= Table[t + 1, {t, 1, 10}]

Out[4]= {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}

In[5]:= t

Out[5]= 7

This is how it should work. When you run use “Evaluate”, you are asking for that section to evaluate before being given to Table. And in Table, you’ve asked for “t” to be localized.

That behavior is how this is all supposed to work.

Thanks.

Yes, this is how it works, but it’s (for me) strange behavior: t in Evaluate is diferent that t in list {t,1,10}.

Thanks, again.

I can understand why it is confusing.

If it helps, Evaluate should almost never be used. So understanding this distinction is not really important for writing good code.

The behaviour of Table is the same as Plot. If you write

x = 1;
Plot[x, {x, 0, 1}]

do you expect the same output as

Plot[1, {x, 0, 1}]

?

Plot and Table are designed to protect their iterator variable from outside interference.
You can bypass the protection with Evaluate, which forces evaluation of the argument before anything else.
Sometimes you cannot avoid Evaluate: the following does not work:

Table[x[t] /. DSolve[{x'[t] == -x[t], x[0] == 1}, x, t][[1]], {t, 1, 
  10}]

and you have to do it this way

Table[Evaluate[
  x[t] /. DSolve[{x'[t] == -x[t], x[0] == 1}, x, t][[1]]], {t, 1, 10}]

But in this case you have to be careful of ouside interference with the variable t:

t = 1;
Table[Evaluate[
  x[t] /. DSolve[{x'[t] == -x[t], x[0] == 1}, x, t][[1]]], {t, 1, 10}]

Evaluate can also be used on the iterator side, and it forces the iterator variable to become a constant:

t = 1;
Table[t, Evaluate[{t, 1, 10}]]

I understand how it works but I think it is confuse.

See this!

In[1]:= t = 7

Out[1]= 7

In[2]:= Table[t, {t, t, 10}]

Out[2]= {7, 8, 9, 10}

In[4]:= Table[t, {t, t, t + t}]

Out[4]= {7, 8, 9, 10, 11, 12, 13, 14}

This is also completely correct. the other t’s in the list take on the global value of “t”. The first one is just the variable for the local iteration.

It is easy to create intentionally confusing code like:

t = 7; Table[t, {t, t, 10}]

Most programmers cannot tell you what this code would do. They just avoid this kind of ambiguity.

Avoiding ambiguity is an important programming skill. More important than a strict understanding of how code evaluates.

I knew a proffesor in college (not Comp Sci.) who loved to create these kinds of puzzles in the C programming language and gave them to his students. He had no real programming experience and got very little taught except making sure his students hated programming as much as he did.

Without the protection of the iteration variable, the command Table[t,{t,1,10}] could give different, apparently random, results at different times in a session. I think that would be more confusing.