In simple terms, && is true (1) if both sides of the expression returns NOT false (0).
/* These all return TRUE (1) */ if (4 && 5) return(); i=3; j=2; return( i && j);
The expression is evaluated 'Left to Right' If any part of the expression returns ZERO - The evaluation ends. This is called "short circuiting" and can cause serious problems if you don't understand it.
k=0; i=3; j=2; if ( i-i && j++) k=1
OR also evaluates 'Left to Right' and will stop when an expression returns true. SO WATCH OUT....
k=0; i=3; j=2; if ( i+i && j++) k=1
This can also be somewhat useful to do things like checking a pointer is not null before calling a routine that depends on a valid pointer, or doing a low cost condition first before doing a second condition which is more expensive to compute. e.g. if (0==a && f/123>2.3334) will execute faster than if (f/123>2.3334 && 0==a). However, because each condition is type changed into a boolean (1 or 0 only) before being returned, you can NOT do things like i = a || 1 to default i to 1 if a is zero, which works nicely in JavaScript, Perl, LUA, etc... In C or C++ i will be 1 no matter what a is, because a will be converted to true (1) if it isn't zero, and if it is zero, the 1 will be converted to true (1) and returned instead.
NOT reverses the logical state of its operand. If the operand is 0, 1 is returned, else 0 is returned.
!4 /* Returns 0 */ !-4 /* Returns 0 */ !1 /* Returns 0 */ !0 /* Returns 1 */
Top | Master Index | Keywords | Functions |
See also: