[Info-vax] decrementing & for loops in C
Bill Gunshannon
billg999 at cs.uofs.edu
Thu Jan 8 09:17:02 EST 2009
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.
I think the answer is to explain in more detail what is wrong with the
first one.
for (i = n; i != 0; i--)
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. 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.
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