Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!mcsun!unido!ira.uka.de!ifistg!bs3!schoebel From: schoebel@bs3.informatik.uni-stuttgart.de (Thomas Schoebel) Newsgroups: comp.lang.modula2 Subject: Re: Need Help With 'BY' Message-ID: <6892@ifi.informatik.uni-stuttgart.de> Date: 14 Jan 91 11:48:18 GMT References: <91003.102704BJ020000@NDSUVM1.BITNET> Sender: news@ifistg.uucp Organization: Informatik, Uni Stuttgart, W. Germany Lines: 49 In article josef@nixpbe.nixdorf.de (josef Moellers) writes: > > replace > > FOR Loop := Begin TO End BY By DO > > by > > Loop := Begin; > WHILE Loop <= End DO > > INC(Loop, By); > END; Danger! This will not work! If By is negative, the loop is intended to count backward and normally Begin > End. The above code will never execute the loop in this case! So the WHILE condition should read Loop>=End if By is negative, and Loop<=End otherwise. There are once again two possibilities: 1) Use two different WHILE loops with different conditions. But then you have to repeat the inside the WHILE body twice. Also, there is no advantage against using the FOR version. 2) Replace the While condition by Loop <> End. To get correct results, the End variable has to be modified one step before the WHILE loop begins. Also, the special case of never executing the FOR loop has to be treated separately. This code is: IF ((By>0) AND (Begin<=End)) OR ((By<0) AND (Begin>=End)) THEN INC (End, By); Loop:=Begin; WHILE Loop <> End DO INC (Loop, By); END; END; This version avoids to copy the twice and will give relative good performance. Thomas Thomas Sch"obel, Institut f"ur Informatik, Universit"at Stuttgart Internet: schoebel@ifi.informatik.uni-stuttgart.de Phone: +49 711 121-1409