K. Tsang CEng MBCS PMP PMI-ACP OCMJEA OCPJP
Guillermo Ishi wrote:I figured it was left to right with logical operators for a long time, until I realized there was a precedence. It's especially worth remembering that = has the lowest precedence of all. How'd you break both elbows? You must be a man of action
K. Tsang wrote:a && b | c is same as a && (b | c)
K. Tsang CEng MBCS PMP PMI-ACP OCMJEA OCPJP
K. Tsang wrote:Hmm I disagree.
Looking at a && b | c first. If short circuited, the b | c will not be evaluated right? If this holds as long as "a" is true, the output will be true.
K. Tsang wrote:Hmm I disagree.
Looking at a && b | c first. If short circuited, the b | c will not be evaluated right? If this holds as long as "a" is true, the output will be true. BUT
Roel De Nijs wrote:
K. Tsang wrote:Hmm I disagree.
Looking at a && b | c first. If short circuited, the b | c will not be evaluated right? If this holds as long as "a" is true, the output will be true.
WhyIf a is true, b | c will be evaluated, because the short-circuit AND operator will only result in true if both operands are true. So if the left operand evaluates to true, the right operand will be evaluated (and determine the outcome). If the left operand evaluates to false, then the right operand won't be evaluated (because it can never be true). In your example b | c evaluates to false, so that's why false is printed.
expr1 && expr2 represents a logical AND operation that employs short-circuiting behavior. That is, expr2 is not evaluated if expr1 is logical 0 (false). Each expression must evaluate to a scalar logical result.
expr1 || expr2 represents a logical OR operation that employs short-circuiting behavior. That is, expr2 is not evaluated if expr1 is logical 1 (true). Each expression must evaluate to a scalar logical result.
K. Tsang CEng MBCS PMP PMI-ACP OCMJEA OCPJP
nick woodward wrote:but that doesn't mean it evaluates (b | c) first, does it? it just means it has evaluated that part because && requires it.
nick woodward wrote:so if it was false && true | true, b and c would be false (if initially default values)
Guillermo Ishi wrote:For the sake of simplicity, how would you rewrite K. Tsang's table?
nick woodward wrote:yeah, sorry i meant if it was
a = false && b = true | c = true;
but yeah, I think I'm on the right page. that link you provided is decent, thanks.
nick woodward wrote:is it just me, or is there not a huge amount of discussion (in text books) of this distinction between precedence and evaluation order when using these operators?
nick woodward wrote:I know everyone is different, but to be honest for me this does not help make things clearer at all, however the rest of that thread is great, and in fact you explain very similar code very well in your post starting "it's hard, very hard". I'm now getting all the examples correct
nick woodward wrote:it appears that a && (b | c) IS correct for precedence, but that evaluation order trumps precedence. however it is still not the same as (a && b) | c. I'm going to look at your program (above) to see if this becomes a bit clearer.
nick woodward wrote:a && b | c does have the precedence a && (b | c) - but that isn't useful because it implies you work out (b | c) first, which isn't correct because the order of evaluation trumps the precedence.
nick woodward wrote:so i guess the easiest way to write the answer would be:
if(a){ b | c };
nick woodward wrote:I knew the results of all of your examples quickly before looking at your answers thanks to that previous thread, so I definitely understand now!
nick woodward wrote:And ok, by the looks of things your program is also printing the initial values of the booleans along with the result of the expression? fair enough!
nick woodward wrote:honestly, this website is invaluable - it's amazing how these topics often appear so simple and are described as such in the literature (less so in the certification books to be fair), but end up being quite complex. there are several topics I feel really quite strong on now, and it's largely down to this place, and in particular your posts roel! appreciated!
nick woodward wrote:have you ever considered a 'best of' thread? for example combining links to the threads you usually point posters to?
Roel De Nijs wrote:
That's something I haven't considered yet. Probably because I think only a few links will be in this "best of" thread. But could definitely be something to think about in the (near) future (when I have created a bunch other quality posts).
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|