Path: 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 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; > WHILE I < B DO >END ; > I := I + 1 > END; > IF I = B THEN > > 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