If 语句和 && 或 ||
If statements and && or ||
好的,我是 C# 的初学者,我无法理解下面的 if 语句。
对于此示例,INumber 声明为 8,dPrice 为 0,dTAX 为 10。
if (iNumber != 8 || iNumber != 9)
{
dPrice = dPrice + dTAX;
}
有人可以解释为什么要输入语句并将 dTAX 中的 10 添加到 dPrice 中吗?
我知道将其更改为 && 有效,但为什么呢?
据我了解,如果iNumber不等于8或9,应该只进入If语句,这里是,所以不应该进入。
这是我在 运行 通过 || 之后的输出if 语句。
Inumber is: 8
dPrice was: 0
dPrice is now: 10
dTAX is: 10
有人可以给我解释一下吗?
如果iNumber不等于8或iNumber不等于9,它应该只进入If语句。它不等于9,所以它会进入
逻辑与 (&&)
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.
逻辑或 (||)
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.
所以如果你有:
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
}
您正在使用 ||
(Logical OR),如果任何操作数为真,则计算结果为真。因此,您的第一个条件 (iNumber != 8
) 为假,但第二个条件 (iNumber != 9
) 为真,因此总体结果为真。
它与 &&
一起工作的原因是 AND 运算符要求两个操作数都为真,计算结果为真。如果其中一个操作数为假,则总体结果为假。
此代码中的 if
条件将 始终 计算为 true
:
if (iNumber != 8 || iNumber != 9)
当iNumber
为8
时,不等于9
,所以第二部分为true
。当iNumber
为9
时,不等于8
,所以第一部分为true
。别的什么,两边都是true
。 ||
条件导致 true
,任何一方都是 true
。这不可能是 false
。你想在这里 &&
而不是 ||
:
if (iNumber != 8 && iNumber != 9)
或者您可以使用 DeMorgan's Law 得到这个:
if (! (iNumber == 8 || iNumber == 9))
进入语句是因为语句在计算iNumber != 9时为真
一个||如果任何语句为真,if 中的(或运算符)将为真。
这样想..
8 != 8 is False
8 != 9 is True
if ( False || True )
{
//Do Stuff
}
|| OR 运算符意味着只有一个或另一个必须为真。在本例中,iNumber != 9,因此该部分代码为真并进入语句。我想你会想要使用 && AND 运算符来指示它不能是 8 并且它不能是 9.
该语句在逻辑上等同于
if (!(iNumber == 8 && iNumber == 9))
{
dPrice = dPrice + dTAX;
}
这总是正确的,因为一个数字不能同时是 8 和 9。
你想要:
if (iNumber != 8 && iNumber != 9)
{
dPrice = dPrice + dTAX;
}
或
if (!(iNumber == 8 || iNumber == 9))
{
dPrice = dPrice + dTAX;
}
从逻辑上看,哪个对您来说更有意义。
在英文中,它的意思是:If iNumber is not 8 OR iNumber is not 9。
iNumber 是 8,不是 9(你的第二次检查),所以它落入 if 块。
您描述的是 AND 条件,它既不等于 8 也不等于 9。
it should only enter the If statement, if iNumber does not equal 8 or
9
那就是:
if (!(iNumber == 8 || iNumber == 9))
...
使用
if (iNumber != 8 && iNumber != 9)
这意味着 "if iNumber is not equal to eight and iNumber is not equal to nine"。您的声明:
if (!Number != 8 || iNumber != 9)
表示 "if !iNumber is not equal to eight or iNumber is not equal to nine" 只要 iNumber 不等于这些值之一就为真,并且因为它只能包含一个值而不能同时包含两个值,所以此语句将始终为真。
以下内容来自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.
在您的示例中,第一个条件 (!=8) 为假,因为 iNumber = 8,但第二个条件 (!=9) 为真。所以这就是它进入大括号的原因。
如果你说 !=8 && !=9,它不会放在大括号中,因为它不满足两个条件。
维护大型网站的菜单既困难又费时。
在ASP.NET中,菜单可以存储在一个文件中,以便于维护。这个文件一般叫做web.sitemap,存放在网站的根目录下。
此外,ASP.NET 有三个新的导航控件:
动态菜单
树视图
站点地图路径
好的,我是 C# 的初学者,我无法理解下面的 if 语句。
对于此示例,INumber 声明为 8,dPrice 为 0,dTAX 为 10。
if (iNumber != 8 || iNumber != 9)
{
dPrice = dPrice + dTAX;
}
有人可以解释为什么要输入语句并将 dTAX 中的 10 添加到 dPrice 中吗?
我知道将其更改为 && 有效,但为什么呢?
据我了解,如果iNumber不等于8或9,应该只进入If语句,这里是,所以不应该进入。
这是我在 运行 通过 || 之后的输出if 语句。
Inumber is: 8
dPrice was: 0
dPrice is now: 10
dTAX is: 10
有人可以给我解释一下吗?
如果iNumber不等于8或iNumber不等于9,它应该只进入If语句。它不等于9,所以它会进入
逻辑与 (&&)
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.
逻辑或 (||)
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.
所以如果你有:
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
}
您正在使用 ||
(Logical OR),如果任何操作数为真,则计算结果为真。因此,您的第一个条件 (iNumber != 8
) 为假,但第二个条件 (iNumber != 9
) 为真,因此总体结果为真。
它与 &&
一起工作的原因是 AND 运算符要求两个操作数都为真,计算结果为真。如果其中一个操作数为假,则总体结果为假。
此代码中的 if
条件将 始终 计算为 true
:
if (iNumber != 8 || iNumber != 9)
当iNumber
为8
时,不等于9
,所以第二部分为true
。当iNumber
为9
时,不等于8
,所以第一部分为true
。别的什么,两边都是true
。 ||
条件导致 true
,任何一方都是 true
。这不可能是 false
。你想在这里 &&
而不是 ||
:
if (iNumber != 8 && iNumber != 9)
或者您可以使用 DeMorgan's Law 得到这个:
if (! (iNumber == 8 || iNumber == 9))
进入语句是因为语句在计算iNumber != 9时为真
一个||如果任何语句为真,if 中的(或运算符)将为真。
这样想..
8 != 8 is False
8 != 9 is True
if ( False || True )
{
//Do Stuff
}
|| OR 运算符意味着只有一个或另一个必须为真。在本例中,iNumber != 9,因此该部分代码为真并进入语句。我想你会想要使用 && AND 运算符来指示它不能是 8 并且它不能是 9.
该语句在逻辑上等同于
if (!(iNumber == 8 && iNumber == 9))
{
dPrice = dPrice + dTAX;
}
这总是正确的,因为一个数字不能同时是 8 和 9。
你想要:
if (iNumber != 8 && iNumber != 9)
{
dPrice = dPrice + dTAX;
}
或
if (!(iNumber == 8 || iNumber == 9))
{
dPrice = dPrice + dTAX;
}
从逻辑上看,哪个对您来说更有意义。
在英文中,它的意思是:If iNumber is not 8 OR iNumber is not 9。 iNumber 是 8,不是 9(你的第二次检查),所以它落入 if 块。
您描述的是 AND 条件,它既不等于 8 也不等于 9。
it should only enter the If statement, if iNumber does not equal 8 or 9
那就是:
if (!(iNumber == 8 || iNumber == 9))
...
使用
if (iNumber != 8 && iNumber != 9)
这意味着 "if iNumber is not equal to eight and iNumber is not equal to nine"。您的声明:
if (!Number != 8 || iNumber != 9)
表示 "if !iNumber is not equal to eight or iNumber is not equal to nine" 只要 iNumber 不等于这些值之一就为真,并且因为它只能包含一个值而不能同时包含两个值,所以此语句将始终为真。
以下内容来自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.
在您的示例中,第一个条件 (!=8) 为假,因为 iNumber = 8,但第二个条件 (!=9) 为真。所以这就是它进入大括号的原因。
如果你说 !=8 && !=9,它不会放在大括号中,因为它不满足两个条件。
维护大型网站的菜单既困难又费时。
在ASP.NET中,菜单可以存储在一个文件中,以便于维护。这个文件一般叫做web.sitemap,存放在网站的根目录下。
此外,ASP.NET 有三个新的导航控件:
动态菜单 树视图 站点地图路径