1. for puzzlement

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?

new topic     » topic index » view message » categorize

2. Re: for puzzlement

Addendum:
Changing the name in Main to rn_number did not solve the problem.

new topic     » goto parent     » topic index » view message » categorize

3. Re: for puzzlement

You write a declaration; it is in the top-level of your file.

first rn_nmbr

alrobnett said...

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

alrobnett said...


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

alrobnett said...

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.

alrobnett said...

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

new topic     » goto parent     » topic index » view message » categorize

4. Re: for puzzlement

alrobnett said...

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 
new topic     » goto parent     » topic index » view message » categorize

5. Re: for puzzlement

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

new topic     » goto parent     » topic index » view message » categorize

6. Re: for puzzlement

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.

new topic     » goto parent     » topic index » view message » categorize

7. Re: for puzzlement

Well, I never. Pre-declaring loop variables has worked for so long in phix that I forgot you cannot do that in OE.

new topic     » goto parent     » topic index » view message » categorize

8. Re: for puzzlement

petelomax said...

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

new topic     » goto parent     » topic index » view message » categorize

9. Re: for puzzlement

Not important but what is the significance of the 'O' in 'OE'?

new topic     » goto parent     » topic index » view message » categorize

10. Re: for puzzlement

alrobnett said...

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

new topic     » goto parent     » topic index » view message » categorize

11. Re: for puzzlement

petelomax said...

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?

new topic     » goto parent     » topic index » view message » categorize

12. Re: for puzzlement

euphoric said...

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

new topic     » goto parent     » topic index » view message » categorize

13. Re: for puzzlement

euphoric said...
petelomax said...

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

new topic     » goto parent     » topic index » view message » categorize

14. Re: for puzzlement

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.

new topic     » goto parent     » topic index » view message » categorize

15. Re: for puzzlement

ghaberek said...
euphoric said...

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. grin

new topic     » goto parent     » topic index » view message » categorize

16. Re: for puzzlement

petelomax said...
euphoric said...
petelomax said...

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.

petelomax said...

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? grin

new topic     » goto parent     » topic index » view message » categorize

17. Re: for puzzlement

irv said...

...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!

new topic     » goto parent     » topic index » view message » categorize

18. Re: for puzzlement

euphoric said...

What does "btdt" mean? grin

been there, done that

new topic     » goto parent     » topic index » view message » categorize

19. Re: for puzzlement

petelomax said...
euphoric said...

What does "btdt" mean? grin

been there, done that

OooOoooh, dang it. grin

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu