Posts: 1,002
Threads: 50
Joined: May 2022
Reputation:
27
05-18-2024, 08:13 PM
(This post was last modified: 05-18-2024, 08:15 PM by Kernelpanic.)
I have a question regarding loop variables.
A loop variable should actually only be declared before or direct in use, something like in C (all loop variables are local, related to the loop):
Code: (Select All)
#include
#include
int main(void)
{
int i;
for (int i = 0; i < 5; i++)
{
printf("\n%2d", i);
}
printf("\n\n");
for (int i = 0; i < 5; i++)
{
printf("\n%2d", i);
}
printf("\n\n%2d", i = 10);
return(0);
}
"i" is now really 10!
In Basic, everything is usually declared at the beginning of the program, and there is rarely something like this:
Code: (Select All)
Option _Explicit
'. . .
Dim As Integer i
For i = 1 To 5
Print i
Next
Print
For i = 1 To 3
Print i
Next
Print
Print i + 2
And the last "i" will be taken. Of course, because it's not really local.
As mentioned, is it possible to do it in Basic like in C, without much effort, or does it not fit into Basic's grammar at all?
Posts: 303
Threads: 10
Joined: Apr 2022
Reputation:
44
It's not possible - all variables in QB64 are scoped to the main code or to the sub/function they're in, there is no local scope for loops or IF's like in other languages. Ex: This is not valid in C but is valid in QB64:
Code: (Select All)
a = 20
If a = 20 Then
Dim b As Long
b = 40
End If
Print b
That's perfectly valid code that prints 40, but the `b` is declared inside of the `If`. Despite that, the `b` still exists everywhere after the `Dim` regardless of control structures, it's not scoped to only inside of the `If` that it was declared inside of.
That said it is potentially something that could be introduced, maybe a `Dim _Local` or something, but that's never been discussed.
Posts: 1,002
Threads: 50
Joined: May 2022
Reputation:
27
@DSMan195276, Thanks for the explanation. The "_local" is a good idea. It would professionalize Basic even more. But what would the workload be in Basic?
Posts: 1,002
Threads: 50
Joined: May 2022
Reputation:
27
05-18-2024, 09:55 PM
(This post was last modified: 05-18-2024, 10:14 PM by Kernelpanic.)
Damned! Sure, “i” is 10 here. It gets clearer here (Korrektur, that is clearer):
Code: (Select All)
#include
#include
int main(void)
{
int i;
for (int i = 0; i < 5; i++)
{
printf("\n%2d", i);
}
printf("\n\n");
for (int i = 1; i < 5; i++)
{
printf("\n%2d", i);
}
//"i" is not initialized
printf("\n\n%2d", i);
//"i" is not initialized + 10
printf("\n\n%2d", i = i + 10);
printf("\n\n%2d", i = 10);
return(0);
}
Now "i" points to a memory address because it hasn't been assigned a value, plus 10. Yes, "i" is local.
Posts: 303
Threads: 10
Joined: Apr 2022
Reputation:
44
(05-18-2024, 08:43 PM)Kernelpanic Wrote: @DSMan195276, Thanks for the explanation. The "_local" is a good idea. It would professionalize Basic even more. But what would the workload be in Basic? It's a fair question, and I'm honestly not sure. I suspect there are ways to implement it that aren't too crazy, but either way the compiler would have to start tracking which control scope the variables belong too, that might be complicated to add.
Posts: 1,002
Threads: 50
Joined: May 2022
Reputation:
27
Actually, the running variable mentioned here only refers to the “for loop”, or am I wrong?
In C forums something like this was immediately criticized as a beginner's mistake:
Code: (Select All)
. . .
int i;
. . .
for (i = 1; i < x; i++)
. . .
Can't one limit "_local" to the for loop?
Posts: 2,696
Threads: 327
Joined: Apr 2022
Reputation:
217
We've discussed this type of thing before, but the main issue is someone having the time and dedication to sit down and work it into the language for us.
FOR i AS INTEGER = 1 TO 10 STEP 2
PRINT i
NEXT
In the above, i would only be an integer for that short FOR...NEXT routine, and the best usage for this type of coding would be for temp variables with use with OPTION _EXPLICIT so one doesn't have to stop and declare that variable to prevent error/warning messages.
Posts: 2,696
Threads: 327
Joined: Apr 2022
Reputation:
217
(05-18-2024, 08:20 PM)DSMan195276 Wrote: That said it is potentially something that could be introduced, maybe a `Dim _Local` or something, but that's never been discussed.
Perhaps something similar to the following could be added someday:
Code: (Select All) _Scope 'foo'
As Long a, b, c
As Single s
As Double d
End _Scope
_ScopeStart foo
'use a, b, c, d, s all you want as they were defined in _SCOPE 'foo'
_ScopeEnd foo
Then one can pick exactly how local they want that variable to be.
|