S
Skarmander
No, I rather like formally precise tests. (Of course, if you screw them up,Frederick said:Is the domestic usage of the C "for" loop inefficient when it comes to
simple incrementation?
No.
Here's a very simple program that prints out the
bit-numbers in a byte.
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int main(void)
{
unsigned i;
for( i = 0; i != CHAR_BIT; ++i )
{
printf( "Bit Index %u\n", i );
}
system("PAUSE");
}
(Feel free to substitute "i != CHAR_BIT" with "i < CHAR_BIT".)
you're much more likely to get an infinite loop then your fellow programmer
who used an inexact comparison and got infinity as wiggle room.)
<snip>If I try to replicate that using "goto",
Don't. There are good reasons to use goto. This isn't one of them.
However, we can see that the very first conditional test is redundant --
it would be better to test the condition AFTER each iteration, rather
than before. Something like:
Then glance harder. There's nothing "inherently" inefficient about it. ForIf we compare the execution of both code snippets, we can see:
Snippet 1: Tests the condition 9 times
Snippet 2: Tests the condition 8 times
Is the widely used C method of simple loop incrementation inherently
inefficient? At first glance, it appears so to me.
most loops it's perfectly valid and desirable to potentially execute 0
times. A pretest loop will handle this; a posttest loop will not.
Then, as I said above, use do-while. You might as well communicate your(Yes, I realise that most compilers will "unroll" that loop, so lets
pretend we're working with a more complicated loop whose amount of
iterations isn't determined until runtime.)
knowledge (or assumption) about the loop's behavior to the esteemed maintainers.
Upthread, I see you don't want to use do-while because you like the way the
for-statement looks better. To that my educated response would be: tough
noogies. You can't have your cake and eat it too.
S.