Live data from Hacker News

Why 1 && 2 == 2

blog.chewxy.com

41–50 of 58 posts

Re: Why 1 && 2 == 2

#41
post #25

Once you understand short-circuit evaluation, it opens up certain coding tricks. 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]" wi…

This is commonly seen in Perl and PHP with stuff like launchMissiles() or die('failure');

Ugh, `or` is mostly useless in PHP.

That code is identical to:

    launchMissiles() || die('failure');

Re: Why 1 && 2 == 2

#42
post #35
post #32

Earlier quoted context omitted.

[deleted]

Well, there is a difference between "format specifier" and "format string". The first argument to printf is the format string, the `%d` and `%s` type things are the format specifiers. I was talking about this printf("Hi", 45); which will not segfault. If you meant printf(45); Then you are absolutely correct, that would segfault on common machines.

Deleted my comment because you are, of course, correct. (However, I do suspect rayiner was referring to omitting the format string.)

Re: Why 1 && 2 == 2

#43
post #35
post #32

Earlier quoted context omitted.

[deleted]

Well, there is a difference between "format specifier" and "format string". The first argument to printf is the format string, the `%d` and `%s` type things are the format specifiers. I was talking about this printf("Hi", 45); which will not segfault. If you meant printf(45); Then you are absolutely correct, that would segfault on common machines.

I think that must be it. And it makes perfect sense how he would hit it. Example: http://ideone.com/MilaUN

Re: Why 1 && 2 == 2

#44
post #42
post #35

Earlier quoted context omitted.

Well, there is a difference between "format specifier" and "format string". The first argument to printf is the format string, the `%d` and `%s` type things are the format specifiers. I was talking about this printf("Hi", 45); which will not segfault. If you meant printf(45); Then you are absolutely correct, that would segfault on common machines.

Deleted my comment because you are, of course, correct. (However, I do suspect rayiner was referring to omitting the format string.)

I think so too, I just have bad reading comprehension and should have known but didn't.

Re: Why 1 && 2 == 2

#45
post #25

Once you understand short-circuit evaluation, it opens up certain coding tricks. 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]" wi…

This is commonly seen in Perl and PHP with stuff like launchMissiles() or die('failure');

The better form of this would be

    launchMissiles() and die('mutual annihilation");

Re: Why 1 && 2 == 2

#46
post #10
post #9

Earlier quoted context omitted.

He probably thought pointers are the only kind of variables and that he needed to dereference them. I imagine something like 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);

Nah mate, it's just some compiler flag that I didn't turn on (don't know which. my gcc-fu is fail since it's been really a long time since I touched C)

It's not a compiler flag, I guarantee it. Your code was simply incorrect. The only thing a compiler flag might have done was turn on a warning that would have told you what you did wrong (depending on what, exactly, you did do wrong).

Re: Why 1 && 2 == 2

#47
You wrote at the end that you weren't sure why literal values were returned instead of TRUE and FALSE. The practical answer is that literal values are far more useful. The simple answer is that, since the literal value already represents TRUE (or FALSE), returning it makes just as much sense as returning a literal TRUE/FALSE, and is simpler to do.

Re: Why 1 && 2 == 2

#48
post #31

I like how cout demonstrates the lameness of the whole C++ iostreams syntax. I'll have to remember that the next time an argument arises.

Feel free to change it to how you like it if you feel that another syntax would be more convenient. You can choose another operator or just create a variadic template function and use function syntax to avoid operator precedence issues.

  printf("%d", 2 && 1);
seems to work OK.

Re: Why 1 && 2 == 2

#49
post #22

For Python, a common idiom is to do this: condition and truecase or falsecase See "Boolean operations" and "Conditional expressions": http://docs.python.org/3/reference/expressions.html#boolean-...

ah! I've been trying to write Python lately (I'm more of a Rubyist) and the internet suggested: 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

I've never seen:

  $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.

Re: Why 1 && 2 == 2

#50
post #25

Once you understand short-circuit evaluation, it opens up certain coding tricks. 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]" wi…

This is commonly seen in Perl and PHP with stuff like launchMissiles() or die('failure');

It's worth noting that in Perl the 'or' and 'and' operators are not equivalent to || and &&. For instance, they have super low operator precedence:

  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. ^^;;
Post reply on HN