Article View: comp.lang.modula2
Article #1691Re: Need Help With 'BY'
From: Thomas Schoebel
Date: Mon, 14 Jan 1991 11:48
Date: Mon, 14 Jan 1991 11:48
49 lines
1965 bytes
1965 bytes
In article <josef.663236830@ugum01> josef@nixpbe.nixdorf.de (josef Moellers) writes:
>
> replace
>
> FOR Loop := Begin TO End BY By DO
>
> by
>
> Loop := Begin;
> WHILE Loop <= End DO
> <statements>
> 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 <statements> 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
<statements>
INC (Loop, By);
END;
END;
This version avoids to copy the <statements> 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
Technical Details
Message-ID:
<6892@ifi.informatik.uni-stuttgart.de>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!mcsun!unido!ira.uka.de!ifistg!bs3!schoebelReferences:
<91003.102704BJ020000@NDSUVM1.BITNET> <josef.663236830@ugum01>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!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> <josef.663236830@ugum01> Sender: news@ifistg.uucp Organization: Informatik, Uni Stuttgart, W. Germany Lines: 49