条件运算符的嵌套_条件运算符
條件運算符的嵌套
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
條件運算符的嵌套
總結
以上是生活随笔為你收集整理的条件运算符的嵌套_条件运算符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Stellarium:Compile w
- 下一篇: qp_查看表的数据是否更新了指定的某一天