Why does Execute return 'undefined' when accessing a loop variable?

The following code tries to use Execute() function to convert the variable var containing the string "x" to the loop variable 'x' and print its value. It returns 'undefined' 10 times instead of printing the numbers from 1 to 10.

WHY DOES THIS FAIL?

   var ="x"
   for x = 1 to 10 do
   (
     y = execute var
     print y
   )

The answer to this question is based on the fact that Execute always uses the global scope. On the other hand, a for loop always creates a new scope and the variable 'x' is local to this scope. This is the key to understanding the original problem and its solution - the Execute just does not "see" the local variable 'x' in the global scope.

Here is a possible solution which correctly prints the numbers from 1 to 10.

EXAMPLE

   global x --define a global stand-in variable for executing
   var = "x" --define the string to execute
   for xx = 1 to 10 do--the loop has a different var name
   (
     x = xx --copy the loop variable into global scope
     y = execute var --execute in global scope
     print y --print the result
   )