🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.modula2
Article #4203

Re: The semantics of the for-loop

#4203
From: Matthew Eric Seitz
Date: Sun, 17 Apr 1988 03:40
39 lines
1924 bytes
In article <5571@uwmcsd1.UUCP> markh@csd4.milw.wisc.edu (Mark William Hopkins) writes:
>
>     The best way out of the difficulties associated with the for-loop is
>to define it.  I would suggest the following definition:
>
>FOR I := A TO B DO       ==      I := A;
> <Statement Sequence>            WHILE I < B DO 
>END                                <Statement Sequence>;
>                                   I := I + 1
>                                 END;
>				 IF I = B THEN
>				   <Statment Sequence>
>                                 ENDIF
>
>This set-up makes I come out of the loop with the value B.

_Programming_In_Modula-2_ essentially defines the for statement as the following
equivalency

"FOR" ident ":=" expression1 "TO" expression2 [ "BY" ConstExpression ] "DO" 
	StatementSequence
"END"
is equivalent to

ident ":=" expression1;
"WHILE" ident "<=" expression2 "DO"
	StatementSequence
	[ident ":=" ident "+" ConstExpression (* if "BY" clause used *) |
	 ident ":=" ident "+ 1" (* if "BY" clause not used *) ]";"
"END"
(* ident := undefined *)

This means that you can replicate any FOR statement by a WHILE statement with
only two additional lines.  Since this is also more explicit and flexible than
a FOR loop, I've started using the WHILE loop exclusively.  I imagine Wirth
had similar reasons for dropping the FOR loop from Oberon.

					Matthew Seitz
					seitz@cory.berkeley.edu
Technical Details
Message-ID: <2455@pasteur.Berkeley.Edu>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!ncc!alberta!att-ih!att-cb!ucbvax!pasteur!cory.Berkeley.EDU!seitz
References: <5571@uwmcsd1.UUCP>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!ncc!alberta!att-ih!att-cb!ucbvax!pasteur!cory.Berkeley.EDU!seitz
From: seitz@cory.Berkeley.EDU (Matthew Eric Seitz)
Newsgroups: comp.lang.modula2
Subject: Re: The semantics of the for-loop
Message-ID: <2455@pasteur.Berkeley.Edu>
Date: 17 Apr 88 03:40:35 GMT
References: <5571@uwmcsd1.UUCP>
Sender: news@pasteur.Berkeley.Edu
Reply-To: seitz@cory.Berkeley.EDU.UUCP (Matthew Eric Seitz)
Organization: University of California, Berkeley
Lines: 39