#585 was: Quick update
Dirk Hohndel
dirk at hohndel.org
Thu Jul 3 08:40:14 PDT 2014
On Thu, Jul 03, 2014 at 04:36:15PM +0200, Robert Helling wrote:
>
> On 03.07.2014, at 15:58, Dirk Hohndel <dirk at hohndel.org> wrote:
>
> >> 1) A minor one: the “going back in time” is done as a loop with
> >>
> >> while (i && —i)
> >>
> >> where i is the index in the dive list. Here, it seems to me i==0 is a perfectly fine value, it is the first entry in the dive list. So the condition should rather be
> >>
> >> while (i >= 0 && --i >= 0)
> >
> > Yes, but that's equivalent to what we have.
> > while (i && --i) allows i = 1 which gets decremented to 0 before the loop
> > starts.
> >
> >> Could somebody please confirm/refute this?
> >
> > refute :-)
>
> Ahm. I thought the difference is whether the loop runs with the value
> i==0 (which is a perfectly fine number for an index of the dive list).
> Do you still hold to your refutation?
But of course. Here's the code:
divenr = get_divenr(dive);
when = dive->when;
i = divenr;
while (i && --i) {
struct dive *pdive = get_dive(i);
if we have a dive in the divelist, then get_divenr will give us a
non-negative divenr.
If divenr is 0 (first dive), then the while condition will FAIL because
there is no previous dive pdive that we could look at (--i will be -1)
If divenr is >0, then the while condition will be true and pdive will
indeed be the previous dive.
And if the divelist is empty (or dive is NULL) get_divenr will give us -1
and the while condition will also be false.
So to condens my answer: no, i=0 is NOT a valid number on which to enter
the loop as then --i turns it into -1 and pdive would become NULL. i=1 is
perfectly fine (second dive in the list), pdive will be get_dive(0) and
therefore the first dive.
The difference is that it's perfectly fine to have i=0 AFTER the --i, but
not before it...
Any more questions?
/D
More information about the subsurface
mailing list