[Info-vax] decrementing & for loops in C
Bob Eager
rde42 at spamcop.net
Thu Jan 8 09:57:38 EST 2009
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.
> 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.
--
Bob Eager
More information about the Info-vax
mailing list