Double comparison

后端 未结 3 1094
轮回少年
轮回少年 2021-01-29 03:21

Can I do this in C++?

if (4<5<6)
 cout<<\"valid\"<

i.e a double comparison? Since I know that I can

boo         


        
3条回答
  •  迷失自我
    2021-01-29 04:04

    It compiles but won't do what you expect -

    if( 4 < 5 < 2) 
    

    same as

    if( (4 < 5) < 2)
    

    same as

    if( (1 < 2) )  //1 obtained from cast to boolean
    

    which is of course true, even though I imagine you were expecting something quite different.

提交回复
热议问题