条件运算符的嵌套_条件运算符

条件运算符的嵌套

Conditional operators are used to evaluate a condition that's applied to one or two boolean expressions. The result of the evaluation is either true or false.

条件运算符用于评估应用于一个或两个布尔表达式的条件。 评估结果为真或假。

There are three conditional operators:

有三个条件运算符:

 &&   the logical AND operator.
||   the logical OR operator.
?:   the ternary operator.

条件运算符 ( Conditional Operators )

The logical AND and logical OR operators both take two operands. Each operand is a boolean expression (i.e., it evaluates to either true or false). The logical AND condition returns true if both operands are true, otherwise, it returns false. The logical OR condition returns false if both operands are false, otherwise, it returns true.

逻辑AND和逻辑OR运算符都采用两个操作数。 每个操作数都是一个布尔表达式(即,它的计算结果为true或false)。 如果两个操作数都为true,则逻辑AND条件返回true,否则返回false。 如果两个操作数均为false,则逻辑OR条件返回false,否则返回true。

Both the logical AND and logical OR operators apply a short circuit method of evaluation. In other words, if the first operand determines the overall value for the condition, then the second operand is not evaluated. For example, if the logical OR operator evaluates its first operand to be true, it does not need to evaluate the second one because it already knows the logical OR condition has to be true. Similarly, if the logical AND operator evaluates its first operand to be false, it can skip the second operand because it already knows the logical AND condition will be false.

逻辑与和逻辑或运算符均采用短路评估方法。 换句话说,如果第一个操作数确定条件的总值,则不评估第二个操作数。 例如,如果逻辑OR运算符将其第一个操作数评估为true,则无需评估第二个操作数,因为它已经知道逻辑OR条件必须为true。 同样,如果逻辑AND运算符将其第一个操作数评估为false,则可以跳过第二个操作数,因为它已经知道逻辑AND条件将为false。

The ternary operator takes three operands. The first is a boolean expression; the second and third are values. If the boolean expression is true, the ternary operator returns the value of the second operand, otherwise, it returns the value of the third operand.

三元运算符采用三个操作数。 第一个是布尔表达式;第二个是布尔表达式。 第二和第三是值。 如果布尔表达式为true,则三元运算符将返回第二个操作数的值,否则,它将返回第三个操作数的值。

条件运算符的一个例子 ( An Example of Conditional Operators )

To test if a number is divisible by two and four:

要测试数字是否可以被二和四整除:

 int number = 16;
if (number % 2 == 0 && number % 4 == 0)
{
  System.out.println("It's divisible by two and four!");
}
else
{
  System.out.println("It's not divisible by two and four!");
}

The conditional operator "&&" first evaluates whether its first operand (i.e., number % 2 == 0) is true and then evaluates whether its second operand (i.e., number % 4 == 0) is true. As both are true, the logical AND condition is true.

条件运算符“ &&”首先评估其第一个操作数(即数字%2 == 0)是否为真,然后评估其第二个操作数(即数字%4 == 0)是否为真。 由于两者均为真,所以逻辑与条件为真。

翻译自: https://www.thoughtco.com/conditional-operator-2034056

条件运算符的嵌套

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值