[Gambas-user] Variable reinitialized at each iteration of a loop structure

Jussi Lahtinen jussi.lahtinen at gmail.com
Wed Mar 29 01:48:36 CEST 2023


You made a very good point. But normally "Dim p as Integer" also
initializes the variable, and within the loop this is the only thing left
to do after the initial declaration (or raise error that variable is
already declared). So, this is up to interpretation on what characteristics
you are relying on. No logical resolution.
Thus now I think this works logically only, if the variable has scope
limited to the loop where it is declared like in C. I do not like this.

Jussi



On Wed, Mar 29, 2023 at 1:13 AM Christof Thalhofer <chrisml at deganius.de>
wrote:

> Am 28.03.23 um 09:54 schrieb Benoit Minisini:
>
> > By the way, I almost never declare variables in the middle of a
> > function. It's always a source of confusion, sometimes bugs, and make
> > the code less readable.
>
> It was a bad idea, I was against that. But now I think, that a 'Dim'
> inside a function should behave like that to be logical:
>
> If the code explicitly defines it's content it is clear that the
> variable contains the content defined:
>
> -----------------------------------------------------------
>
> For i = 0 to 100
>     Dim p as Integer = 0
>     Inc p
> Next
>
> Print p
>
> Prints 1
>
> -----------------------------------------------------------
>
> If the 'Dim' in the code does not explicitly define it's content, the
> preexisting content has to be preserved. This would be the same as if
> the variable were declared at the top of the method:
>
> -----------------------------------------------------------
>
> For i = 0 to 99
>     Dim p as Integer
>     Inc p
> Next
>
> Print p
>
> Prints 100
>
> -----------------------------------------------------------
>
> This is what Gambas does and IMO it should not be altered.
>
> IMO it would be a bad idea and illogical if Gambas alters the content of
> the variable silently to '0' if the programmer did not explicitly wanted
> it. Because on the other hand if the programmer wants a change of the
> content, it must be defined in the code.
>
> Alles Gute
>
> Christof Thalhofer
>
> --
> Dies ist keine Signatur
>
>
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20230329/b387aa5a/attachment.htm>


More information about the User mailing list