Thread: Which one is way if(i==4) or if(4==i)

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    24

    Post Which one is way if(i==4) or if(4==i)

    Hi All,

    Which one is correct way


    Code:
    if(i==4)
    {
        /*Do something*/
    }

    Code:
    if(4==i)
    {
        /*Do something*/
    }
    considering
    Code:
    i
    is any type of variable,


    Also sometimes I have seen this


    Code:
    if(i==(u8)4)
    {
    /*Do something*/
    }
    considering u8 as unsigned char type and i is any type of variable.

    what is the significance of typecasting the decimal value 4 to unsigned char?


    Thanks in advance
    Last edited by shaswat; 03-16-2016 at 01:06 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shaswat
    Which one is correct way
    Both ways are correct, so it is just a stylistic preference. Some people advocate if (4 == i) because if you accidentally write if (4 = i) it will be a compile error, but compilers typically can be configured to warn about if (i = 4), which will also detect a possible if (i = j) mistake that the if (4 == i) style cannot.

    Quote Originally Posted by shaswat
    what is the significance of typecasting the decimal value 4 to unsigned char?
    It is quite complicated to reason about "i is any type of variable", and it wouldn't make sense for many such cases anyway, so let's simplify it to "i is of an arithmetic type".

    What happens is that the usual arithmetic conversions are performed on both operands. If i is of long double, double or float, then the (u8)4 will be converted to the corresponding type, so the cast has no net effect. Otherwise, the integer promotions are performed on both operands, hence the (u8)4 will be promoted to int, so the cast has no net effect. Therefore, the typecast is not significant here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    4 == i is a pointless trick which doesn't even work all the time.
    Question 17.4

    Instead of remembering one fact (use == rather than =), you now have to remember two facts (use == rather than =, and remember to swap things around).

    So why is it pointless?
    1. It destroys readability. It falls foul of the usual English semantics of subject (i) and object (4). If you were describing what the code was doing to someone else, you would always say "i equals 4". Describing the code one way, and writing the code another way is a sure-fire way of adding bugs, not removing them.

    2. It only works when one of the terms cannot be an l-value. At the moment it is most critical, the safety net is gone when you have if ( i = j ). No amount of rearranging the deck chairs on the Titanic is going to save you.

    3. Even when the left term is naturally a non-lvalue, people insist on applying the rule, so you end up with weirdness like if ( 0 == strcmp(a,b) ). if ( strcmp(a,b) = 0 ) would have been an error anyway, without any additional help.

    4. Some proponents go overboard and start reversing other operands (like say <). But changing if ( i < 4 ) into if ( 4 >= i ) is a rather more complicated task which I've seen people screw up on from time to time.

    5. Back in the 1980's when compilers were dumb, and only reported syntax errors, it may have had some benefit. Nowadays, any modern compiler worthy of the name will diagnose this (any many other horrors) for you without resorting to semantic trickery.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Salem View Post
    4. Some proponents go overboard and start reversing other operands (like say <). But changing if ( i < 4 ) into if ( 4 >= i ) is a rather more complicated task which I've seen people screw up on from time to time.
    I don't know if you did that on purpose, but that's a great illustration of how someone can screw up the condition by swapping the order (it should be 4 > i, not 4 >= i).

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    24
    Hi,

    Any explanation about typecasting an arithmetic value like I asked

    if(i==(u8)4)

    What's the reason behind it?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shaswat
    Any explanation about typecasting an arithmetic value like I asked

    if(i==(u8)4)

    What's the reason behind it?
    I already explained that in the second part of my post #2.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by shaswat View Post
    Which one is correct way
    Code:
    if(i==4)
    Code:
    if(4==i)
    There is no "correct" way here because both statements are logically the same. In a simple case like this they are also equally readable, but with a longer expression you are free to choose whichever form you find most straightforward. Note also that this rule is something that should have been learned from elementary algebra.

    As far as style goes, writing the conditional in the second way is informally called a "Yoga conditional" and is done to avoid "accidentally" writing '=' when '==' was intended. For example:

    Code:
     if (i=4) ... /* oops */
    However, all compilers nowadays will warn about this, so the real problem with using yoda conditionals is not about readability, but about what their use implies: it means the user of Yoda conditionals probably didn't run their code through any sort of static checking (not good).

  8. #8
    Registered User
    Join Date
    Oct 2013
    Posts
    24
    Hi laserlight,

    Your answer was perfect and I got your's but just wanted know from others also.

Popular pages Recent additions subscribe to a feed

Tags for this Thread