(!( && !

Started by
13 comments, last by Tazel 21 years, 2 months ago
Do these things mean the same thing:

if(!(blah))
return(0);
  
and

if(!blah)
return(0);
 
BTW, if(blah) is checking whether or not blah is existant? [edited by - Tazel on January 28, 2003 4:46:18 PM]
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
Advertisement
They mean the same thing. They are each checking whether the value in "blah" is equal to 0.

Don't listen to me. I've had too much coffee.

[edited by - sneftel on January 28, 2003 4:49:40 PM]
you cant compile that code if blah is not existent. however, if blah is a pointer, it can check if blah points to NULL.
It won''t compile? Then why do error checking that way?


tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
Never mind. I just saw Sneftel said that it''s checking if it''s FALSE.


tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
this can be more complicated that you think

first of all, if(!(blah)) and if(!blah) should be exactly the same, unless you have relatively rare case that blah has been #defined for the preprocessor, in which case the substitution might confuse you:

example:

bool foo;
extern bool myfunc(int n);

#define blah foo && myfunc(4)

now, "if(!blah)" expands to "if(!foo && myfunc(4))", but "if(!(blah))" expands to "if(!(foo && myfunc(4)))"


now, the other situation, testing whether blah is defined...
if you are using &#106avascript, then I think that is what this does, but otherwise, there are two slightly different cases:

in C, if(blah) "converts" blah to a number, and evaluates the guarded code if the number is nonzero (if the number is 0 then it evaluates the "else" clause)... so, if blah is a number type, then the normal test case happens, if blah is a pointer type, the same thing, the pointer is treated as a number (so the guarded code is executed if the pointer is not NULL))

in C++, the semantics fall back to C, to maintain backwards compatibility, but if blah is a variable of type "bool", or another type which has defined an implicit conversion to bool, then it uses this bool value


now, "if(!blah)" expands to "if(!foo && myfunc(4))", but "if(!(blah))" expands to "if(!(foo && myfunc(4)))"

Again, wouldn''t that be the same thing?


tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
quote:Original post by Tazel
Again, wouldn''t that be the same thing?


no.

"if(!foo && myfunc(4))" means if foo is false and myfunc(4) is true then execute the following logic


"if(!(foo && myfunc(4)))" means if BOTH foo and myfunc(4) are false then execute the following logic.

those are quite different

-me


No. You have to use order of operations.

"if(!blah)" expands to "if(!foo && myfunc(4))"

would resolve to "if foo is not true, and myfunc(4) is true"

"if(!(blah))" expands to "if(!(foo && myfunc(4)))"

would resolve to "the opposite of the result of checking if foo and myfunc(4) are both true".

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
I see. So that happens when there are more than one things being evaluated.


tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)

This topic is closed to new replies.

Advertisement