Re: Correct C++ tutorial part 3 "Intro to loops" available (Windows, mingw/msvc/std)

From: Attila Feher (attila.feher_at_lmf.ericsson.se)
Date: 01/27/05


Date: Thu, 27 Jan 2005 15:28:45 +0200

Alf P. Steinbach wrote:
[SNIP]
>>> * If you use integer update you should use integer type for the
>>> variable.
>>
>> Why? You have introduced your lines with the word reasoning, but I
>> see no reason told for this statement.
>
> It seems to boil down to two issues: why it's not a good idea to use a
> misleading type (maintainability), and whether a floating point type
> can always represent all values of an integral type exactly
> (technical).

Why would a floating point type misleading if I need a floating point value?
Furthermore - on the same note - why would I care about it representing or
not integer values, if I needed a floating point one in the first place?

[SNIP]
> Yes, to the latter two sentences. I wonder whether you are happy with
> the resolution of that issue as it turned out in the rest of the
> thread? That is, adding the parenthetical expression "unless you
> really know what you're doing"?

That is one way to put it. :-) But I guess there is another and probably
more important reason/cause: do you need a floating point iteration or not?
:-) And then comes the know what you do, do what you know. ;-)

[SNIP]
> Iterators and pointers as loop control variables is a good point,
> thanks.

No problemo. :-)

> The discussion above was trapped in the implicit context of numeric
> loop control variables.

And it possibly should for that very-beginner part, but IMHO the rule itself
may need to be worded differently. So that it suggests to the reader that
the rules is supposed to say: do not use floating point unless you really
need it (IMHO this goes not only for loops), so basically tell what is
wrong, instead of identifying what is right. Because limiting the possible
types (in this case/stage) will rule out valid cases, valid cases you did
not introduce yet for a reason.

> Possibly I should add the word "numeric" in the advice. But I think
> it would then hang in thin air, not related to the rest of the
> discussion. Suggestions?

What I wrote above could probably be worded.

Loops can do (at least) counting and iterating, not taking into account the
"abuses" of them, like when a do {} while(0); loop (with breaks inside) is
used instead of a local try-catch...

So the loop variable in those non-abuse cases can mean a numeric value
(counting), and index value (addressing into containers using the array
access) or an iterator. A loop walking a linked list (with the loop
variable being a pointer to one list element) is (IMHO) a special case of
iteration.

My point is: there can be numerous other (legal and "good design") uses of
loops, which the current rule rules out. If the rule could be worded to
only say what not to do, the "unknown number of other legal uses" would not
be affected by it.

--
Attila aka WW


Relevant Pages

  • Re: [PATCH 0/24] make atomic_read() behave consistently across all architectures
    ... iteration through the loop, unless there's a compelling reason not to. ... but hardware-independent core subsystems probably have no ...
    (Linux-Kernel)
  • Re: Iteration in lisp
    ... Unless you have a very good reason for reinventing your own list ... iteration, you should use what's already in the Common Lisp library. ... one possibility is a loop. ...
    (comp.lang.lisp)
  • Yow! LOOP macros are LOOPY!
    ... By relying entirely on procedure calls to express iteration, ... to but cleaner than C's FOR loop. ... other macros going around at the time other than MacLisp's ... (bind (vi (vector-ref v i))) ...
    (comp.lang.scheme)
  • Re: Polling, Interrupts, DMA, Synchronous, Asynchronous I/O Definitions
    ... the terminology is less useful than it might be. ... though a "message loop" could arguably be claimed to be ... considering in a particular iteration but what is true for _ALL_ ... watching the "polling" version eating up every single CPU cycle ...
    (alt.lang.asm)
  • Re: Histogram of character frequencies
    ... generated object code may simply be a loop in which elements are ... believe any C compiler anywhere would reject it. ... On the first iteration of the loop you test the end of file indicator ...
    (comp.lang.c)