From: Keith Thompson on
Andrew Poelstra <apoelstra(a)localhost.localdomain> writes:
[...]
> Also, the idiomatic "count up to 10" loop is:
> for(i = 0; i < 10; ++i)
>
> while "count down to 10" is:
> for(i = 10; i >= 10; ++i)

I don't think that's what you meant.

[...]

--
Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
From: Andrew Poelstra on
On 2010-03-05, Keith Thompson <kst-u(a)mib.org> wrote:
> Andrew Poelstra <apoelstra(a)localhost.localdomain> writes:
> [...]
>> Also, the idiomatic "count up to 10" loop is:
>> for(i = 0; i < 10; ++i)
>>
>> while "count down to 10" is:
>> for(i = 10; i >= 10; ++i)
>
> I don't think that's what you meant.
>

:)

for(i = 10; i >= 0; ++i)

vi makes copy/paste too easy to do without looking at the
screen.

--
Andrew Poelstra
http://www.wpsoftware.net/andrew
From: Keith Thompson on
Andrew Poelstra <apoelstra(a)localhost.localdomain> writes:
> On 2010-03-05, Keith Thompson <kst-u(a)mib.org> wrote:
>> Andrew Poelstra <apoelstra(a)localhost.localdomain> writes:
>> [...]
>>> Also, the idiomatic "count up to 10" loop is:
>>> for(i = 0; i < 10; ++i)
>>>
>>> while "count down to 10" is:
>>> for(i = 10; i >= 10; ++i)
>>
>> I don't think that's what you meant.
>>
>
> :)
>
> for(i = 10; i >= 0; ++i)
>
> vi makes copy/paste too easy to do without looking at the
> screen.

So that's counting down *from* 10, not *to* 10 (speaking of
copy/paste).

Also, the "up to 10" loop executes 10 times, for values from 0 to 9
inclusive, while the "down from 10" loop executes 11 times, for values
from 10 down to 0 inclusive.

Finally, it's an infinite loop if i is of an unsigned type.

--
Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
From: Andrew Poelstra on
On 2010-03-05, Keith Thompson <kst-u(a)mib.org> wrote:
> Andrew Poelstra <apoelstra(a)localhost.localdomain> writes:
>> On 2010-03-05, Keith Thompson <kst-u(a)mib.org> wrote:
>>> Andrew Poelstra <apoelstra(a)localhost.localdomain> writes:
>>> [...]
>>>> Also, the idiomatic "count up to 10" loop is:
>>>> for(i = 0; i < 10; ++i)
>>>>
>>>> while "count down to 10" is:
>>>> for(i = 10; i >= 10; ++i)
>>>
>>> I don't think that's what you meant.
>>>
>>
>> :)
>>
>> for(i = 10; i >= 0; ++i)
>>
>> vi makes copy/paste too easy to do without looking at the
>> screen.
>
> So that's counting down *from* 10, not *to* 10 (speaking of
> copy/paste).
>
> Also, the "up to 10" loop executes 10 times, for values from 0 to 9
> inclusive, while the "down from 10" loop executes 11 times, for values
> from 10 down to 0 inclusive.
>
> Finally, it's an infinite loop if i is of an unsigned type.
>

Plus, I used in ++i instead of --i. My goodness.

In real life I always do:

i = 10; /* or max, or probably max - 1 */
while(--i)
...


--
Andrew Poelstra
http://www.wpsoftware.net/andrew
From: pete on
Andrew Poelstra wrote:
>
> On 2010-03-05, Keith Thompson <kst-u(a)mib.org> wrote:
> > Andrew Poelstra <apoelstra(a)localhost.localdomain> writes:
> >> On 2010-03-05, Keith Thompson <kst-u(a)mib.org> wrote:
> >>> Andrew Poelstra <apoelstra(a)localhost.localdomain> writes:
> >>> [...]
> >>>> Also, the idiomatic "count up to 10" loop is:
> >>>> for(i = 0; i < 10; ++i)
> >>>>
> >>>> while "count down to 10" is:
> >>>> for(i = 10; i >= 10; ++i)
> >>>
> >>> I don't think that's what you meant.
> >>>
> >>
> >> :)
> >>
> >> for(i = 10; i >= 0; ++i)
> >>
> >> vi makes copy/paste too easy to do without looking at the
> >> screen.
> >
> > So that's counting down *from* 10, not *to* 10 (speaking of
> > copy/paste).
> >
> > Also, the "up to 10" loop executes 10 times, for values from 0 to 9
> > inclusive, while the "down from 10" loop executes 11 times, for values
> > from 10 down to 0 inclusive.
> >
> > Finally, it's an infinite loop if i is of an unsigned type.
> >
>
> Plus, I used in ++i instead of --i. My goodness.
>
> In real life I always do:
>
> i = 10; /* or max, or probably max - 1 */
> while(--i)
> ...

To count down through an array with N elements, I use

size_t i = N;

while (i-- != 0) {
array[i];
}

--
pete
First  |  Prev  |  Next  |  Last
Pages: 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
Prev: integer
Next: shared memory question