For loop iterator variable scope

Please fill out these questions for all feature requests and bug reports. If you're requesting a feature, please let us know why this feature is important or useful, not just what it should do.

  1. What browsers show this problem? Chrome
  2. Please share an example project (if possible).
  3. Describes the steps to reproduce this issue.
    In Snap 5 the for loop creates the "i" variable. But the scope of this variable does not appear to be limited to within the for loop ("i" can be referenced after the for loop) And, it makes things confusing if the user creates a variable named "i" since the user variable does not appear to change when clicked to show the value.
  4. What does Snap! currently do? The scope and lifetime of the "i" variable created by the for loop is confusing.
  5. What should Snap! do instead? Use some unique symbol other than "i" because "i" is commonly used. And limit the scope and lifetime to within the for loop.

Hi, thanks for posting.

About the unique symbol, you can do that yourself: Click on the orange "i" circle in the FOR block (without dragging it), and a window will open in which you can give it another name.

About the scope, it's a local variable, but it can be used if there are more blocks after the FOR block in the same script. It's not available in other scripts.

I hope that helps!

Thanks. However, things get a bit confusing if I also create my own variable named i and try to use my variable named i both before and after the for loop. Since i is a pretty common variable name it might be best if the default is not i.

"I" is a common index variable name -- just the sort of thing you want to use in a FOR loop! If you're using "I" as a global variable name, then I'd say it's you who should rename your variable! :~P But, in any case, we're not going to pick an obscure name as the default, so if you use our default name there's always a chance of conflict. Just think of the FOR variable as a variable declaration. If you want to use the same name in a different variable declaration, you get to choose which name to change! It's not hard to change the name of the FOR variable.

Please take a look at this from the perspective of someone learning to program with Snap, or trying to teach someone how to program with Snap. This is the only place I have found where a variable gets created on the fly in Snap and its scope is a bit unusual. From the way it appears within the block it would appear as if the scope would be limited to the block, but instead the scope is anytime after the for loop. Treating the "i" variable of a for loop different than other variables in Snap will not make it easier to teach programming.

I use Snap as a tool to help students learn how to program and having a consistent, well designed tool is very helpful. I do like the addition of the for loop that is similar to the iterator for loop one would find in Python. This one has the feel of a for loop in C, but Snap doesn't allow for variable creation in other places and the scoping is off.

Here's a link to my code;

I created a variable named i.
I then set the variable to 5. (I checked the box to watch i -- it shows 5)
I then ran a for loop with i from 1 to 10
After the for loop I printed out the value of i and it is 11.
However, the value of i in the box still shows 5.

I know it can be explained to students, but it would be better if the explanation wasn't required.

I really hope you aren't teaching your students to use one-letter global variable names! If they do that, I'm afraid they deserve the resulting bugs.

The FOR variable works exactly the same as other script local variables (including procedure inputs and variables declared with the SCRIPT VARIABLES block -- and, for that matter, exactly the same as local variables in whatever your favorite programming language is. Namely, its scope is the script in which it occurs. (Most programming languages don't have more than one toplevel script, so for them, "local to a script" is the same as "local to a procedure.")

Here's how to explain it to your students:

  1. Don't use global variables!

  2. If you absolutely must use a global variable, don't give it a single-letter name! Give it a multi-word name that documents its purpose.

  3. There is no 3. There's no excuse ever to have a short name for a global variable.

The way I understand it is that the for loop automatically creates a script-level variable - effectively like this


so the scope of the script variable i is local to the script and not just the for loop

So, you could teach your students to always write for loops like this so that things are explicit


But why? Snap! isn't the only language in which FOR variables are automatically local to the scope that includes the FOR. And you're not going to use a SCRIPT VARIABLES for formal parameters, which are also local to the procedure's scope. At some point, his students are going to have to learn that some variables are implicitly local.

I provided feedback because I felt the product can be improved.

Here's an example of a language creating a local scope within the for loop

for ( int i = 0; i < 10; i++ )
printf("%d\n", i );
printf("%d"\n", i); // This should create a compile time error since i is not defined in this scope

I like the reply from cymplecy because it provides an easy way to explain the behavior.

Well, whatever floats your boat. I've just never understood the kind of teaching that's all about having a dozen variables all called x and making students try to figure out what they do. Or, as in your example, leaving out the braces and the indentation that would make the scope apparent.

When I was a high school teacher, and students would come to me with bugs they couldn't figure out, I had a rule that I wouldn't look at any program that had a variable named x. And when the students gave their variables self-documenting names, their bugs magically disappeared.

1 Like

I've enjoyed most aspects of Snap for teaching. I've been surprised with how well it works in most cases. The spaces for indentation were present before I hit reply and were then stripped, apparently leading spaces get removed on here. And yes, I do teach to use descriptive variable names. But, there will be students that use one letter variables or that change the i to clobber one of their own variables. Now I have a simple way to explain the behavior.

Okay. I'm glad you're enjoying Snap!.

The only thing I'm confused about is that the Control for loop's default variable is "i" but the Lists one is "item":

Oh, that's because with FOR EACH it's definitely a list you're iterating over, whereas with FOR you could be computing factorials or something.

for ( int i = 0; i < 10; i++ ) {
    printf("%d\n", i );
printf("%d\n", i); // This should create a compile time error since i is not defined in this scope

however, in python or javascript

for i in range(10):
print('loop over')


loop over


I agree! Its not readable!

Or get them in a namespace like
(list(list [myvar] [kkk]))