1. for puzzlement
- Posted by alrobnett Jan 07, 2019
- 1470 views
rn_nmbr is declared as integer
A procedure contains the statement:
for rn_nmbr = 1 to rn_limit
It functioned as expected.
Later, the identical statement was added to Main:
for rn_nmbr = 1 to rn_limit
The compiler now complains about an attempt to redefine rn_nmbr.
The manual seems to imply that "i" is the variable to use in 'for'.
Any insights?
2. Re: for puzzlement
- Posted by alrobnett Jan 07, 2019
- 1442 views
Addendum:
Changing the name in Main to rn_number did not solve the problem.
3. Re: for puzzlement
- Posted by _tom (admin) Jan 07, 2019
- 1452 views
You write a declaration; it is in the top-level of your file.
first rn_nmbr
rn_nmbr is declared as integer
That means rn_nmber is in-scope to the bottom of the file at the top level. Where in-scope means you can use that name for the purpose you expect.
second rn_nmbr
A procedure contains the statement:
for rn_nmbr = 1 to rn_limit
It functioned as expected.
A procedure or function describes a new nested code-block. It finishes with the keyword end. Inside this new code-block you can use any name (even if it exists on the outside) as a completely new name. The for statement has created a new varaible, called rn_number, that has nothing to do with "rn_number" you created outside of the routine. We say a declaration inside a routine shadows any declaration that comes before it.
third rn_nmbr
Later, the identical statement was added to Main:
for rn_nmbr = 1 to rn_limit
The compiler now complains about an attempt to redefine rn_nmbr.
I am guesing that "Main" is not a separate routine, but is part of the main file where you are writing statements...
This particular for statement is written at the "top-level" of the file. The name rn_number was already declared at the top level (its now used up) and can not be used again in a declaration.
In this case your third "rn_nmbr" is in fact the same as the very first one you declared (hence the for statement protests). The second "rn_nmber" is nested inside the routine; it does not exist outisde of the routine; is in fact a different name.
The manual seems to imply that "i" is the variable to use in 'for'.
Any insights?
The "i" in a for statement is an artifact of old-skule education; where math/physics courses use i,j,k (and x,y) for most any purpose. The "i" implies iterate which is what the for statement does; no magic here.
_tom
4. Re: for puzzlement
- Posted by _tom (admin) Jan 07, 2019
- 1425 views
Addendum:
Changing the name in Main to rn_number did not solve the problem.
It helps if you write a mini-program that contains just your problem.
integer rn_nmbr = 2 integer rn_limit = 4 procedure myloop( ) -- CAN use rn_nmbr as a completely new name for rn_nmbr = 1 to rn_lmit do ? rn_nmber end for end procedure -- can NOT use rn_nmbr here, alread declared for rn_nmbr = 1 to rn_limit do ? rb_bnbr end for
5. Re: for puzzlement
- Posted by ChrisB (moderator) Jan 07, 2019
- 1470 views
Hi
Already been explained, but another way to look at it is that the variable in the for loop is created when the loop is run, so logically you shouldn't have already created it.
in my opinion, and to avoid loop confusion, don't repeat the variable names, so
integer rn_nmbr = 2 integer rn_limit = 4 procedure myloop( ) for i = rn_numbr to rn_lmit do ? i end for end procedure
Just another opinion!
Cheers
Chris
6. Re: for puzzlement
- Posted by alrobnett Jan 08, 2019
- 1423 views
Aha, one more candle lighting the darkness. I thought I needed to declare the index if not using 'i'. I see now that whatever name is used for the index is automatically declared. By experiment, I found that using the same index name in two different 'for' statements does not create a problem.
7. Re: for puzzlement
- Posted by petelomax Jan 08, 2019
- 1402 views
Well, I never. Pre-declaring loop variables has worked for so long in phix that I forgot you cannot do that in OE.
8. Re: for puzzlement
- Posted by ChrisB (moderator) Jan 08, 2019
- 1425 views
Well, I never. Pre-declaring loop variables has worked for so long in phix that I forgot you cannot do that in OE.
Well, I never knew that you could do that in Phix either - this is a day of candles alighting for certain!
Cheers
Chris
9. Re: for puzzlement
- Posted by alrobnett Jan 08, 2019
- 1389 views
Not important but what is the significance of the 'O' in 'OE'?
10. Re: for puzzlement
- Posted by _tom (admin) Jan 08, 2019
- 1391 views
Not important but what is the significance of the 'O' in 'OE'?
In 1993 Robert Craig invented the Euphoria programming language. Euphoria was developed as shareware and RDS provided consulting.
Later, the source-code to Euphoria was released. From this code base the OpenEuphoria language extended the original Euphoria with a new standard library and extra features.
As lazy creatures, we sometimes type OE or oE instead of "OpenEuphoria" .
Meanwhile, Pete Lomax invented the Phix language. Phix uses some Euphoria ideas; it "fixes" some Euphoria limitations; but it is a separate language.
The more Euphoria the better means we promote both oE and Phix.
_tom
11. Re: for puzzlement
- Posted by euphoric (admin) Jan 10, 2019
- 1346 views
Well, I never. Pre-declaring loop variables has worked for so long in phix that I forgot you cannot do that in OE.
Somebody catch me up: Why would you pre-declare your loop variables?
12. Re: for puzzlement
- Posted by ghaberek (admin) Jan 10, 2019
- 1332 views
Somebody catch me up: Why would you pre-declare your loop variables?
In many languages, you have to declare the type of the for variable, and some may just require that be done before the for loop. Euphoria can infer the type from values given to the loop.
Fun fact: in the early days of C you had to do this. You also had to declare your parameter types between the function declaration and its body. Early C is weird.
void myfunc( foo, bar ) int foo; char *bar; { int i; for (i = 0; i < foo; i++) { printf( "i = %d\n", i ); } printf( "bar = '%s'\n", bar ); }
-Greg
13. Re: for puzzlement
- Posted by petelomax Jan 11, 2019
- 1306 views
Well, I never. Pre-declaring loop variables has worked for so long in phix that I forgot you cannot do that in OE.
Somebody catch me up: Why would you pre-declare your loop variables?
So you can look see how far the loop got before exiting, especially useful if it has multiple exit points, and/or if the whole point of the loop was just to set i anyway.
for i=1 to length(s) do ... if this then exit end if ... if that then exit end if ... if the other then exit end if ... end for ?i -- oops, error (unless you pre-declare it)
The only other sensible alternative would be to pre-declare another variable say ifail and set it to i at the top of the loop, btdt.
Pete
14. Re: for puzzlement
- Posted by irv Jan 11, 2019
- 1284 views
Given that looping constructs are very common in most programs, the idea of having the loop variable automatically declared and visible only within the loop is a good one. It prevents accidentally "clobbering" a variable by the same name declared elsewhere in your program. That can create a bug that can be very difficult to track down.
The reason the loop variable is (often) named i, j, k, l or similar is partly tradition (from Fortran, perhaps?) and partly common sense: more often than not, the loop variable is going to be used within the loop, perhaps to access an item in an array. It's just easier, quicker, and neater to code:
for i = 1 to 100 do something[i] = something_else[i][2]
than:
for my_var_1 = 1 to 100 do something[my_var_1] = something_else[my_var_1][2]
And, of course, this also means you have to invent fewer unique variable names. This is not a minor consideration when you are writing large programs.
15. Re: for puzzlement
- Posted by euphoric (admin) Jan 11, 2019
- 1259 views
Somebody catch me up: Why would you pre-declare your loop variables?
In many languages, you have to declare the type of the for variable, and some may just require that be done before the for loop. Euphoria can infer the type from values given to the loop.
Ah, OK... So you would have to declare it if the language could not infer it.
I was so immersed in the OE paradigm that I couldn't see outside of it.
16. Re: for puzzlement
- Posted by euphoric (admin) Jan 11, 2019
- 1265 views
Well, I never. Pre-declaring loop variables has worked for so long in phix that I forgot you cannot do that in OE.
Somebody catch me up: Why would you pre-declare your loop variables?
So you can look see how far the loop got before exiting, especially useful if it has multiple exit points, and/or if the whole point of the loop was just to set i anyway.
Ah, that makes sense.
The only other sensible alternative would be to pre-declare another variable say ifail and set it to i at the top of the loop, btdt.
What does "btdt" mean?
17. Re: for puzzlement
- Posted by euphoric (admin) Jan 11, 2019
- 1273 views
...the idea of having the loop variable automatically declared and visible only within the loop is a good one.
And, of course, this also means you have to invent fewer unique variable names. This is not a minor consideration when you are writing large programs.
Good points, irv. Thanks!
18. Re: for puzzlement
- Posted by petelomax Jan 11, 2019
- 1275 views
What does "btdt" mean?
been there, done that
19. Re: for puzzlement
- Posted by euphoric (admin) Jan 11, 2019
- 1276 views
What does "btdt" mean?
been there, done that
OooOoooh, dang it.