Why 1 && 2 == 2
blog.chewxy.com
blog.chewxy.com
(cout << 1) && 2;
...which is not an error, since the result of operator<< is an std::ostream, which is convertible to bool. Therefore operator&&(bool, bool) is invoked but the result is, of course, discarded.Disambiguating the expression with parentheses should output 1 in every case.
Thanks
condition and truecase or falsecase
See "Boolean operations" and "Conditional expressions":
http://docs.python.org/3/reference/expressions.html#boolean-...
Nowadays you would do this: truecase if condition else falsecase
Personally, I prefer the brevity of the older syntax, but to each his own. However, I understand the "ternary" expression was added because the older syntax was error-prone:
http://mail.python.org/pipermail/python-dev/2005-September/0... http://www.python.org/dev/peps/pep-0308/
truecase if condition else falsecase
which is so inside-out that it hurts.
It didn't occur to me to use the boolean style - I guess I think of that as a perl idiom, and assumed that it would get sneered at in py-land
$CONDITION && $TRUE_CASE || $FALSE_CASE
used as a Perl idiom in lieu of: $CONDITION ? $TRUE_CASE : $FALSE_CASE
I can't claim to have worked with Perl much prior to Perl 5.6/5.8 though, so it may have been an older idiom (though I doubt it as one of Perl's influences was Awk, which also has a C ternary operator, IIRC).That said, using truthy/falsey values in Python doesn't seem super common. I surprised a couple of interviewers by doing that while implementing some tree manipulation functions.
int *a = 1, *b = 2;
printf("%d\n", *a && *b);
Either that or he used `%s` as a format specifier. That seems more probable.Edit: Or, as I just realised the people below me meant,
printf(a && b);From the article, about the C version: "Mine hadn't worked, which I suspect is some sort of compiler issue."
Yeah I'm going to go out on a limb and proclaim that there's no way this was due to a compiler "issue" in gcc. Not that gcc is perfect, but there's no way it is barfing on one of these handful of line programs that does nothing but some logical operations and printfs.
Parts of the blog post was written late last night (about 3 am), and parts of it was written on my commute to work. I tried to rewrite what I wrote last night and tried to compile it on my work computer, and all I got were warnings.
So... can't recreate. Sorry
int main(void){ printf("1 && 2 = %d\n", 1 && 2); printf("1 || 2 = %d\n", 1 || 2); }
What were you doing?
The line "[operation] or [error condition]" will attempt the operation on the left. If it succeeds, the program continues on the next line, ignoring the part about the error condition. If it fails, the error condition will execute instead. This might be something like "open file or die".
Likewise, "[operation] and [success condition]" will attempt the operation on the left, and if it succeeds, perform the operation on the right. This might be something like "read input and process it", which will perform the (possibly expensive) processing step only when input is received.
Code written in this style is sometimes, but not always, faster to execute. It's also sometimes, but not always, clearer.
See also http://en.wikipedia.org/wiki/Short-circuit_evaluation
launchMissiles() or die('failure');That code is identical to:
launchMissiles() || die('failure');Also, using or instead or || for these types of things is more readable since it makes your intent of conditional code execution (as opposed to boolean evaluation) clear.
Considering 99.9% of it's use in PHP is some variant of "connect() or die()", I stand by my statement. Besides, those "connect() or die()" expressions are widely considered bad-practice in the first place.
launchMissiles() and die('mutual annihilation"); my $variable = 0 or 1;
Would result in $variable == 0 because it really looks like this: (my $variable = 0) or 1;
I allowed myself to get burned by this once because I didn't read the fine print. ^^;;EDIT: Oh, missed the errata.
http://www.lispworks.com/documentation/HyperSpec/Body/m_and....
Just be careful with this in JavaScript, as the greater number of things that are falsy can bite you if you do `a = a || 10;`, which is a very common JS bug.
echo 2 && 1; // 1
echo 1 && 2; // 1
echo 2 || 1; // 1
echo 1 || 2; // 1
echo 2 ?: 1; // 2
In php ?: is javascript's || Unfortunately there is no && that works correctly. $ php -r 'var_dump(1 && 2);'
bool(true)The odd bit is that casting a boolean to a string[0] with echo coerces the result to (string) "1" for true and (string) "" for false.
[0]: http://www.php.net/manual/en/language.types.string.php#langu...
Since 1d = 01b and 2d = 10b...
1 AND 2 = 01 AND 10 = 00 = 0
1 OR 2 = 01 OR 10 = 11 = 3
cout << 2 && 1
demonstrates the lameness of the whole C++ iostreams syntax. I'll have to remember that the next time an argument arises. printf("%d", 2 && 1);
seems to work OK.E.g. in Haskell, it would be possible to do this safely, because it is guaranteed by the type system that there are no side effects. However, even with literal 0 in the code the other operand still gets evaluated.