if判断value == variable

来源:互联网 发布:迅雷赚钱宝秒杀软件 编辑:程序博客网 时间:2024/06/16 20:38

在C++里面的判断,有人把通常的variable == value 写成 value == variable是有原因的。

#include <iostream>using namespace std;int main(){    int a = 2;    if(a == 1)        cout << "happy more!" << endl;    else         cout << "more happy!" << endl;    return 0;}


#include <iostream>using namespace std;int main(){    int a = 2;    if(a = 1)        cout << "happy more!" << endl;    else        cout << "more happy!" << endl;    return 0;}


#include <iostream>using namespace std;int main(){    int a = 2;    if(1 == a)        cout << "happy more!" << endl;    else        cout << "more happy!" << endl;    return 0;}





即数值放前面可让编译器报错出直接赋值的情况,避免这种错误发生。

0 0
原创粉丝点击