Archived

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

Tazel

(!( && !

Recommended Posts

Tazel    122
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 this post


Link to post
Share on other sites
Sneftel    1788
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Tazel    122
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 this post


Link to post
Share on other sites
Palidine    1315
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 this post


Link to post
Share on other sites
felisandria    739
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 this post


Link to post
Share on other sites
felisandria    739
You understand Order of Operations, right Tazel? You know that

2 + 1 * 6 is different than
(2 + 1) * 6 ?

We just added some more operations into the order of operations, that you might not be used to. So, when in doubt, use more parenthesis. Or look up "operator precedence" and see what tier it''s on.

-fel

Share this post


Link to post
Share on other sites
Thunder_Hawk    314
This is why, if you feel the need to use macros, you must make liberal use of parentheses in the macro if it starts to gain much any kind of complexity, unless it''s the intended behaviour.

Share this post


Link to post
Share on other sites
Stoffel    250
!(a && b) is equivalent to (!a || !b)

Knowing a little boolean logic can get you pretty far in programming. Example:

(a || (!a && b)) is equivalent to (a || b), and easier to read if using long variable names.

Think Liberally..

Share this post


Link to post
Share on other sites
Tazel    122
Fel: Yes. The parentheses and any grouping symbol''s contents are evaluated first.

Then powers.

Then multiplication and division from left to right.

Then add. and sub. from left to right.



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

Share this post


Link to post
Share on other sites