Questions tagged [logical-operators]

Logical operators are symbols that aid in evaluating boolean expressions. These are found in just about every programming language that supports boolean. Common examples are && (AND), || (OR), ! (NOT), == ([value] equal to), === ([value and type] equal to), >= (Greater than or equal to), <= (Less than or equal to), > (Greater than), and < (less than)

Operations:

  • AND: both expressions are TRUE
  • OR: either expression is TRUE
  • NOT: TRUE if the expression is FALSE
  • NAND: the inverse of an AND operation
  • NOR: the inverse of an OR operation
  • XOR: either expression is TRUE but not both
  • XNOR: both must be TRUE or both must be FALSE

In scientific software for statistical computing and graphics, consult ?base::Logic for logical operators.

2579 questions
954
votes
29 answers

How do you get the logical xor of two variables in Python?

How do you get the logical xor of two variables in Python? For example, I have two variables that I expect to be strings. I want to test that only one of them contains a True value (is not None or an empty string): str1 = raw_input("Enter string…
Zach Hirsch
  • 24,631
  • 8
  • 32
  • 29
375
votes
11 answers

Logical XOR operator in C++?

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 ^^…
RAC
  • 4,979
  • 3
  • 21
  • 10
331
votes
5 answers

Simple logical operators in Bash

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…
Amit
  • 7,688
  • 17
  • 53
  • 68
324
votes
5 answers

Difference between Boolean operators && and & and between || and | in R

According to the R language definition, the difference between & and && (correspondingly | and ||) is that the former is vectorized while the latter is not. According to the help text, I read the difference akin to the difference between an "And"…
Suraj
  • 35,905
  • 47
  • 139
  • 250
311
votes
14 answers

Logical operators ("and", "or") in DOS batch

How would you implement logical operators in DOS Batch files?
JoelFan
  • 37,465
  • 35
  • 132
  • 205
295
votes
6 answers

Are || and ! operators sufficient to make every possible logical expression?

The logical expression ( a && b ) (both a and b have boolean values) can be written like !(!a || !b), for example. Doesn't this mean that && is "unneccesary"? Does this mean that all logical expressions can be made only using || and !?
JakeTheSnake
  • 2,456
  • 3
  • 14
  • 26
283
votes
20 answers

Why is there no logical XOR?

Why is there no logical XOR in JavaScript?
DarkLightA
  • 14,980
  • 18
  • 49
  • 57
228
votes
5 answers

SQL Logic Operator Precedence: And and Or

Are the two statements below equivalent? SELECT [...] FROM [...] WHERE some_col in (1,2,3,4,5) AND some_other_expr and SELECT [...] FROM [...] WHERE some_col in (1,2,3) or some_col in (4,5) AND some_other_expr Is there some sort of truth table I…
nc.
  • 7,179
  • 5
  • 28
  • 38
173
votes
7 answers

Is short-circuiting logical operators mandated? And evaluation order?

Does the ANSI standard mandate the logical operators to be short-circuited, in either C or C++? I'm confused for I recall the K&R book saying your code shouldn't depend on these operations being short circuited, for they may not. Could someone…
Joe Pineda
  • 5,521
  • 3
  • 31
  • 40
161
votes
12 answers

Is it good practice to use the xor operator for boolean checks?

I personally like the exclusive or, ^, operator when it makes sense in the context of boolean checks because of its conciseness. I much prefer to write if (boolean1 ^ boolean2) { //do it } than if((boolean1 && !boolean2) || (boolean2 &&…
Peter
  • 29,498
  • 21
  • 89
  • 122
143
votes
8 answers

How do "and" and "or" act with non-boolean values?

I'm trying to learn python and came across some code that is nice and short but doesn't totally make sense the context was: def fn(*args): return len(args) and max(args)-min(args) I get what it's doing, but why does python do this - ie return…
Marcin
  • 1,889
  • 2
  • 16
  • 20
141
votes
9 answers

Is there actually a reason why overloaded && and || don't short circuit?

The short circuiting behaviour of the operators && and || is an amazing tool for programmers. But why do they lose this behaviour when overloaded? I understand that operators are merely syntactic sugar for functions but the operators for bool have…
131
votes
10 answers

Java logical operator short-circuiting

Which set is short-circuiting, and what exactly does it mean that the complex conditional expression is short-circuiting? public static void main(String[] args) { int x, y, z; x = 10; y = 20; z = 30; // T T // T F // F T // F F …
Aaron
  • 11,239
  • 18
  • 58
  • 73
124
votes
4 answers

What's the difference between & and && in JavaScript?

What's the difference between & and && in JavaScript? Example code: var first = 123; var second = false; var third = 456; var fourth = "abc"; var fifth = true; alert(first & second); // 0 alert(first & third); // 72 alert(first & fourth); //…
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
124
votes
11 answers

Differences in boolean operators: & vs && and | vs ||

I know the rules for && and || but what are & and |? Please explain these to me with an example.
Sumithra
  • 6,587
  • 19
  • 51
  • 50
1
2 3
99 100