Thread: if condition

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    8

    if condition

    Are are any significant difference between the following two satements ?

    1) if ( 0 == strcmp(str1, str)) ..

    2) if ( strcmp(str1, str) == 0)) ...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Only that the first is a lot more unreadable than the second.

    The supposed advantage is that it won't compile if you do if ( 0 = var )
    Question 17.4

    But since strcmp(x,y) = 0 would also not compile, it is a moot point in this case.

    Where people got all out of shape over it was with if ( var = 0 ) becoming if ( 0 = var ), which would indeed be a good thing.
    So they apply the same rule to lots of other cases, like swapping if ( i < 10 ) into if ( 10 > i ), which makes almost no sense at all when read out loud.

    But when you have if ( var1 = var2 ), then the safety net has gone, and no amount of rearranging will save you from not knowing about ==

    It was a semi-popular idea in the 1980's (like dumb xor-swaps were). But nowawdays, any half-decent compiler will warn you if you do if ( var = something ), even for the troublesome case of if ( var1 = var2 )

    For me, the first order for a program is that it should be readable. That is, consistently indented and using familar idioms.
    Syntactic tricks to solve a problem which doesn't exist waste the time of EVERY person who has to read the code. Code gets read a lot more times than it is written.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If condition
    By rits in forum C Programming
    Replies: 3
    Last Post: 09-02-2009, 05:54 AM
  2. while condition question..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 04:49 PM
  3. SDL Condition variables.
    By antex in forum Game Programming
    Replies: 3
    Last Post: 11-11-2005, 07:11 AM
  4. Looping condition
    By Chaplin27 in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2005, 02:06 PM
  5. c++ condition use? how u use it? help
    By mikeasianlee in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2004, 09:44 PM