Jump to content
Excelsior Forums
oo

Do Local Variables cease to exist after a For Loop?

Recommended Posts

Hi,

I am trying to get some old Top Speed Modula 2 Code to work. It had a lot of For-Loops where it used a global variable as the control variable. It also tried to assign the control variable inside the loop ? in order to make a quick exit out of the loop.

For example it had something of the form:

FOR i5:=0 TO Pbdim DO

IF (Something here) THEN i5:= Pbdim; END;

END

I have declared a new local variable i5_extra, and a Boolean breakLoop locally. I have modified my loop as

breakLoop:=FALSE;

FOR i5_extra:=0 TO Pbdim DO

IF breakLoop = FALSE THEN

        IF (Something here) THEN breakLoop:=TRUE; END;

        END;

END;

i5:=i5_extra;

My program crashes at the last line i.e. i5:=i5_extra; Is it because the local variable somehow disappears?? I feel that it should be in scope as it is defined for the entire procedure. I get the error of the form:

?RTS Exception #3: invalid location?

The above is an example ? my code is much longer than this.

Thanks for your help.

O.O.

Share this post


Link to post
Share on other sites

In such cases you better use any other loops WHILE, REPEAT and LOOP instead of FOR statement.

i5 := 0;

LOOP

  IF i5 > Pbdim THEN

    EXIT;

  END;

  (* some code here *)

  IF  (Something here) THEN

    EXIT;

  END;

  (* some code here *)

  INC(i5);

END

(* i5 has the last value here *)

Share this post


Link to post
Share on other sites

Two golden rules regarding FOR loops

The loop control variable of a FOR loop may not be threatened by code within the loop

At the conclusion of a Modula-2 FOR loop, the value of the loop control variable is undefined

As suggested use a WHILE loop

Regards

Share this post


Link to post
Share on other sites

Thanks to the two of you for your comments. I never expected that the variable would end up being undefined after the loop.

Just a last question before I consider making huge modifications to my code

Is there a way to prematurely exit from a For loop?

I know that Exit works to get out of a While loop. Does something analogous exist for a For loop.

Thanks a lot to you guys.

O.O.

Share this post


Link to post
Share on other sites

Some time ago I recommended to read http://www.modula2.org/tutor/index.php; in my opinion, this is very useful tutorial for the M2-beginners. You know, you can find the detailed information on loops statements there.  For example, http://www.modula2.org/tutor/chapter4.php "The value of the index must be assumed to be undefined after the loop terminates. You may discover that it is predictable on your compiler, but it may not be on some other compiler, and you may want to transfer your program to another system someday."  ;)

As I know, there is only one way to prematurely exit from the FOR statement - RETURN.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×