logical-operators

Setting default values (conditional assignment)

二次信任 提交于 2019-11-26 20:32:18
问题 In Ruby you can easily set a default value for a variable x ||= "default" The above statement will set the value of x to "default" if x is nil or false Is there a similar shortcut in PHP or do I have to use the longer form: $x = (isset($x))? $x : "default"; Are there any easier ways to handle this in PHP? 回答1: As of PHP 5.3 you can use the ternary operator while omitting the middle argument: $x = $x ?: 'default'; 回答2: isset($x) or $x = 'default'; 回答3: As of PHP 7.0, you can also use the null

Dealing with TRUE, FALSE, NA and NaN

不打扰是莪最后的温柔 提交于 2019-11-26 20:24:41
Here is a vector a <- c(TRUE, FALSE, FALSE, NA, FALSE, TRUE, NA, FALSE, TRUE) I'd like a simple function that returns TRUE everytime there is a TRUE in "a", and FALSE everytime there is a FALSE or a NA in "a". The three following things do not work a == TRUE identical(TRUE, a) isTRUE(a) Here is a solution a[-which(is.na(a))] but it doesn't seem to be a straightforward and easy solution Is there another solution ? Here are some functions (and operators) I know: identical() isTRUE() is.na() na.rm() & | ! What are the other functions (operators, tips, whatever,...) that are useful to deal with

How does javascript logical assignment work?

我的梦境 提交于 2019-11-26 20:06:19
问题 In javascript, if we have some code such as var a = "one"; var b = q || a; alert (b); The logical OR operator will assign a's value to b, and the alert will be "one." Is this limited to assignments only or can we use it everywhere? It seems an empty string is treated the same as undefined. Is this right? How does this work with AND variables? What about combinations of them? What is a good example of when to use these idioms, or when not to? 回答1: For your q || a to evaluate to a , q should be

Logical Operators in C

我与影子孤独终老i 提交于 2019-11-26 19:12:55
I am having trouble trying to understand how logical operators work in C. I already understand how the bit-level operators work, and I also know that logical operators treat nonzero arguments as representing TRUE and zero arguments as representing FALSE But say we have 0x65 && 0x55. I do not understand why and how this operations gives 0x01. I tried to convert it to binary, but I cannot figure out how it works dasblinkenlight The && is a logical AND (as opposed to & , which is a bitwise AND ). It cares only that its operands as zero/non-zero values. Zeros are considered false , while non-zeros

Logical XOR operator in C++?

纵然是瞬间 提交于 2019-11-26 17:29:35
问题 Is there such a thing? It is the first time I encountered a practical need for it, but I don't see one listed in Stroustrup. I intend to write: // Detect when exactly one of A,B is equal to five. return (A==5) ^^ (B==5); But there is no ^^ operator. Can I use the bitwise ^ here and get the right answer (regardless of machine representation of true and false)? I never mix & and && , or | and || , so I hesitate to do that with ^ and ^^ . I'd be more comfortable writing my own bool XOR(bool,bool

The difference between 'AND' and '&&' in SQL

折月煮酒 提交于 2019-11-26 16:59:08
问题 Is there a difference in the way SQL interprets the logical operators AND and && ? 回答1: For mySQL: The manual is not saying it explicitly, but they are listed as identical: AND, && Logical AND. Evaluates to 1 if all operands are nonzero and not NULL, to 0 if one or more operands are 0, otherwise NULL is returned. The operator precedence page also makes no distiction. 回答2: AND is Standard SQL && is proprietary syntax 回答3: According to this page http://msdn.microsoft.com/en-us/library/bb387129

Simple logical operators in Bash

大城市里の小女人 提交于 2019-11-26 16:50:33
I have a couple of variables and I want to check the following condition (written out in words, then my failed attempt at bash scripting): if varA EQUALS 1 AND ( varB EQUALS "t1" OR varB EQUALS "t2" ) then do something done. And in my failed attempt, I came up with: if (($varA == 1)) && ( (($varB == "t1")) || (($varC == "t2")) ); then scale=0.05 fi Gilles What you've written actually almost works (it would work if all the variables were numbers), but it's not an idiomatic way at all. (…) parentheses indicate a subshell . What's inside them isn't an expression like in many other languages. It's

What&#39;s the difference between & and && in MATLAB?

为君一笑 提交于 2019-11-26 15:12:26
What is the difference between the & and && logical operators in MATLAB? The single ampersand & is the logical AND operator. The double ampersand && is again a logical AND operator that employs short-circuiting behaviour. Short-circuiting just means the second operand (right hand side) is evaluated only when the result is not fully determined by the first operand (left hand side) A & B (A and B are evaluated) A && B (B is only evaluated if A is true) Loren && and || take scalar inputs and short-circuit always. | and & take array inputs and short-circuit only in if/while statements. For

!! c operator, is a two NOT?

女生的网名这么多〃 提交于 2019-11-26 13:49:39
I reading this code , and have this line switch (!!up + !!left) { what is !! operator ? two logical NOT ? yes, it's two nots. !!a is 1 if a is non-zero and 0 if a is 0 You can think of !! as clamping, as it were, to {0,1} . I personally find the usage a bad attempt to appear fancy. You can imagine it like this: !(!(a)) If you do it step by step, this make sense result = !42; //Result = 0 result = !(!42) //Result = 1 because !0 = 1 This will return 1 with any number (-42, 4.2f, etc.) but only with 0 , this will happens result = !0; //Result = 1 result = !(!0) //result = 0 !! is a more-portable

The written versions of the logical operators

不打扰是莪最后的温柔 提交于 2019-11-26 13:06:00
This is the only place I've ever seen and , or and not listed as actual operators in C++. When I wrote up a test program in NetBeans, I got the red underlining as if there was a syntax error and figured the website was wrong, but it is NetBeans which is wrong because it compiled and ran as expected. I can see ! being favored over not but the readability of and && or seems greater than their grammatical brothers. Why do these versions of the logical operators exist and why does seemingly no one use it? Is this truly valid C++ or some sort of compatibility with C that was included with the