Re: for puzzlement

new topic     » goto parent     » topic index » view thread      » older message » newer message

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.


new topic     » goto parent     » topic index » view thread      » older message » newer message


Quick Links

User menu

Not signed in.

Misc Menu