C ++ if语句符号-这等效吗? [英] C++ if statement notation - Is this equivalent?

查看:233
本文介绍了C ++ if语句符号-这等效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我99%的人肯定这行不通,但剩下的1%的人困扰我

I'm 99% sure this won't work but that remaining 1% is bothering me

    int x;

    //is this if statement

    if(x == 1, 5, 7)
    {
    //do something here
    }
    //equivalent to this if statement

    if((x == 1) || (x == 5) || (x == 7))
    {
    //do something here
    }

推荐答案

不,这完全不等效.

if(x == 1, 5, 7)

调用逗号运算符,由于优先级最低:

calls the comma operator, which will effectively end up in the last value because of , has the lowest precedence:

if(7)

因为带有括号的展开应该看起来像

since unfolding with parenthesis should look like

if(((x == 1), 5), 7)

同时

if((x == 1) || (x == 2) || (x == 7))

检查x是否等于127.

这篇关于C ++ if语句符号-这等效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆