Find the answer to your Linux question:
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() ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    7

    Smile If Condition

    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() == 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

  2. #2
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    This sounds like homework...
    Linux User #453176

  3. #3
    Just 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

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Try this its more conventional:

    Code:
    if (!xyz())
    {
    
    }
    Make mine Arch Linux

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Indeed: for boolean statements, it is much more common to see:
    Code:
    if(booleanCall()) { ... }
    if(!booleanCall()) { ... }
    However, for non-boolean comparisons with constants, there is a bit of a debate.

    Traditionally (and most commonly), you see this:
    Code:
    if(num == 3) { ... }
    However, some people argue that it should be:
    Code:
    if(3 == num) { ... }
    The reason for this is that it is very common to accidentally use "=" instead of "==" in comparisons. If we wrote:
    Code:
    if(num = 3) { ... }
    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.

    On the other hand, a line like this:
    Code:
    if(3 = num) { ... }
    would throw a compile-time error, as you cannot assign a value to a constant.

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...