Click here to Skip to main content
15,882,209 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
code is:

C++
int a=20,b=10,c=5;
int I= a<b<c;
cout<<I;
Posted
Updated 24-Mar-14 9:27am
v3
Comments
Sergey Alexandrovich Kryukov 24-Mar-14 14:41pm    
Not clear. Not an appropriate question, or not a question at all. There is no such concept as "the answer of the code"...
—SA
PIEBALDconsult 24-Mar-14 15:07pm    
Now you've mangled your operators with HTML.

I would say "compile it and see by yourself"…, but with C++, it won't even compile. You need to declare i as Boolean (bool). Or, say, even int, because Boolean is actually implemented as integer.

[EDIT]

I scratched the words about not compiling code because OP later fixed it in the text of the question. OP could compile it and see what's going on.

[END EDIT]

Here is the hint for you: bool i = a < b < c; will be compiled as it was bool i = (a < b) < c; and a < b is Boolean.

And now, compile and run it, see what's going on and try to explain it. It's going to be easy enough.

Surprised with the result? Then try to see what's going on step by step, using the debugger. Compare with:
C++
bool i = (a < b) & (b < c);


Note that the real intention of the developer could be the line shown above, quite likely, so the expression shown in your question would be the result of misunderstanding. Now think why should you always avoid expressions like the one shown in your question, mixing result of comparison with numeric operand. This should be the real lesson to learn from this ridiculous example, and, in this sense, it should be useful for you.

—SA
 
Share this answer
 
v4
Well, 20 is not less than 10, so a<b results in 0; 0 is less than 5, so the result of (a<b)<c is 1. What did you expect?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900