Article View: net.micro.mac
Article #6071Re: Megamax bug?
From: Nathaniel Thurston
Date: Sat, 14 Jun 1986 11:11
Date: Sat, 14 Jun 1986 11:11
29 lines
1692 bytes
1692 bytes
In article <219@analog.UUCP> kim@analog.UUCP (Kim Helliwell) writes:
>> for (i = ntables; i--; ) <statement>
>> if (i == -1) <more statements>
>>
>> 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--) <statement>
> if (i = -1) <more statements>
>
>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; ) <statement>
if (i == -1) <more statements>
--
Nathaniel Thurston
UUCP: {allegra, astrovax, princeton, twg} !fisher!njt
BELL: (609) 683-0543
USnail: 106 FitzRandolph Rd., Princeton, NJ 08544
Technical Details
Message-ID:
<1436@fisher.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!princeton!astrovax!fisher!njtReferences:
<1023@k.cs.cmu.edu> <219@analog.UUCP>Show Full Headers
Complete Article Headers
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: arc1-ca.pugleaf.net!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