From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.7 required=5.0 tests=BAYES_00,INVALID_DATE, MSGID_SHORT,REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Xref: utzoo comp.lang.c:29537 comp.lang.ada:3951 Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!tut.cis.ohio-state.edu!pt.cs.cmu.edu!sei!firth From: firth@sei.cmu.edu (Robert Firth) Newsgroups: comp.lang.c,comp.lang.ada Subject: Re: bug me now / bug me later Keywords: philosophical differences Message-ID: <7498@fy.sei.cmu.edu> Date: 12 Jun 90 13:36:20 GMT References: <811@sagpd1.UUCP> Reply-To: firth@sei.cmu.edu (Robert Firth) Organization: Software Engineering Institute, Pittsburgh, PA List-Id: In article <811@sagpd1.UUCP> jharkins@sagpd1.UUCP (Jim Harkins) writes: >Which is better: > > a. for(i = SIZE; i != 0; i -= STEP) >or > b. for(i = SIZE; i > 0; i -= STEP) > >Where this makes a difference is suppose SIZE is changed to 7. Obviously >'a' goes into an infinite loop, while 'b' stops. In the real world SIZE and >STEP could be variables that have been input by a user and manipulated a >gazillion times before being used in the for loop. First, the coding of the loop should have nothing to do with the values of SIZE and STEP. The code you write should be determined by the postcondition you wish to establish. If the required postcondition is "i=0" then the correct continuation test is "i/=0"; on the other hand if the required postcondition is "i<=0" then the test is "i>0". This would all be much clearer in a language that allowed the condition to be written positively, thus: loop ... exit when i=0 end loop -- postcondition: i=0 Secondly, the question of errors. If the loop variable reaches a state from which the postcondition is unreachable, there is an error in the code. For example, if the postcondition is "i=0", the recurrence relation "i'