Article View: comp.lang.modula2
Article #4208Re: The semantics of the for-loop
From: chrisj
Date: Wed, 20 Apr 1988 06:25
Date: Wed, 20 Apr 1988 06:25
30 lines
1913 bytes
1913 bytes
In 5099@aw.sei.cmu.edu, firth@sei.cmu.edu (Robert Firth) quotes an earlier
article about the semantics of FOR loops and says:
>... thereby perpetuates the design or implementation error that
>has cursed FOR-loops since the days of Algol-60: a loop set to end on the
>largest value of the associated type will fail.
>
>This is 1988. Let's at least put THAT right!
This is purely an implementation issue. MacMETH does it correctly, as
I'm sure some other compilers do. At the bottom of the loop, MacMETH
generates an ADDI or SUBI, followed by either a BVC (INTEGER or LONGINT)
or a BCC back to the top of the loop. Note that this correctly handles
FOR u := 0 TO MAX(CARDINAL) BY 2 DO ... END;
Stopping on >= before the increment, rather than > after the increment,
would not help in this case, although I suppose the generated code could
decrement the limit by (stepsize-1) before the loop, and then use a >=
test before increment.
If your compiler doesn't handle these limit situations correctly, gripe
at the vendor until they fix it. (Hi, Eli! :-))
Of course this means that a FOR loop is not EXACTLY equivalent to the
corresponding WHILE, since the WHILE version will either ignore the
carry/overflow or abort execution, depending on the implementation.
Christopher Jewell | If God had meant for us to fly, He
chrisj@cup.portal.com | would NEVER have given us the railways.
ucbvax!sun!cup.portal.com!chrisj | -- Flanders & Swan
Technical Details
Message-ID:
<4639@cup.portal.com>Path:
arc1-ca.pugleaf.net!utzoo!mnetor!uunet!portal!cup.portal.com!chrisjReferences:
<5571@uwmcsd1.UUCP> <2455@pasteur.Berkeley.Edu> <5099@aw.sei.cmu.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!portal!cup.portal.com!chrisj From: chrisj@cup.portal.com Newsgroups: comp.lang.modula2 Subject: Re: The semantics of the for-loop Message-ID: <4639@cup.portal.com> Date: 20 Apr 88 06:25:13 GMT References: <5571@uwmcsd1.UUCP> <2455@pasteur.Berkeley.Edu> <5099@aw.sei.cmu.edu> Organization: The Portal System (TM) Lines: 29 XPortal-User-Id: 1.1001.2041