比较 if 语句中的 3 个参数时无法理解第一个代码的输出

发布于 2024-11-29 03:45:23 字数 756 浏览 1 评论 0 原文

这是一个小代码,一次性比较 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 语句是如何计算的? 请解释一下。

this is a small code that compares the 3 variables at a go . I have compared 2 codes.

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

I understand the second output but can't understand how is the ifstatement in the first code is evaluated ?
Please explain.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

执笔绘流年 2024-12-06 03:45:23

关系运算符是从左到右的,因此 8>7>6 的计算结果为 (8>7)>68>7 计算结果为 true,它会转换为 1,因此您会得到 1>6,即

C++标准其实明确提到了这一点:

关系运算符从左到右分组。 [示例:a

The relational operators are left-to-right, so 8>7>6 gets evaluated as (8>7)>6. 8>7 evaluates to true, which is converted to 1, so you get 1>6, which is false.

The C++ standard actually mentions this point explicitely:

The relational operators group left-to-right. [Example: a<b<c means (a<b)<c and not (a<b)&&(b<c). ]

叶落知秋 2024-12-06 03:45:23

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) means if( (8 > 7) > 6) which means if( (1) > 6) which means if(false). That means, if block cannot execute, else block will be executed which prints false.

if( 8 > (7 > 6)) means if( ( 8 > (1) ) which means if(true). So if block gets executed which prints true.

And the value of (8<9) is easy to know if you know what < means. Well, (8<9) returns true which means 1.

迟月 2024-12-06 03:45:23

第一个代码的 if 语句按从左到右的顺序进行计算。即: (8 > 7) > 6 计算如下:

(8 > 7) > 6
(1) > 6
0

请注意,(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:

(8 > 7) > 6
(1) > 6
0

Note that (8 > 7) is true, so it returns the numerical equivalent for true, 1.

The overall evaluation comes to 0, ie. false.

自此以后,行同陌路 2024-12-06 03:45:23

if 语句通常用于比较 2 个语句,如果您想要比较 2 个以上的语句,您应该使用布尔 OR || 或布尔 AND &&.这背后的推理基于布尔逻辑。您在第一个程序中发布的语句从左到右计算,这意味着

if (8 >7)>6

如果 8 大于 7(确实如此),它将计算为 true,即布尔逻辑中的“1”(1 为 true,0 为 false)。
因此,在评估之后,你有:

if (1 > 6)

自然评估为 false。因为比较不再是 8/7/6,而是 true / 6。在这种情况下,true 小于 6,因此为 false。

如果你像这样设置它,就会得到预期的结果:

if (8 > 7 && 7 > 6)//Do stuff

或者,如果你想详尽无遗..

if (8 > 7 && 7 > 6 && 8 > 6)

那么所有数字都会被比较(如果你有 3 个变量,这会更明智,也许在哪里x 应等于 100,y 应等于 50,z 应等于 25。)

然后您将进行比较

if (x > y && y > z && x > y) //do stuff

如果您的变量具有您期望的内容,则该比较将评估为 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 meaning

if (8 >7)>6

If 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:

if (1 > 6)

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:

if (8 > 7 && 7 > 6)//Do stuff

or alternatively, if you wanted to be exhaustive..

if (8 > 7 && 7 > 6 && 8 > 6)

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

if (x > y && y > z && x > y) //do stuff

This would evaluate to true in the event that your variables had the contents that you expected.

蓝颜夕 2024-12-06 03:45:23

if(8 > 7 > 6) 的计算方式如下:

if((8 > 7) > 6)
if(true > 6)
true is implicitly casted to 1. 
1 > 6 is false.

if(8 > 7 > 6) is evaluated like :

if((8 > 7) > 6)
if(true > 6)
true is implicitly casted to 1. 
1 > 6 is false.
じее 2024-12-06 03:45:23

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.

勿忘初心 2024-12-06 03:45:23

关系运算符从左到右关联!
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

风情万种。 2024-12-06 03:45:23

运算符“>”的结合性是从左到右,所以这意味着在第一个示例中,首先评估“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文