Iterators

RacingDog

Active Member
Licensed User
Hi, it's the class dummy again....

Why is this an error.....

Dim i As Integer
For i = 0 To 5
<something or other>
Next i

When I attempt to run it says iterators have to be a string, number or undefined.

That is surely wrong. Why insist on inefficiency and prevent efficiency? Why is every other language wrong and B4PPC is right?
 

RacingDog

Active Member
Licensed User
Well, yes, so it seems, but the question is why? Computers are naturally integer machines and anything else involves a degree of inefficiency, so this makes no sense to me at the moment. I can't think of any variant of the imperitive language family that insists on preventing this. I can think of some where it wasn't possible (early Basics for example, right back to the original Dartmouth interpreter) because they didn't have integer types. But when an integer type is available, to prevent it's use is surely nonesense?
 

agraham

Expert
Licensed User
Longtime User
You don't actually have to "Dim " your iterator at all but as Klaus noted if you do it needs to be a Number, or a Double which is equivalent, or a String. The code produced is however slightly more efficient if you do declare iterator types as Numbers. Also you do not need to specify the iterator after "Next" as strict block structuring enures there is no ambiguity.

Iterators are not restricted to integer values "For i = 0.5 to 3.7 Step 0.1" is valid so for simplicity in language implementation all iteration values are treated as Numbers. This therefore is the reason for the restriction.
 

mjcoon

Well-Known Member
Licensed User
Iterators are not restricted to integer values "For i = 0.5 to 3.7 Step 0.1" is valid ...

Remembering that representation of fractional values in binary is not necessarily precise, so incrementation might not lead to exactly the values you expect. Best to allow some slack for rounding error...

Mike.
 

RacingDog

Active Member
Licensed User
Thanks. I see. But I can't agree with doing that. I don't think it is that much of a simplification (I have worked with parser generators etc so I know what is involved, "not daunting" is the phrase I would use).

You are also promoting appallingly bad programming.

To one extent you have no choice, undeclared variables are part of Basic culture. But I have to say that this very sad. For the sake of a few seconds of extreme idleness, one risks many hours of debugging faults that were entirely avoidable simply by declaring variables. Is that the intelligent person's choice? For that reason I will never leave a variable undeclared and if I ever submit a script here in which that is done then it will be an error that needs correcting and I will be pleased to be told.

Back in 1968, at the University of Kent, we took delivery of the very first Dartmouth Basic interpreter in the UK. So for a brief period we were the only people in the country using Basic (a dubious honour that I'm not sure is so wonderful, we were already using Algol 60 and an interpreter for that to complement the compiler would have been better). In those days the only data type in Basic was real numbers and was therefore implicit. Nonetheless, and well before the era of "politically correct" programming, we rapidly developed a list of things not to do. So it has been known for 42 years at least that iterating with fractions was the nearest thing to expressing a death wish in a program. That of course is due to rounding errors. For the sake of occasionally introducing a very occassional minor inefficiency, it soon became accepted that no sane programmer would even think of using anything but integer variables as iterators, even though they were stored as numbers. When integer types were eventually introduced to Basic, only the foolhardy failed to switch to using integer variables for iterators, espacially as in most uses it is far more natural and efficient than any other method. And yet that is the one thing that isn't allowed in this dialect. That seems very strange.

The error dialog indicates that I should use undefined, number or string. STRING??? Someone just has to be joking. One does something as complex as implementing that as an iterator and then one says that integer is left out for simplicity? Wherein lies any sort of consistency? Also, how can the resulting code ever be efficient?

Once upon a time, some of the more obsesive of the politically correct programming brigade insisted that a language was no good if it's compiler/interpreter wasn't written in the language itself (after an initial non-native subset to boot the process of course). So this question is not coming from that angle, this is just simple curiosity, but what is B4PPC written in?

Hmmm, this could be a fun thread. I like a good discussion.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
I can think of some where it wasn't possible (early Basics for example, right back to the original Dartmouth interpreter) because they didn't have integer types. But when an integer type is available, to prevent it's use is surely nonesense?
I agree that the lack of support for integers in a For loop is far from being perfect. I believe that it will be added in the future. Support for typed regular variables was only added in v6.90. Implementing such a fundamental change was a very sensitive process and there were places where I favored simplicity over the "perfect solution" in the first version.
 
Top