If statements and && or ||

后端 未结 12 1276
臣服心动
臣服心动 2020-12-22 02:54

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.

相关标签:
12条回答
  • 2020-12-22 03:45

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

    That would be:

    if (!(iNumber == 8 || iNumber == 9))
    ...
    
    0 讨论(0)
  • 2020-12-22 03:50

    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
    }
    
    0 讨论(0)
  • 2020-12-22 03:51

    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.

    0 讨论(0)
  • 2020-12-22 03:53

    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

    0 讨论(0)
  • 2020-12-22 03:53

    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

    0 讨论(0)
  • 2020-12-22 03:57

    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

    0 讨论(0)
提交回复
热议问题