Results 1 to 5 of 5
Hi friends,
I have a basic question.
Is there any difference between the below statements..
Statement 1:
Code:
if (false == xyz())
{
--------------------
}
Statement 2:
Code:
if (xyz() ...
- 04-17-2009 #1Just Joined!
- Join Date
- Oct 2008
- Posts
- 7
If Condition
Hi friends,
I have a basic question.
Is there any difference between the below statements..
Statement 1:
Statement 2:Code:if (false == xyz()) { -------------------- }
Code:if (xyz() == false) { -------------------- }
where function xyz() returns bool value.
How is IF condition implemented internally??
Which one out of these above statement are the correct way of coding and why?
Thanks and warm Regards,
Veda
- 04-17-2009 #2
This sounds like homework...
Linux User #453176
- 04-17-2009 #3Just Joined!
- Join Date
- Oct 2008
- Posts
- 7
Well.. its not home work..
I see this in my company project... they are messed up... n i am new into this project .. I am into the project for some feature enhancement.
As of nw i am confused which convention to follow for the code that I am writting...
Appreciate your suggessions,
Veda
- 04-17-2009 #4
Try this its more conventional:
Code:if (!xyz()) { }Make mine Arch Linux
- 04-17-2009 #5
Indeed: for boolean statements, it is much more common to see:
However, for non-boolean comparisons with constants, there is a bit of a debate.Code:if(booleanCall()) { ... } if(!booleanCall()) { ... }
Traditionally (and most commonly), you see this:
However, some people argue that it should be:Code:if(num == 3) { ... }
The reason for this is that it is very common to accidentally use "=" instead of "==" in comparisons. If we wrote:Code:if(3 == num) { ... }
in C, num would be assigned the value 3, and the condition would be true (as in C, all non-0 numbers are true). This would also occur in Perl, or any language that uses numbers for booleans. Errors like this can be very hard to track down.Code:if(num = 3) { ... }
On the other hand, a line like this:
would throw a compile-time error, as you cannot assign a value to a constant.Code:if(3 = num) { ... }
Most people still use the first way, but as I say, there are arguments to do it the second way.DISTRO=Arch
Registered Linux User #388732


Reply With Quote