Chris
Chris

Reputation: 27

If statements and && or ||

OK so I'm a beginner with C# and I am having trouble understanding the if statement below.

For this example INumber is declared as 8, dPrice is 0 and dTAX is 10.

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

Can somebody explain why it is entering the statement and adding the 10 from dTAX to dPrice?

I know changing it to && works, but why?

As I understand it, it should only enter the If statement, if iNumber does not equal 8 or 9, which here it does, so it should not enter.

Here are my outputs after running it through the || if statement.

        Inumber is: 8

        dPrice was: 0
        dPrice is now: 10

        dTAX is: 10

Can somebody please explain this to me?

Upvotes: 1

Views: 57000

Answers (12)

Joel Coehoorn
Joel Coehoorn

Reputation: 415630

The if conditional expression 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))

Upvotes: 4

kyawmin tun
kyawmin tun

Reputation: 1

Maintaining the menu of a large web site is difficult and time consuming.

In ASP.NET the menu can be stored in a file to make it easier to maintain. This file is normally called web.sitemap, and is stored in the root directory of the web.

In addition, ASP.NET has three new navigation controls:

Dynamic menus TreeViews Site Map Path

Upvotes: -1

Smurfie
Smurfie

Reputation: 124

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.

Upvotes: 1

User42
User42

Reputation: 370

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

That would be:

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

Upvotes: 0

Vasil Indzhev
Vasil Indzhev

Reputation: 695

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
}

Upvotes: 2

Sophie Coyne
Sophie Coyne

Reputation: 1006

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.

Upvotes: 0

Francisco d'Anconia
Francisco d'Anconia

Reputation: 2636

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.

Upvotes: 0

D Stanley
D Stanley

Reputation: 152511

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.

Upvotes: 1

Danielle
Danielle

Reputation: 490

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.

Upvotes: 0

Brady Liles
Brady Liles

Reputation: 723

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
}

Upvotes: 5

Habib
Habib

Reputation: 223207

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

Upvotes: 0

Robert McKee
Robert McKee

Reputation: 21487

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

Upvotes: 1

Related Questions