[Info-vax] decrementing & for loops in C
Bill Gunshannon
billg999 at cs.uofs.edu
Thu Jan 8 10:42:01 EST 2009
In article <176uZD2KcidF-pn2-hvhm4U3ZxO4w at rikki.tavi.co.uk>,
"Bob Eager" <rde42 at spamcop.net> writes:
> On Thu, 8 Jan 2009 14:17:02 UTC, billg999 at cs.uofs.edu (Bill Gunshannon)
> wrote:
>
>> In article <176uZD2KcidF-pn2-KPYqms0nkLMf at rikki.tavi.co.uk>,
>> "Bob Eager" <rde42 at spamcop.net> writes:
>> > On Thu, 8 Jan 2009 13:07:17 UTC, FrankS <sapienza at noesys.com> wrote:
>> >
>> >> The conditional equivalance operator in C is == not =.
>> >>
>> >> Your syntax error occurs in the FOR statement. You have:
>> >>
>> >> for (i = n; i = 0; i--)
>> >
>> > No, it's syntactically correct. Not what is wanted, perhaps, but
>> > syntactically correct.
>> > There is a semantic error if a countdown loop is wanted.
>> >
>> >> but it should be:
>> >>
>> >> for (i = n; i == 0; i--)
>> >
>> > No....it should be:
>> >
>> > for (i = n; i != 0; i--)
>> >
>> > (see my other post)
>>
>> Or it could be:
>> for (i = n; i == 0; i--)
>>
>> which would work but is probably questionable stylisticaly.
>
> No. That has a different meaning. The 'i == 0' means (in this case)
> 'continue the loop while i is zero' - which it isn't to start with.
>
>> I think the answer is to explain in more detail what is wrong with the
>> first one.
>>
>> for (i = n; i != 0; i--)
>
> That isn't the first one.
Oops. Typo on my part. I thought I had fixed it from the cut+paste.
But I think we all knew what was first. :-)
>
>> In a C For loop the first part sets up the initial condition.
>> In this loop i is set to the value of n which is 10. Good so far.
>>
>> I will skip to the third part. This sets the action to be performed
>> until some defined condition is met. In this case, decrement i.
>>
>> And now the important part, where the logic, not syntax, error occurs.
>>
>> The second part specifies the end condition.
>
> NO! It specifies the condition which, when no longer true, causes the
> loop to terminate.
>
>> When this conditionis met
>> the result is a logical "true" and the loop ends. Here we have:
>>
>> i = 0;
>>
>> Note, that is the assignment operator in C and not the comparison operator.
>> The result is to set i to 0. This operation is, of course, successful and
>> results in returning a logical "true", thus, the loop is terminated. the
>> for loop did exactly as you asked it to do. All C compilers will treat it
>> exactly the same.
>
> I agree, and understood that. My point was that you have the loop
> termination condition back to front.
>
True, I appologize. I was trying to concentrate on the use of the
assignment operator instead of the comparison operator and missed
the general logic mistake completely.
But in any case, use of the assignment operator is always going to
cause the loop to end and would likely cause the loop to be optimized
out of the program entirely. Maybe I will try some of the compilers I
have to see if one of them actually does it.
bill
--
Bill Gunshannon | de-moc-ra-cy (di mok' ra see) n. Three wolves
billg999 at cs.scranton.edu | and a sheep voting on what's for dinner.
University of Scranton |
Scranton, Pennsylvania | #include <std.disclaimer.h>
More information about the Info-vax
mailing list