比较 if 语句中的 3 个参数时无法理解第一个代码的输出
这是一个小代码,一次性比较 3 个变量。我比较了2个代码。
FIRST
#include <iostream>
int main() {
if( 8 > 7 > 6) std::cout << "true";
else std::cout << "false";
std::cout << "\n";
std::cout << (8 < 9);
std::cout << "\n";
system("pause");
}
// output :
/* false
* 1
*/
SECOND
#include <iostream>
int main() {
if( 8 > (7 > 6) ) std::cout << "true"; // with brackets
else std::cout << "false";
std::cout << "\n";
std::cout << (8 < 9);
std::cout << "\n";
system("pause");
}
// output :
// true
// 1
我理解第二个输出,但无法理解第一个代码中的 if
语句是如何计算的?
请解释一下。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
关系运算符是从左到右的,因此
8>7>6
的计算结果为 (8>7)>6
。8>7
计算结果为true
,它会转换为1
,因此您会得到1>6
,即假
。C++标准其实明确提到了这一点:
The relational operators are left-to-right, so
8>7>6
gets evaluated as (8>7)>6
.8>7
evaluates totrue
, which is converted to1
, so you get1>6
, which isfalse
.The C++ standard actually mentions this point explicitely:
if( 8 > 7 > 6)
表示if( (8 > 7) > 6)
表示if( (1) > 6 )
表示if(false)
。这意味着,if
块无法执行,else
块将被执行,并打印false
。if( 8 > (7 > 6))
表示if( ( 8 > (1) )
表示if(true)
因此,执行if
块会打印true
,如果您知道
(8<9)
的值,就很容易知道是什么。 ><
的意思是。(8<9)
返回true
,这意味着1
。if( 8 > 7 > 6)
meansif( (8 > 7) > 6)
which meansif( (1) > 6)
which meansif(false)
. That means,if
block cannot execute,else
block will be executed which printsfalse
.if( 8 > (7 > 6))
meansif( ( 8 > (1) )
which meansif(true)
. Soif
block gets executed which printstrue
.And the value of
(8<9)
is easy to know if you know what<
means. Well,(8<9)
returnstrue
which means1
.第一个代码的 if 语句按从左到右的顺序进行计算。即:
(8 > 7) > 6
计算如下:请注意,
(8 > 7)
为true
,因此它返回与true
等价的数值>,<代码>1。总体评价为
0
,即。假
。The first code's if statement is evaluated in left-to-right order. ie:
(8 > 7) > 6
which is evaluated as follows:Note that
(8 > 7)
istrue
, so it returns the numerical equivalent fortrue
,1
.The overall evaluation comes to
0
, ie.false
.if 语句通常用于比较 2 个语句,如果您想要比较 2 个以上的语句,您应该使用布尔 OR
||
或布尔 AND&&.这背后的推理基于布尔逻辑。您在第一个程序中发布的语句从左到右计算,这意味着
如果 8 大于 7(确实如此),它将计算为 true,即布尔逻辑中的“1”(1 为 true,0 为 false)。
因此,在评估之后,你有:
自然评估为 false。因为比较不再是 8/7/6,而是 true / 6。在这种情况下,true 小于 6,因此为 false。
如果你像这样设置它,就会得到预期的结果:
或者,如果你想详尽无遗..
那么所有数字都会被比较(如果你有 3 个变量,这会更明智,也许在哪里x 应等于 100,y 应等于 50,z 应等于 25。)
然后您将进行比较
如果您的变量具有您期望的内容,则该比较将评估为 true。
An if statement is classically made to compare 2 statements, if you are wanting to compare more than 2 statements you should be using a boolean OR
||
or a boolean AND&&
. The reasoning behind this is based in boolean logic. The statement that you posted in the first program evaluates from left to right meaningIf 8 is greater than 7 (which it is) it will evaluate to true, which is "1" in boolean logic (1 being true, 0 being false).
Thus after that evaluates you have:
which naturally evaluates to false. Because the comparison is no longer 8/7/6, it's true / 6. And in this case, true is less than 6, thus a false.
If you were to set it up like this, would would have the expected results:
or alternatively, if you wanted to be exhaustive..
so all numbers are compared (this would be more sensible in the event that you had 3 variables, perhaps where x should be equal to 100, y should be equal to 50, and z should be equal to 25.)
You'd then have the comparison
This would evaluate to true in the event that your variables had the contents that you expected.
if(8 > 7 > 6) 的计算方式如下:
if(8 > 7 > 6) is evaluated like :
if 语句从左到右计算,因此与 (8>7)>6 相同。
8>7 为真,等于 1,因此它评估 1>6,为假。
The if statement is evaluated left to right so it is the same as (8>7)>6.
8>7 is true which is equal to 1 so then it evaluates 1>6 which is false.
关系运算符从左到右关联!
8>7>6 = (8>7)>6 = 1>6 = 假
The relational operators associate from left to right!
8>7>6 = (8>7)>6 = 1>6 = FALSE
运算符“>”的结合性是从左到右,所以这意味着在第一个示例中,首先评估“8 > 7”,其结果为 1,然后针对 6 评估,结果为 false。
在第二个示例中,由于您使用括号,因此我们有 8 > 1,这是真的。
希望这有帮助。
The associativity of operator ">" is from left to right, so it means that in your first example, "8 > 7" is evaluated first, and the result of that is 1, which is then evaluated against 6, which result is false.
In the second example, since you use brackets, the we have 8 > 1, which is true.
Hope this helps.