1
$\begingroup$

This is related to the issue asked about in this other question.

For the sake of this question, let's assume that we have a variable v that can have any value from the set of values

"a", "b", "c", ..., "z"

In this case then the programmers wants to have specific code executed when the value of v is not "x", "y", or "z".

So the following IF statement is constructed:

if (v != "x" || v != "y" || v != "z") {//do something}

Above statement would always return true, due to that the 3 conditions using negation are checked independently of each other.

Consequently, at any given time, any single one of the 3 conditions can evaluate to TRUE, leading to the entire IF statement evaluating to TRUE.

Anyway so the question I wanted to ask is, does this behavior or situation have a formal name, and if so, what is it?

$\endgroup$
2
  • 2
    $\begingroup$ I don't understand your question. In all programming languages, the if statement you give is true, since v can't be simultaneously equal to three different things so it's guaranteed that at least two of the conditions are true. And Boolean OR by definition evaluates to true if at least one of its arguments is true. So I'm not sure what behaviour you're talking about. $\endgroup$ Commented Aug 18, 2017 at 19:55
  • $\begingroup$ And the condition that's normally expressed as "not "x", "y" or "z"" would be written !(v=="x" || v=="y" || v=="z") $\endgroup$ Commented Aug 18, 2017 at 20:23

1 Answer 1

4
$\begingroup$

In many programming languages above statement always returns true, due to that the 3 conditions using negation are checked independently of each other.

It returns true not just due to "that the 3 conditions using negation are checked independently of each other", but because the expression (v != "x" || v != "y" || v != "z") is always necessarily true. In logic such expressions are called tautology, i.e. a formula that is true in every possible interpretation (Wikipedia).

But "this behavior or situation" does not have a special name in programming and is usually considered as a programming error.

$\endgroup$
1
  • 2
    $\begingroup$ Logic error - it has the simple name. $\endgroup$
    – Evil
    Commented Aug 19, 2017 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.