在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;
}
即数值放前面可让编译器报错出直接赋值的情况,避免这种错误发生。