Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site fisher.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!princeton!astrovax!fisher!njt From: njt@fisher.UUCP (Nathaniel Thurston) Newsgroups: net.micro.mac Subject: Re: Megamax bug? Message-ID: <1436@fisher.UUCP> Date: Sat, 14-Jun-86 07:11:58 EDT Article-I.D.: fisher.1436 Posted: Sat Jun 14 07:11:58 1986 Date-Received: Tue, 17-Jun-86 21:21:00 EDT References: <1023@k.cs.cmu.edu> <219@analog.UUCP> Reply-To: njt@fisher.UUCP (Nathaniel Thurston) Distribution: net Organization: Princeton University.Mathematics Lines: 29 Summary: In article <219@analog.UUCP> kim@analog.UUCP (Kim Helliwell) writes: >> for (i = ntables; i--; ) >> if (i == -1) >> >> I would expect this to get the value of i, decrement the stored value, >> and then exit or execute the statement. Rather, it tests the value >> before it decrements, and the loop exits with i = 0. Is this a bug >> in the code generation, or is C supposed to do this? >> >> Greg > > for (i = ntables; ; i--) > if (i = -1) > >Kim Helliwell You're both wrong. The first loop should perform the action Greg describes, using the value before decrementing. To have C test the value after decrementing, use --i. In the "fix" that Kim gives, the loop will never exit without a break statement. The correct code is: for (i = ntables; --i; ) if (i == -1) -- Nathaniel Thurston UUCP: {allegra, astrovax, princeton, twg} !fisher!njt BELL: (609) 683-0543 USnail: 106 FitzRandolph Rd., Princeton, NJ 08544