Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop variables
#1
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?
Reply


Messages In This Thread
Loop variables - by Kernelpanic - 05-18-2024, 08:13 PM
RE: Loop variables - by DSMan195276 - 05-18-2024, 08:20 PM
RE: Loop variables - by SMcNeill - 05-19-2024, 07:45 PM
RE: Loop variables - by Kernelpanic - 05-18-2024, 08:43 PM
RE: Loop variables - by Kernelpanic - 05-18-2024, 09:55 PM
RE: Loop variables - by DSMan195276 - 05-19-2024, 12:43 AM
RE: Loop variables - by Kernelpanic - 05-19-2024, 07:22 PM
RE: Loop variables - by SMcNeill - 05-19-2024, 07:33 PM



Users browsing this thread: 2 Guest(s)