Alf said:
* (e-mail address removed):
Generally you should simply refrain from coding loops that
count down. Let them count up. Then compute whatever value
you require.
It depends. Generally, you should use the most natural form.
If the loop is "do n times", where the index is not used, the
most natural form is to use the number of times remaining to be
done as the loop invariant, which results in something like:
for ( int count = N ; count > 0 ; -- count ) ...
However, if you absolutely must, then do it in the most
straightforward way possible.
Not offset by 1 or other ingenious workarounds for
non-existent problems, just directly
for( size_t i = m; i != size_t(-1); --i ) { ... }
assuming you want i to have the values m, m-1, m-2, ... 2, 1,
and 0.
That's a nice solution too, although it hadn't occurred to me.
Probably because the only times I use size_t is when I'm
dealing with a container, and if I'm not using iterators, then
it's because I need the index. And as you say, when dealing
with indexes, it's almost always more natural to start at the
beginning.
Btw., all-uppercase names such as M should not be used except
for macros.
Or, when posting code, to represent arbitrary (constant) values?
(I don't know, but it's a convention I've seen occasionally. It
doesn't mean that such a symbol will occur in an actual
program.)
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See
http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]