[Gambas-user] Variable reinitialized at each iteration of a loop structure
Brian G
brian at westwoodsvcs.com
Tue Mar 28 02:58:12 CEST 2023
Hi Bruce,
Thanks for your reply.
I do understand that in fact both examples are actually implements at the highest level of any given function.
And I understand that the initialization value is equivalent to breaking it into two statements.
I was really looking for a consistence in the behavior of values as one enters a block of code where the value is defined.
Would it be better to have the dim a as integer = 1 be an initial value upon entering the block. instead of being reset on every iteration of the loop.
In that notation One would then write explicitly a = 1 at the top of each loop if it should be reset.
as well if a variable is declared within a block with no initial value should it be reset to the default value at that time.
more like :
for b as integer 0 to 100
dim c as integer = 50 ' Set upon entry
dim e as integer ' e gets set to the default value
forloop:
inc c
print c
next ' goes to forloop
I know this is different from the actual implementation, but the question is would this be more consistent?
And yes I understand I could move both to just outside the loop to get this effect,
but since we allow this type of notation....
"Failure is the key to success;
each mistake teaches us something" .. Morihei Ueshiba
Brian G
----- On Mar 27, 2023, at 11:15 PM, Bruce <adamnt42 at gmail.com> wrote:
> I think
> In the first example
> dim a as integer = 1
> is shorthand for
> dim a as integer
> a = 1
> in the second example, try reading it like this:
> for y = 0 to 2
> for i = 1 to 1000000
> dim b as integer <--- i.e. the "declaration" is implemented at the highest
> block.
> inc b
> next
> print "b=";b
> next
> You could try running it with the debug option to see this
> b
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20230327/2fad74c0/attachment.htm>
More information about the User
mailing list