Faster for() loops? - Page 6

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Re: Faster for() loops?

Quoted text here. Click to load it
common
over
tests
out

The use of a boolean flag as the target parameter of a boolean expression is
of course completely appropriate.  What I was whining about (I admit it is a
pointless whine) was using an arithmetic object, as in if(--index).  What
that really means is "if the decremented index now equals 0" and just takes
advantage of the fact that the if operator (or while operator) in C is
hinged on the 0 value (from the ubiquitous JMPZ (or similar) operator, I
would suppose).  While it is correct in C grammar, and will "always work",
it is not clear what you mean by that.  The longer statement if(--index ==
0) (or in better style, if (0 == --index)) gets the same result, possible in
the same amount of machine code, and is explicit about its meaning.

As for pointers:  I was not talking about decrementing pointers, but their
use in while( and if( statements as above.  When you write

    if(ptr)
    {
        //do something
    }

what you really mean is

    if(ptr != NULL)
    {
        //do something
    }



Re: Faster for() loops?
Quoted text here. Click to load it
<snip>

I'd agree the TRUE and FALSE instance are pedantic, but THIS form

    if ( boolean_flag == RlyON )

can be safer and simpler, as any downstream logic inversion is
handled transparently.
  Here, you have a common define setting RlyON is active Hi or Lo,
but the code is always clearer. ( and that may change with
PCB revision, for example )

Also note that not all compilers treat these the same !!

   if ( boolean_flag == TRUE )
   if ( boolean_flag )

-jg


Re: Faster for() loops?
Quoted text here. Click to load it

I prefer to compare Boolean values against zero or FALSE.

    if (boolean_flag != 0)

means just exactly the same thing as

    if ( boolean_flag )

and should be easy enough to read.

--
pete

Re: Faster for() loops?
Quoted text here. Click to load it
common
Quoted text here. Click to load it
over
Quoted text here. Click to load it
tests
Quoted text here. Click to load it

Actually, with values of a boolean nature,
I prefer to do it Steve's way:

#include <ctype.h>

int a_toi(const char *nptr)
{
    int n;

    n = 0;
    while (isspace(*nptr)) {
        ++nptr;
    }
    if (*nptr != '-') {
        if (*nptr == '+') {
            ++nptr;
        }
        while (isdigit(*nptr)) {
            n = 10 * n - '0' + *nptr++;
        }
    } else {
        ++nptr;
        while (isdigit(*nptr)) {
            n = 10 * n + '0' - *nptr++;
        }
    }
    return n;
}


--
pete

Re: Faster for() loops?
Quoted text here. Click to load it

I'll ignore, for now, the built-in type _Bool and the standard header
<stdbool.h> introduced in C99.

A value used as a condition in C is treated as false if it's equal to
zero, true if it's non-zero.  It's not 0 vs. 1, it's 0 vs. anything
other than 0.

If your variable "boolean_flag" is something other than zero-is-false,
non-zero-is-true, it shouldn't be called "boolean_flag".

Assuming boolean_flag is a proper condition, "boolean_flag == TRUE"
isn't just pedantic, it's positively dangerous.  The relational
operators always yield 0 or 1, but other boolean expressions can have
any non-zero value; for example, the is*() functions in <ctype.h>
return 0 for false, but can return any arbitrary non-zero value for
true.

Quoted text here. Click to load it

Any compiler that does treat them the same is badly broken (unless the
compiler is able to prove that the value of boolean_flag is either 0
or 1).

This is a different issue from using something that's not a
boolean flag as a condition, such as "if (!ptr)" rather than
"if (ptr == NULL)", or "if (count)" rather than "if (count != 0)".
I prefer the more explicit comparison, but both forms are perfectly
valid and equally safe.

See also section 9 of the C FAQ.

--
Keith Thompson (The_Other_Keith) snipped-for-privacy@mib.org  <http://www.ghoti.net/~kst
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst
We've slightly trimmed the long signature. Click to see the full one.
Re: Faster for() loops?
Quoted text here. Click to load it

Exactly my point. Thanks for making it clearer than I managed to ;).

Steve
http://www.fivetrees.com




Re: Faster for() loops?

Quoted text here. Click to load it
<http://www.ghoti.net/~kst
Quoted text here. Click to load it
<http://users.sdsc.edu/~kst
Quoted text here. Click to load it



Site Timeline