If statements and && or ||

不羁的心 提交于 2019-11-29 17:05:16

It is entering the statement because the statement becomes true when it calculates iNumber != 9

An || (Or Operator) in an if will be true if any statement is true.

Think of it this way..

8 != 8 is False
8 != 9 is True

if ( False || True )
{
    //Do Stuff
}

The if condition in this code will always evaluate as true:

if (iNumber != 8 || iNumber != 9)

When iNumber is 8, it's not equal to 9, so the 2nd part is true. When iNumber is 9, it's not equal to 8, so the first part is true. Anything else, and both sides are true. || conditions result in true with either side is true. There's no way for this to ever be false. You want && here instead of ||:

if (iNumber != 8 && iNumber != 9)

Or you could use DeMorgan's Law and get this:

if (! (iNumber == 8 || iNumber == 9))

it should only enter the If statement, if iNumber does not equal 8 OR if iNumber does not equal 9. It does not equal 9, so it will enter

The statement is logically equivalent to

    if (!(iNumber == 8 && iNumber == 9))
    {
        dPrice = dPrice + dTAX;
    }

which is ALWAYS true since a number cannot be both 8 and 9.

You want:

    if (iNumber != 8 && iNumber != 9)
    {
        dPrice = dPrice + dTAX;
    }

or

    if (!(iNumber == 8 || iNumber == 9))
    {
        dPrice = dPrice + dTAX;
    }

whichever makes more sense to you, logically.

Logical AND (&&)

The logical AND operator (&&) returns the boolean value true if both operands are true and returns false otherwise. The operands are implicitly converted to type bool prior to evaluation, and the result is of type bool. Logical AND has left-to-right associativity.

Logical OR (||)

The logical OR operator (||) returns the boolean value true if either or both operands is true and returns false otherwise. The operands are implicitly converted to type bool prior to evaluation, and the result is of type bool. Logical OR has left-to-right associativity.

So if you have:

bool someVariable = true;
bool someOtherVariable = false;

if ((someVariable == true) && (someOtherVaribale == true))
{
    //This code will be executed
}

if ((someVaribale == true) || (someOtherVariable == true))
{
    //This code will be executed
}

You are using || (Logical OR), which would evaluate to true if any of the operand is true. So your first condition (iNumber != 8) is false but the second condition (iNumber != 9) is true, hence the overall result is true.

The reason it works with && is that AND operator requires both operand to be true, to evaluate to true. If one of the operand is false the overall result is false.

You should see: Truth Tables, Logic, and DeMorgan's Laws

The || OR operator means only one OR the other has to be true. In this case, iNumber != 9, so that portion of the code is true and it enters the statement. I think you'll want to use the && AND operator to indicate that it can't be 8 AND it can't be 9.

In English, this reads as: If iNumber is not 8 OR iNumber is not 9. iNumber is 8, which IS NOT 9 (your second check), so it drops into the if block.

You're describing an AND condition where it doesn't equal 8 and it doesn't equal 9.

Use

if (iNumber != 8 && iNumber != 9)

This means "if iNumber is not equal to eight and iNumber is not equal to nine". Your statement:

if (!Number != 8 || iNumber != 9)

Means "if !iNumber is not equal to eight or iNumber is not equal to nine" which is true as long as iNumber is not equal to one of those values, and because it can only hold one value and not two simultaneously, this statement will always be true.

it should only enter the If statement, if iNumber does not equal 8 or 9

That would be:

if (!(iNumber == 8 || iNumber == 9))
...

The following is from MSDN:

The conditional-OR operator (||) performs a logical-OR of its bool operands. If the first operand evaluates to true, the second operand isn't evaluated. If the first operand evaluates to false, the second operator determines whether the OR expression as a whole evaluates to true or false.

In your example the first condition (!=8) is false because iNumber = 8, but the second condition is (!=9), which is true. So that's why it goes into the braces.

If instead you say !=8 && !=9, it will not go in the braces because it doesn't satisfy both conditions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!