• Popular Now

• 12
• 12
• 9
• 10
• 13

Archived

This topic is now archived and is closed to further replies.

(!( && !

This topic is 5531 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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]

Share on other sites
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]

Share on other sites
you cant compile that code if blah is not existent. however, if blah is a pointer, it can check if blah points to NULL.

Share on other sites
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)

Share on other sites
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)

Share on other sites
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 javascript, 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

Share on other sites
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)

Share on other sites
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

Share on other sites
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

Share on other sites
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)