problem with FOR statement (side effect on control variable) 
Author Message
 problem with FOR statement (side effect on control variable)

We use M2VMS 4.2-3 compiler for both vax and alpha. In a FOR statement,
when I modify the loop counter, the behaviour is not the same with the
OpenVMS Alpha modula-2 compiler and the modula-2 compiler for vax. What
is the problem ? do the modula-2 specifications allow the modification
of loop counter ? Is it compiler dependant ?
I test it with this program :

MODULE testboucle;

FROM InOut IMPORT
WriteCard, WriteString, WriteLn;

VAR i : CARDINAL;

BEGIN
 WriteString("Debut de la boucle de test ...");
 WriteLn;
 FOR i := 1 TO 10 DO
  WriteCard(i, 5);
  WriteLn;
  IF i = 5 THEN
   WriteString("Modification de la variable de contr?le ...");
   WriteLn;
   i := 10;
   WriteString("La variable de contr?le vaut prsent ");
   WriteCard(i,5);
   WriteLn;
  END;
 END;
 WriteString("Fin de la boucle de test ...");
 WriteLn;
END testboucle.

output on alpha openvms :
Debut de la boucle de test ...
1
2
3
4
5
Modification de la variable de contr?le ...
La variable de contr?le vaut prsent    10
6
7
8
9
10
Fin de la boucle de test ...

output on vax openvms :
Debut de la boucle de test ...
1
2
3
4
5
Modification de la variable de contr?le ...
La variable de contr?le vaut prsent    10
Fin de la boucle de test ...

Thank you for your help.



Mon, 24 Jun 2002 03:00:00 GMT  
 problem with FOR statement (side effect on control variable)

Quote:

> We use M2VMS 4.2-3 compiler for both vax and alpha. In a FOR statement,
> when I modify the loop counter, the behaviour is not the same with the
> OpenVMS Alpha modula-2 compiler and the modula-2 compiler for vax. What
> is the problem ? do the modula-2 specifications allow the modification
> of loop counter ? Is it compiler dependant ?

Both the language report in the back of "Programming in Modula-2" and
the ISO standard state that the control variable of a FOR loop should
not be modified by statements inside the loop. As you have found, you
get varying results if you try it, depending on which compiler you
use (and often what level of optimisation you are using). If you need
to exit a loop early, use a WHILE, REPEAT, or LOOP statement.

Martin



Mon, 24 Jun 2002 03:00:00 GMT  
 problem with FOR statement (side effect on control variable)

Quote:


> > We use M2VMS 4.2-3 compiler for both vax and alpha. In a FOR statement,
> > when I modify the loop counter, the behaviour is not the same with the
> > OpenVMS Alpha modula-2 compiler and the modula-2 compiler for vax. What
> > is the problem ? do the modula-2 specifications allow the modification
> > of loop counter ? Is it compiler dependant ?

> Both the language report in the back of "Programming in Modula-2" and
> the ISO standard state that the control variable of a FOR loop should
> not be modified by statements inside the loop.

It is a bit sad that compilers do not fault this if it is in fact not permitted.

I can't recall what PIM3 had to say on it, but surely the ISO formal language specification must give a fully defined
behaviour in the event that you do try to do this (in error) ? I would hope for a compile time error msg or warning but
perhaps I am being over optimistic.

Quote:
> As you have found, you get varying results if you try it, depending on which compiler you
> use (and often what level of optimisation you are using). If you need
> to exit a loop early, use a WHILE, REPEAT, or LOOP statement.

People do assign loop variables by accident/typo though.
Especially when the loop variable is a single character identifier.

Regards,
Martin Brown



Mon, 24 Jun 2002 03:00:00 GMT  
 problem with FOR statement (side effect on control variable)
Quote:
> I can't recall what PIM3 had to say on it, but surely the ISO formal

language specification must give a fully defined
Quote:
> behaviour in the event that you do try to do this (in error) ? I would

hope for a compile time error msg or warning but

Quote:
> perhaps I am being over optimistic.

ISO does define this as a compile time error. ISO defines very stringent
rules for how you can use a FOR loop control variable.

--
Best Regards,
Norman Black, Pcn
Stony Brook Software


Quote:




> > > We use M2VMS 4.2-3 compiler for both vax and alpha. In a FOR
statement,
> > > when I modify the loop counter, the behaviour is not the same with the
> > > OpenVMS Alpha modula-2 compiler and the modula-2 compiler for vax.
What
> > > is the problem ? do the modula-2 specifications allow the modification
> > > of loop counter ? Is it compiler dependant ?

> > Both the language report in the back of "Programming in Modula-2" and
> > the ISO standard state that the control variable of a FOR loop should
> > not be modified by statements inside the loop.

> It is a bit sad that compilers do not fault this if it is in fact not
permitted.

> I can't recall what PIM3 had to say on it, but surely the ISO formal

language specification must give a fully defined
Quote:
> behaviour in the event that you do try to do this (in error) ? I would

hope for a compile time error msg or warning but

- Show quoted text -

Quote:
> perhaps I am being over optimistic.

> > As you have found, you get varying results if you try it, depending on
which compiler you
> > use (and often what level of optimisation you are using). If you need
> > to exit a loop early, use a WHILE, REPEAT, or LOOP statement.

> People do assign loop variables by accident/typo though.
> Especially when the loop variable is a single character identifier.

> Regards,
> Martin Brown



Mon, 24 Jun 2002 03:00:00 GMT  
 problem with FOR statement (side effect on control variable)
I search a FREE modula-2 compiler for either alpha/openvms or
vax/openvms, or else pc/unix/linux/NT.
This compiler must be able to output a warning or error  if the module
contains a FOR statement with sides effects on loop variable. Do you
know such a compiler ?

Thank you for your precious help.



Tue, 25 Jun 2002 03:00:00 GMT  
 problem with FOR statement (side effect on control variable)

Quote:


> > Both the language report in the back of "Programming in Modula-2" and
> > the ISO standard state that the control variable of a FOR loop should
> > not be modified by statements inside the loop.

> It is a bit sad that compilers do not fault this if it is in fact not permitted.

The good ones do :-)

Quote:
> I can't recall what PIM3 had to say on it, but surely the ISO formal language specification must give a fully defined
> behaviour in the event that you do try to do this (in error) ? I would hope for a compile time error msg or warning but
> perhaps I am being over optimistic.

I don't have PIM3, but PIM2 just tells you not to do it, not what happens
if you do. The ISO standard contains a large number of rules about what
you can and can't do with a FOR loop control variable, which are there to
prevent you modifying it (or in some cases, to detect whether you might
modify it). Violating any of these rules should result in a compile time
error.

(this is one of the many reasons why the ISO standard is somewhat larger
than the PIM language report!)

Regards,

Martin



Fri, 28 Jun 2002 03:00:00 GMT  
 problem with FOR statement (side effect on control variable)

Quote:
> I search a FREE modula-2 compiler for either alpha/openvms or
> vax/openvms, or else pc/unix/linux/NT.
> This compiler must be able to output a warning or error  if the module
> contains a FOR statement with sides effects on loop variable. Do you
> know such a compiler ?

For Linux, try GPM. Otherwise any ISO compliant compiler should do,
but I don't know of a free one. See the FAQ for more info:

http://www.faqs.org/faqs/computer-lang/Modula2-faq/part1/

Regards,

Martin



Fri, 28 Jun 2002 03:00:00 GMT  
 problem with FOR statement (side effect on control variable)

Quote:



> > > Both the language report in the back of "Programming in Modula-2" and
> > > the ISO standard state that the control variable of a FOR loop should
> > > not be modified by statements inside the loop.

> > It is a bit sad that compilers do not fault this if it is in fact not permitted.

> The good ones do :-)

> > I can't recall what PIM3 had to say on it, but surely the ISO formal language specification must give a fully defined
> > behaviour in the event that you do try to do this (in error) ? I would hope for a compile time error msg or warning but
> > perhaps I am being over optimistic.

> I don't have PIM3, but PIM2 just tells you not to do it, not what happens
> if you do.

  PIM3 is the same, it says "Its value should not be changed by the
statement sequence".

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 01 Jul 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. (BUG) Unwanted side effect of format statement

2. local variables don't cause side effects

3. Dialogs and side effects

4. Skipping print preview side effect ArnĂ³r

5. Pattern for side effects

6. Side effects in pure functional languages

7. One advantage of computing by side-effect

8. Forth's popularity, side-effect

9. Side-effect free functions

10. Wrapping a side-effecting function library

11. Functions without side effects (was Old confusion)

12. INI file side effects

 

 
Powered by phpBB® Forum Software