
semantically, there's no difference between the two.. this practice is just to help the compiler spot an error if in case only 1 equals sign was written
if ( variable == TRUE )
when mistakenly written as
if ( variable = TRUE )
will not trigger a compiler error even though logically, error na na..
but kung balehon nimo..
if ( TRUE == variable )
a mistake like this
if ( TRUE = variable )
will be spotted by compiler right away..

nice point
in C++, I saw some people do this way
if (8080==port)
they use it only for numerical values and not for boolean expressions.
but if you asked me, I never do this, I used the conventional way: if(port==8080).
I don't know what compiler you are talking here... but if I do it accidentally this way,
if (x = 1)
It throws a compile time error... Cannot implicitly convert type 'int' to 'bool', you declared x as int, if you wrap it with "if" statement, a good compiler that supports stronger type check will give you an error.
anything that is a constant value, not just boolean or others.. even string literals.. basta constant value..
if ("YES" == response)
if (true == choice)
if (3 == count)
you are probably using .Net.. it has all those checks in the compiler.. in a way, it also makes your code too explicit and kinda redundantly messy..
other compilers have implicit conversions.. obviously, yours doesn't have one..
.NET is nothing to do with this, its just a set of classes anyway I tried on my other C compiler ISO/C99.
compile error: warning #2030: '=' used in a conditional expression. (solves the problem)
On the other hand, it runs on my old C++ compiler (which supposedly not) but if you use the syntax check (which a good practice before compiling) you will get the error.
I don't want to comment about how you evaluate explicit conversion, explicit and implicit have both disadvantage as what I have observed in Python and C++, yet I haven’t seen anything that is messy to methere’s always been a good way.
messy na kaau ni na style ang explicit conversion..
what if choice is integer, and 0 is considered false..
this will result in errorif (choice = getChoice())
so you'd have to explicitly convert it or some other..
orchoice = getChoice()
if ((bool) choice)
but anyway, its programmer's preference... so its up to you which u prefer..choice = getChoice()
if (choice != 0)
Similar Threads |
|