Loop variables - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11) +--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2) +---- Forum: GitHub Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=42) +---- Thread: Loop variables (/showthread.php?tid=2706) |
Loop variables - Kernelpanic - 05-18-2024 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)
"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)
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? RE: Loop variables - DSMan195276 - 05-18-2024 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)
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. RE: Loop variables - Kernelpanic - 05-18-2024 @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? RE: Loop variables - Kernelpanic - 05-18-2024 Damned! Sure, “i” is 10 here. It gets clearer here (Korrektur, that is clearer): Code: (Select All)
Now "i" points to a memory address because it hasn't been assigned a value, plus 10. Yes, "i" is local.RE: Loop variables - DSMan195276 - 05-19-2024 (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. RE: Loop variables - Kernelpanic - 05-19-2024 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)
Can't one limit "_local" to the for loop? RE: Loop variables - SMcNeill - 05-19-2024 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. RE: Loop variables - SMcNeill - 05-19-2024 (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' Then one can pick exactly how local they want that variable to be. |