logical-operators

Java logical operator (&&, ||) short-circuit mechanism

最后都变了- 提交于 2019-12-05 12:13:52
问题 As I was reading a colleague's Java code, I stumbled upon an army of if/else statements. In these statements, several && and || operators were fighting each other without any help from parenthesis. I simplified the statements into: if (true || true && false) return true; else return false; What do you think the result would be? Honestly, I thought it would be false , but it seems short-circuiting doesn't work like I expected. In this case, the result is true . The short-circuit mechanism

Javascript: false || undefined vs undefined || false

狂风中的少年 提交于 2019-12-05 12:05:40
What is the explanation for behavior of the "||" operator (logical OR), when using it with false and undefined on both sides in JavaScript? 1) > false || undefined undefined 2) > undefined || false false The logical OR operator isn't commutative like + , * , etc. It returns the first expression which can be converted into true . (Source Mozilla Doc ) In false || undefined , false can't be converted to true by definition (since it's the opposite), so it returns the second operand ( undefined ) In undefined || false , undefined is a value, but considered as false in Javascript, so the logical

In Ruby, should we always use “&&”, “||” instead of “and”, “or” unless for special situations? [closed]

◇◆丶佛笑我妖孽 提交于 2019-12-05 10:58:57
Is it true that in most cases, in Ruby, it is best to use && , || instead of and , or , unless it is some special situations. I think one of Ruby's design principles is to have least surprises as possible, so using and , or or actually have some surprises... such as and not having a higher precedence than or , while && has a higher precedence than || . So I think in most cases, use && , || . In know in some special situations, it may require using and , or , but I think if those are intermixed with && , || , sooner or later it may create bugs when your coworkers who started in Ruby not so long

Logical AND strictness with IO monad

不羁的心 提交于 2019-12-05 08:27:22
I am trying to write a simple program in Haskell. It should basically run two shell commands in parallel. Here is the code: import System.Cmd import System.Exit import Control.Monad exitCodeToBool ExitSuccess = True exitCodeToBool (ExitFailure _) = False run :: String -> IO Bool run = (fmap exitCodeToBool) . system main = liftM2 (&&) (run "foo") (run "bar") But command "foo" returns ExitFailure and I expect "bar" never to run. This is not the case! They both run and both show errors on the console. At the same time False && (all (/= 0) [1..]) evaluates perfectly well; this means the second

Javascript logical operators and results

。_饼干妹妹 提交于 2019-12-05 05:49:10
问题 I know that the result of logical operations in most of the languages is either true, false or 1,0. In Javascript I tried the following: alert(6||5) // => returns 6 alert(5||6) // => returns 5 alert(0||5) // => returns 5 alert(5||0) // => returns 5 alert(5||1) // => returns 5 alert(1||5) // => returns 1 alert(5&&6) // => returns 6 alert(6&&5) // => returns 5 alert(0&&5) // => returns 0 alert(5&&0) // => returns 0 alert(-1&&5) // => returns 5 alert(5&&-1) // => returns -1 So what is the result

What kind of syntactic sugar is available in Perl to reduce code for l/rvalue operators vs. if statements?

北城以北 提交于 2019-12-04 16:32:49
问题 There's a bunch out there, as Perl is a pretty sugary language, but the most used statements in any language is the combination of if statements and setting values. I think I've found many of them, but there's still a few gaps. Ultimately, the goal would be to not have to write a variable name more than once: Here's what I have so far: $r ||= $s; # $r = $s unless ($r); $r //= $s; # $r = $s unless (defined $r); $r &&= $s; # $r = $s if ($r); $r = $c ? $s : $t; # if ($c) { $r = $s } else { $r =

How to write “if x equals 5 or 4 or 78 or…” in C

帅比萌擦擦* 提交于 2019-12-04 14:06:34
I have a quick question about using logical operators in an if statement. Currently I have an if statement that checks if x equals to 5 or 4 or 78: if ((x == 5) || (x == 4) || (x == 78)) { blah } And I was wondering if I could just condense all that to: if (x == 5 || 4 || 78) { blah } Sorry for such a basic question, I've just started learning C. There is no shortcut, but you need to fix your equality operator. if ((x == 5) || (x == 4) || (x == 78)) { First, you're using assignments not equality tests in your ifs. The first method (with suitable substitutions for equality) is the best way to

What is this assignment construct called? And can you do it in Php?

瘦欲@ 提交于 2019-12-04 14:00:19
I have often used the following construct in Javascript: var foo = other_var || "default_value"; In Javascript, if the left side is falsy, then the value on the right side is assigned. It is very handy, and saves writing longer and unnecessarily explicit ternary expressions. Is there a name for this sort of construct ? Bonus: Is there a trick to do this in Php without using a ternary operator? PS: another variant is to throw an error if you don't get a truthy value, instead of giving a default value: var foo = something || alert("foo is not set!"); The logical-or (usually || ) operator is

Why are logical operators in JavaScript left associative?

霸气de小男生 提交于 2019-12-04 08:54:27
问题 The logical AND and OR operators are the only lazy operators in JavaScript along with the ternary conditional operator. They are tested for short-circuit evaluation using the following rules: false && anything === false true || anything === true This is the same way it is implemented in Haskell: (&&) :: Bool -> Bool -> Bool False && _ = False True && x = x (||) :: Bool -> Bool -> Bool True || _ = True False || x = x However according to MDN logical operators in JavaScript are left associative

Do short-circuiting operators || and && exist for nullable booleans? The RuntimeBinder sometimes thinks so

孤街醉人 提交于 2019-12-04 07:29:40
问题 I read the C# Language Specification on the Conditional logical operators || and && , also known as the short-circuiting logical operators. To me it seemed unclear if these existed for nullable booleans, i.e. the operand type Nullable<bool> (also written bool? ), so I tried it with non-dynamic typing: bool a = true; bool? b = null; bool? xxxx = b || a; // compile-time error, || can't be applied to these types That seemed to settle the question (I could not understand the specification clearly