开发者

unable to understand the output of first code while comparing 3 arguments in the if statement

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:开发者_StackOverflow: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.


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). ]


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.


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.


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.


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

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


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.


The relational operators associate from left to right! 8>7>6 = (8>7)>6 = 1>6 = FALSE


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜