View Full Version : New in next thinBasic: FOR with on-the-fly variable definition
ErosOlmi
04-07-2008, 00:38
In next thinBasic FOR clause will have a new syntax:
FOR Counter [AS AnyNumericType] = nStart TO nStop [STEP Increment] [{WHILE | UNTIL} LogicalExpression]
...
NEXT
So, it will be possible to define the looper controlling variable on the fly. Something like that will be valid:
for X as long = 1 to 16
for Y as long = 1 to 16
printat format$(x*y, "000"), X*4, Y, 14
next
next
Before it was needed something like:
DIM X, Y AS LONG
for X = 1 to 16
for Y = 1 to 16
printat format$(x*y, "000"), X*4, Y, 14
next
next
Hope you like it.
Ciao
Eros
I will love it because playing with C++. I got used to doing this method.
Thanks that is very nice surprise ERos!
Michael Hartlef
04-07-2008, 07:29
Does the declared variable stay declared after the for loop is finished?
Anyway, another nice addition to the language.
ErosOlmi
04-07-2008, 07:44
Does the declared variable stay declared after the for loop is finished?
I too thought about that. Currently yes, it will remain declared.
If you are in a GLOBAL scope (not inside any function), it will be declared GLOBAL. If you are inside a function, it will have LOCAL scope.
Petr Schreiber
04-07-2008, 08:58
Dirty, C-ish, ... but I like it :D
Thanks,
Petr
Michael Clease
04-07-2008, 09:39
Is that optional or can we still use the old syntax?
Petr Schreiber
04-07-2008, 10:00
As the datatype specification is in square brackets, I presume it means it is optional.
ErosOlmi
04-07-2008, 10:23
Is that optional or can we still use the old syntax?
Of course optional. Standard FOR/NEXT will work as usual. It is just an additional way everyone can choose from.
That's one of the advantages of an interpreted language: many syntaxes can be active at the same time and also it is quite easy to change and adapt.
Eros
zlatkoAB
05-07-2008, 18:03
Hi guys interesting stuff but i prefer this:
for X as long = 1 to 16
for Y as long = 1 to 16
printat format$(x*y, "000"), X*4, Y, 14
next Y
next X
it is easy to track... :)
ErosOlmi
05-07-2008, 18:55
Well,
thinBasic does not have NEXT followed by the controlling variable because it is not necessary. Every FOR/NEXT creates a nested level that the compiler or the interpreter are able to track by themself. But yes, adding the controlling variable after NEXT cen result is more readable code if you are used to that syntax.
I'm sorry, I will not implement it for the moment because parsing the controlling variable after the NEXT takes a lot of time for the parser. But I will study if I can find a way to have both syntax with a minimum of speed loosing if variable is present.
Ciao
Eros
Petr Schreiber
05-07-2008, 20:56
Zlatko,
if you are puzzled by NEXTs lacking specified variables, you can still add simple comments there:
for X as long = 1 to 16
for Y as long = 1 to 16
printat format$(x*y, "000"), X*4, Y, 14
next 'Y
next 'X
It is just one character extra, and it means no hurt to parsing performance, as ThinBasic cuts the comments before execution.
Bye,
Petr
sandyrepope
05-07-2008, 21:05
Hi guys interesting stuff but i prefer this:
for X as long = 1 to 16
for Y as long = 1 to 16
printat format$(x*y, "000"), X*4, Y, 14
next Y
next X
it is easy to track...
I must admit that I don't understand the need for having the variable following 'next'. It seems to me that for now a work around could be:
for X as long = 1 to 16
for Y as long = 1 to 16
printat format$(x*y, "000"), X*4, Y, 14
next ' Y
next ' X
By commenting out the variable, thinbasic can still handle the for/next loops and by having the variables there as comments it still remains clear.
Just my thoughts on the subject.
Thanks
Sandy
ErosOlmi
05-07-2008, 22:04
OK, done.
Next thinBasic version will manage both syntax without speed reduction.
thinBasic will allow the Counter variable after NEXT keyword but it will be ignored and parsing will jump over the next token.
There will be no execution speed reduction because there will be a check of the presence of Counter variable after NEXT keyword during Script-Pre-Parsing phase. During this phase, if parser will find some NEXT followed by looper variable, they will be "marked" so real execution will just ignore the next tokens and jump to the exd of line.
Ciao
Eros
zlatkoAB
06-07-2008, 07:55
Wow i just give suggestion i dont mean nothing wrong.Ok
regards
ErosOlmi
06-07-2008, 09:07
In fact your suggestion has been taken into consideration ;)
Thanks
Eros
sandyrepope
06-07-2008, 13:52
Wow i just give suggestion i dont mean nothing wrong.Ok
regards
I don't think you did anything wrong! I hope you didn't get any ideas from what I said that could have made you think you did anything wrong. I believe that any suggestions can and should be made. Please feel free to make any suggestions you have.
Thanks
Sandy
zlatkoAB
09-07-2008, 09:39
OK sandy :)