This topic has been archived. It cannot be replied.
-
工作学习 / 专业知识杂谈 / 工作2个星期,manager突然给了一套interview question,让我们周末去做, 其中有一题是:if(i==3){} 和if(3==i){} 的区别是什么? 请教各位帮忙,多谢!
-jasonwjq(jasonwjq);
2005-9-30
(#2529729@0)
-
If you make mistakens, if(i=3){} 和if(3=i){} , then if(3=i){} is better.
-armfeel(wireless);
2005-9-30
(#2529739@0)
-
what kind of mistakes, how those mistakes happen? thank you.
-jasonwjq(jasonwjq);
2005-9-30
(#2529751@0)
-
3==i is better, for example, i == null
-mixer(长船蝉床);
2005-9-30
(#2530042@0)
-
can you assign null to int?
-oxknife(天下无猪);
2005-9-30
(#2530082@0)
-
不易出错。
-canadiantire(轮胎-M.I.N.K.);
2005-9-30
(#2529742@0)
-
什么错误,如何发生,谢谢
-jasonwjq(jasonwjq);
2005-9-30
(#2529754@0)
-
好习惯:3==i 因为如果写为if(i==3){}时容易误写为if(i=3){}付值语句条件永远成立。
-hahahaly(hahahaly);
2005-9-30
(#2529803@0)
-
Thank you so much.
-jasonwjq(jasonwjq);
2005-9-30
(#2529822@0)
-
当你误写if (i=3}时,程序照样编译通过,这个错误不易发现。而当你误写成if (3=i),编译器会捕捉到问题。如果是在Windows下用Visual Studio等编程,可以通过打开编译开关W3甚至W4来防止类似错误。
-collapsar(笨笨和旦旦);
2005-10-6
(#2540795@0)
-
其实我自己比较喜欢写成i==3, 写成3==i读起来不顺口。一来靠经验保证,二来很多编译器GCC/VC都对if i=3有warning
-google2002(Google);
2005-10-6
(#2540977@0)
-
有时候 Warning 太多,而你们又不是需要消除所有的时候,可能会漏掉这个.而且理论上讲, 写成 if (i = 3) 的, 应该不是故意要这样做, 应该视之为 Error, 而不是 warning.
-deepthought(deepthought);
2005-10-6
(#2540989@0)
-
Warning 太多? 我写万行程序也不会容忍一个WARNING.
-iwantcar(EnjoyStudying);
2005-10-6
(#2541076@0)
-
我们公司的Coding Standard规定必须写成 if (3 == i) 这种格式.
-deepthought(deepthought);
2005-10-6
(#2540985@0)
-
As a matter of fact, they are totally different.For example, let i=3.01, you'll see the difference.
-oasis2000(Oasis);
2005-10-7
{52}
(#2542789@0)
-
有什么不一样阿?#include<stdio.h>
main()
{
double x=3.01;
if ( x == 3)
printf ("x==3 true \n");
else
printf ("x==3 not true \n");
if ( 3 == x)
printf ("3==x true \n");
else
printf ("3==x not true \n");
}
result:
x==3 not true
3==x not true
-holdon(again);
2005-10-8
{359}
(#2543959@0)
-
use "3==i" means you are SMARTER. compiler will not complain either way.
-allenpang(皮皮丁);
2005-10-8
(#2544005@0)
-
Actually no difference in state-of-art language like java and C#. Compiler can catch problem in compilation time either way.
-goodbug9(goodbug);
2005-10-22
(#2567618@0)