LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

FOR variable changes

Root / Programming Questions / [.]

SquareFingersCreated:
The point is that in python, the "loop variable" works the exact same way - if you change its address mid-loop, the FOR assignment of the next value will happen to the new address.
The line "Python behaves EXACTLY like SmileBASIC does" threw me off, because it is false, but I do see your point now. I had a look at the Python documentation. It does not refer to a 'loop variable'. What comes between the 'for' keyword and the ':' symbol is named a "target list", and the semantics of that target list are specified. It was a mistake for me to bring up Python. I didn't fully understand what I was writing about. I imposed on a language (Python), a concept foreign to the language (the 'for loop variable'), so in retrospect I was bound to trip myself up. Sorry for wasting your time. It's not as if I want SmileBasic to be like Python anyway. I want SmileBasic to be like the SmileBasic documentation.

There is such a thing as a "loop variable"
I'm afraid not, and this is one point that NeatNit and I agree on. As he writes: "the resulting target variable (or in this case, array element) is a different one than it was before", i.e. there is no single variable for the loop as a whole.
Yes, well, there's supposed to be just a single variable, but you change it within the loop so it changes. I mean yeah, what you said is correct, but what Rouseau said is also correct.
It's just that using the variable elsewhere is a bad idea. It gets used as a counter, after all, so it gets changed every time the loop runs.
In my code, I did not change any of the 'counter' variables. The counter variables are A[1], A[2], etc, and I don't touch any of those.
You didn't change THEM, but you switched between them. Each of them got incremented by 1 (besides the first of course), but you pulled the switcharoo.
The value after TO is also most definitely a value. It never changes.
Wrong again, I'm afraid.
LO=4
HI=10
FOR COUNTER=LO TO HI
PRINT COUNTER
HI=HI+0.5
NEXT
This code prints all the numbers from 4 to 16 inclusive.
This is correct. Has been this way in Petit Computer too, I think.
It's not necessarily like anything else you've worked with, including other versions of BASIC.
What I want SmileBasic to be like is SmileBasic documentation.
It works exactly as documented, when used as documented. When used differently, its results are mere side-effects to the way it was coded. If you switch the variable to a different one (which you did), then the new one is accessed. After that, it's accessed absolutely according to the documentation.

Just to clarify - what you have discovered is called a hack - some hole that the developers have left behind. It's not a very useful hack, but it's a hack. It needs to be deliberately caused.

Just to clarify - what you have discovered is ... some hole that the developers have left behind.
We are in agreement that this is a "hole". If you prefer, in future communications with you, I will stop referring to this as a "bug report" and instead call it a "hole report". It doesn't matter to me. Since you think of it as a "hole", you essentially agree with the message I'm trying to get across. EDIT: Removed some confrontational and unhelpful material that I put in my original reply... I apologize.

no worries, didn't even see it :) and I, too, sometimes get more aggressive than I intend to be. I think the only point of disagreement between you and me right now is whether this behavior is good or bad, acceptable vs unacceptable. But I don't see any way either of us will be convinced so we might as well leave it at that.

Differences in opinion. It is what it is. My stance, since SquareFingers asked, is that it's not a bug or a hole or a hack or anything similar that you might want to call it, that there is one particular variable (the so-called Loop variable) that the documentation indicates as the counting variable (bypassing it doesn't make it go away). It does appear to me that, in reality, for each FOR loop, the Loop variable first gets set (assigned or initialized, if you will) and then it gets incremented every subsequent step through that loop until the current value of said Loop variable reaches some other value. The vaguer the better. It also occurs to me that variables with elements declared can have more variables nested inside them (to represent the number of their elements) so long as the variable type used stays compatible. Also, I assume that two different elements of the same variable should generally be considered as such. So... I say it's both good and acceptable if not useful as well. Regardless, the OP says this: When the FOR statement is first encountered, J has the value 1, and the variable A[1] is initialized to 1. Inside the loop, J is incremented. When the NEXT is reached, the program goes back to the FOR. Now, A[2] is incremented, and checked against the to-value, 15. Next time, A[3] is incremented and checked. But this is what actually happens, start to finish: When the FOR statement is first encountered, J has the value 1 so the Loop variable (A[ ]) is at least temporarily A[1]. The value 1 is assigned to the variable A[1] which is then checked against the End value, 15. Inside the loop, J is incremented from 1 to 2. When NEXT is reached, the program increments A[2] (0 to 1) and then goes back to the TO. Now, A[2] is checked against the End value, 15. In the loop, J is incremented from 2 to 3. NEXT increments A[3] (0 to 1) and then goes back to the TO. Then, A[3] is checked against the End value, 15. In the loop, J is incremented from 3 to 4. NEXT increments A[4] (0 to 1) and then goes back to the TO. Now, A[4] is checked against the End value, 15. In the loop, J is incremented from 4 to 5. NEXT increments A[5] (80 to 81) and then goes back to the TO. Finally, A[5] is checked against the End value, 15, and since A[5] currently has a value of 81, the loop ends. This is all by the book and easily understandable if you take all of the documentation into consideration as a whole. That includes (to name a few things) the e-manual, the instruction list, update logs, and the specs page I linked to before. Look for "nesting" if you want to learn a new and exciting way to say "Yes, you can." Nesting a variable in the Loop variable just adds one potential level of abstraction to the process, the same way it would if you did it anywhere else. All the rules stay the same. The Loop variable isn't "changing" just because you're looking at a different part of it between steps. It's still A[ ]. Not B[ ] or SEAGULLS[ ] or GAZEBO[ ]. You just have to make sure everything's compatible. It's a lot like backing up with a trailer attached to your car when you think about it. The hitch doesn't change into a different one (or a gazebo, for that matter) when its angle to your car changes but that angle (along with the angle of the car's front tires, I suppose) does end up affecting where the whole thing goes. Here's one way to get this to do what I think you wanted it to do. I'll try to make it as long and convoluted as possible.
DIM A[100] 
'DIM or VAR, pick your poison.

  A[5]=12 
'Value of element 5 is 12

  A[8]=10 
'Value of element 8 is 10

  A[15]=80
'Effectively stops the loop using the calculated End value

  HIGH=15 
'Variable to use instead of the number 15

  TOP=0 
'Variable to use instead of the number 0 when not using J

  J=0 
'Starting value of the nested variable of A[ ]


  A[0]=A[HIGH]-1 
'Header info (79 here) to help tell where the loop should stop
'A[J] technically starts here as A[A[HIGH]-1]. Yowza!

 FOR A[J]=A[J] TO A[TOP]
'The first time->FOR A[0], assign 79 (should be that already), check against 80, it passes.
'Later STEPs->check current value of A[J] against 80 to see if it passes.

  PRINT "Number ";J;" is ",A[J]
'This prints locations/values of elements the loop passes through.

  INC J
'This is driving the proxy counter, J. It moves A[ ] to the next element.

  DEC A[J]
'This negates the effect of the coming NEXT on the current element of A[ ].

 NEXT
'This increments A[J] and goes back to TO.

PRINT "Number ";J;" is ",A[HIGH]
'This prints the value of the element that the loop finishes on.
That should list all values of A[ ] from A[0] to A[15]. It works basically the same way as the OP's example but with all arguments in the FOR loop replaced with variables, some of which include nested or double nested variables to boot. There's a function added to decrement A[J] so that counting duties can effectively be proxied to the nested J without causing elements with predetermined values from being altered. You just need to wrap your head around what it's all actually doing. Fortunately, (in my opinion) SmileBoom has done a fair-to-midland sort of job explaining it all. The sample programs are documentation, too, in case you were wondering. Lots of good comments in there even if they are in Japanese sometimes. I'm going to lightly suggest this topic be relocated to Programming Questions since I definitely see the potential here. You can get this to assign a value (across the elements of an array, no less) each step if you program that in and you don't necessarily need to tell it to print, either. It could be writing that information to an empty program slot and then saving it out to a file if you wanted. There are much more robust versions of this sort of "bait-and-switch" tactic in SmileBoom's own code if you're willing to dig through it. Actually, a "Programming Tips and Tricks" board would probably be even better, now that I think about it.

The vaguer the better.
I have an opinion on that.

More vague means more options. The more specific the instruction is, the less things you can do with the tool.

More vague also means more unpredictable. Adding specificity to the description of an instruction does not change what the instruction does, it does not mean that you can do less things with the instruction. On the contrary, it means that you can use the instruction with confidence in a wider variety of circumstances.

The widest variety of circumstances starts with the simplest logic. You have to work your way up but as you do, you can move forward with the confidence of knowing that all of your existing skills will translate to your new, higher level work.

The widest variety of circumstances starts with the simplest logic.
Yes. It must be logic. For example:
the Loop variable (A[ ])
A[] is not a loop variable, for two reasons: A[] is not a variable. It is an array. Or, if you want to use higher-level concepts, A[] is a variable of type array-of-numbers. It can only hold values of type 'array-of-numbers', it cannot hold values of type 'number', the same way a variable of type 'string' cannot hold values of type 'number'. The loop variable is that which occurs between the 'FOR' keyword and the '=' sign. "A[]" is not what occurred between the 'FOR' keyword and the '=' sign.

This is SmileBASIC. That's just how it works and you're going to reconcile that if you want to use some of the more advanced techniques. If something is in that spot, it's the Loop Variable. Also, from the instruction list: DIM (1) Format: DIM Array variable name[ Number of elements ] ,… It says "Array variable name." The word variable is in there, is it not? If you can show me where it says that an "array variable" isn't a variable then I'll concede this point but, like I've been saying, it's all by the book here. When in SmileBASIC, one must do as the SmileBASIC does.

You're right.
A[] is not a variable.
I take it back.

Here's one way to get this to do what I think you wanted it to do. I'll try to make it as long and convoluted as possible.
-- some big-ass chunk of code --
That should list all values of A[ ] from A[0] to A[15].
That's hilarious, I love it. True to your word, you made it as convoluted as you possibly could. This code does the exact same thing:
DIM A[100] 

' ------- just filling up data, meaningless:
  A[5]=12
  A[8]=10 
  A[15]=80
' --------

  HIGH=15
' Number of elements to check


 FOR J=0 TO HIGH

  PRINT "Number ";J;" is ",A[J]
'This prints locations/values of elements the loop passes through.
 NEXT

PRINT "Number ";J;" is ",A[J]
'This prints the value of the element that the loop finishes on.
I honestly can never ever fathom an instance where using an array element as a FOR variable does anything productive.
I'm going to lightly suggest this topic be relocated to Programming Questions since I definitely see the potential here. You can get this to assign a value (across the elements of an array, no less) each step if you program that in and you don't necessarily need to tell it to print, either. It could be writing that information to an empty program slot and then saving it out to a file if you wanted. There are much more robust versions of this sort of "bait-and-switch" tactic in SmileBoom's own code if you're willing to dig through it. Actually, a "Programming Tips and Tricks" board would probably be even better, now that I think about it.
I'm sorry, I just can't see ANY potential here. And besides that, I was just going over the instruction list and noticed a useful function called FILL: Sets all the elements in an array to the specified value , so you don't even need a FOR loop to do that.

I could see using an array element as a FOR variable being useful in a situation where you're using recursion for an arbitrary number of nested for loops (and you want to be able to access the variables from the outer loops, so just using a local variable as the index doesn't cut it). Like when generating permutations or something.

that's why the convention is to use I, then J, then K, etc.

Here's one way to get this to do what I think you wanted it to do.
If anyone thinks I wanted the code to do anything other than illustrate what happens when the code is run, they misjudge my intentions. Which is why I repeatedly resist making things personal: it is not about what I want, and it is certainly not about what you think I want, when you're that wrong about it.
This code does the exact same thing:
It is not about code that accomplishes a particular goal. It is about the behaviour of particular statements in particular circumstances. Please stop wasting space.

that's why the convention is to use I, then J, then K, etc.
That's the exact opposite of having arbitrary nesting, if you have to give a name to every one. I mean having the number of nested loops being determined at runtime.