Skip to main content
Share Your Experience: Take the 2024 Developer Survey
119 votes
Accepted

&& and || are not logical but conditional operators?

I am a bit confused by the MSDN C# documentation which states that & and | are logical operators and that && and || are conditional operators. I keep calling &&, || and ! logical ...
Eric Lippert's user avatar
  • 46.2k
73 votes

Is there a keyword or operator for "nor"?

Although mainstream languages do not have dedicated NOR and NAND operators, some lesser-known languages (for example, some "golfing" languages) do. For example, APL has ⍱ and ⍲ for NOR and NAND, ...
nomadictype's user avatar
  • 1,652
47 votes

Is there a keyword or operator for "nor"?

No, there is no nor operator in any high level mainstream programming language. Why ? Mainly because it is difficult to read: it requires the mental combination of several operators ( "and not", ...
Christophe's user avatar
27 votes

&& and || are not logical but conditional operators?

In C# these are all logical operators. int x = 0xABCD & 0xFF // x == 0xCD && and || are called "conditional logical operators" because they are short-circuiting. bool ...
Robert Harvey's user avatar
19 votes

Is there a keyword or operator for "nor"?

@KilianFoth's comment on the question is spot on. You can synthesize nor from not and or: if (x nor y) is exactly the same as if (not (x or y)) Introducing nor as a separate operator would ...
Mael's user avatar
  • 2,355
18 votes
Accepted

Why is there both logical operators <> and !=?

The != operator is more commonly used nowadays because of the overwhelming influence in C. But how did C get there? The mathematical operator for inequality is ≠. Some languages do use this operator ...
amon's user avatar
  • 134k
15 votes
Accepted

Any reason to not use a triple not?

You can triple a not operator, but that doesn't really buy you anything except code obfuscation. The single not operator (!) converts its operand to a boolean and yields the inverse boolean value. By ...
Bart van Ingen Schenau's user avatar
12 votes

Is there a keyword or operator for "nor"?

Yes, APL and some of its dialects have nor (and nand). In APL, nor is denoted ⍱ (since ∨ is or and ~ is not): ∇ result←ExampleOne color :If (color≡'green')⍱(color≡'blue') result←'warm' :...
Adám's user avatar
  • 274
11 votes

Any reason to not use a triple not?

I'm not going to not tell you not to do this. I'm not sure this is not a not good idea or not. It might not be not confusing (not). It's not true that something is not not not true. In short, do ...
JimmyJames's user avatar
  • 27.5k
11 votes
Accepted

Conditional vs Logical Testing

If a Then If b Then If c Then If d Then If e Then x = "Passed" Else x = "Failed" That is some horrendous code right there, and it doesn't do what you think it does either: the 3 "methods" you're ...
Mathieu Guindon's user avatar
10 votes

Is there a keyword or operator for "nor"?

This answer is from the assembler language for a computer made in the mid 1960s. That's pretty obscure, but in some respects it addresses your question. DEC (Digital Equipment Corporation) launched ...
Walter Mitty's user avatar
10 votes

What is the best way to make an if statement condition itself conditional?

add more functions: buttonPress() { if(input) { process(a, b) } else { process(b, a) } } process(a, b) { for(i=0 ... }
Ewan's user avatar
  • 76.4k
9 votes
Accepted

Is it bad practice to use nullptr in ternary operation?

This is not a good idea because the ternary operator is intend to be used as an expression, and the recommended C++ core guideline is to avoid complicated expressions. Here you only use only the ...
Christophe's user avatar
9 votes

Is it bad practice to use nullptr in ternary operation?

In C++23 the code will not compile, as constructing a std::string from nullptr is explicitly disabled. Before that, constructing a std::string from a null pointer (of type const CharT*) caused ...
Deduplicator's user avatar
  • 9,051
8 votes
Accepted

What is the best way to make an if statement condition itself conditional?

You can use a ternary operator: if (input ? a < b : a > b) { ... }. However, without properly naming the conditions or adding a comment your code will be harder to read, as it might not be ...
Hans-Martin Mosner's user avatar
6 votes

Why don't languages like C have NAND operators?

I have seen only a few languages that implemented a NAND keyword. Remember that source-code is designed to be expressive of the logic that is needed to solve the problem at hand. It is entirely up to ...
Mike Robinson's user avatar
5 votes

Conditional vs Logical Testing

I feel I must insist on readable, flexible, debugable code. If a = False Then Return "Failed" If b = False Then Return "Failed" If c = False Then Return "Failed" If d = False Then Return "Failed" ...
candied_orange's user avatar
5 votes
Accepted

Why don't languages like C have NAND operators?

There are so many levels in between the source code of some program and the circuits on a chip. While some microarchitecture details definitely have a high-level performance impact, the ability to ...
amon's user avatar
  • 134k
5 votes

Why don't languages like C have NAND operators?

NAND doesn’t match the mental image of logic that normal people including software developers have. A nand B doesn’t make sense to a normal person. It’s absolutely fine in an instruction set, but not ...
gnasher729's user avatar
  • 45.7k
4 votes

Is it bad practice to use nullptr in ternary operation?

Illegal? no. But it can provoke undefined behavior. You're just looking for a default value when menuSelect is some unanticipated value. Any good reason operation can't be "undefined"? When ...
candied_orange's user avatar
4 votes

Is it bad practice to use nullptr in ternary operation?

Is this bad practice? Yes, it is terribly unreadable. Unless your code is actually performance sensitive and you have profiled it, found this code path is the hot path, and shown this construct is ...
Philip Kendall's user avatar
4 votes

Is there a keyword or operator for "nor"?

The nor operator as you described it would not be repeatable, which is bound to lead to lots of difficult to spot bugs. Your "Example 2" is essentially this: if (false nor false) { becomes if (true) ...
Buh Buh's user avatar
  • 303
3 votes

Was there some language(s) in which its logical operators returning a true or false only?

The C Language: #include <stdio.h> int main() { printf(2||3 ? "true\n" : "false\n"); return 0; } In C, zero is interpreted to be false. Any other value is interpreted to be true. ...
Robert Harvey's user avatar
3 votes

Why is there both logical operators <> and !=?

The different representations of the inequality operator is largely the result of historical reasons. The very earliest languages were developed in relative isolation compared to what we see today ...
Martin Spamer's user avatar
3 votes

Is there a keyword or operator for "nor"?

Perl has the unless keyword that lets you invert conditionals: unless ($color eq 'green' or $color eq 'blue') { # code } While not NOR operator, you can express your intent in a similar way.
Rory Hunter's user avatar
  • 1,747
3 votes

What is the best way to make an if statement condition itself conditional?

Well... I think that a good way to do that is to encapsulate your code into procedures/functions and correctly naming your steps/conditionals, like this: function iterateOverSomething() { for (...) {...
cpurificacao's user avatar
2 votes

Why don't languages like C have NAND operators?

There is absolutely no reason to minimize the number of gates involved in executing an instruction. The amount of gates that are used to read the instruction from memory, figuring out what it does, ...
Quitting Due To Antisemitism's user avatar
1 vote

What is the best way to make an if statement condition itself conditional?

In a language where you have lambdas you can easily return predicates (functions return booleans) to eliminate nested if. predicate = if (input) { (a, b) => a < b } else { (a, b) => a >...
Aiono's user avatar
  • 200
1 vote

What is the best way to make an if statement condition itself conditional?

You can always reduce cyclomatic complexity this way: for(i = 0; i < length; i++) { flag = input; if ((flag && (a < b)) || (!flag && (a > b))) { ...
Tulains Córdova's user avatar
1 vote

Conditional vs Logical Testing

You can do in following way also. if(!A) then return "Failed"; else if(!B) then return "Failed"; This could increase overall performance.
Ishan Shah's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible