Thread: a QuickQuestion about "If () {}"

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    15

    a QuickQuestion about "If () {}"

    is there any real reason why some prefer one style over another whyle using them ? :
    Code:
             if (col == 1)
                cout << '*';
             else {
                if (col == height * 2)
                   cout << '*';
                else
                   cout << '0';
             }
    and the one below
    Code:
             if (col == 1)
                cout << '*';
             else if (col == height * 2)
                cout << '*';
             else
                cout << '0';
    the only diference in those 2 that i see is that in first one u can easy add a debug code or so, if the first "if" is false.
    is that the only diference and reason that it could make to be better using that style ?(first seems a bit harder to folow but a bit easyer to modifi)

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Of the two choices, I'd use neither.

    Depending on circumstances I'd be more likely to do something like this
    Code:
        if (1 == col || (2*height) == col)
        {
                cout << '*';
        }
        else
        {
                cout << '0';
        }
    My "depending on circumstances" clause means that I may do something else depending on what the variables represent, whether the various terms are reused, how clear I wish to be......

    Generally I aim for the clearest code that achieves the intended effect.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    55
    I usually don't bother with { } when there's a single instruction on conditional/loops statements, I find code to be easier to read like that

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Code:
             if (col == 1)
                cout << '*';
             if (col == height * 2)
                cout << '*';
             else
                cout << '0';
    That's how I do it.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Since grumpy's code isn't less readable, I would prefer it over yours, since it doesn't duplicate code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    15
    Elysia didnt u posted before grumpy ?(if not, i think my eyes are kinda playing triks on me .. ).
    From what i see (and i read here and there) no real diference or inpact if i use first style or 2nd style its only a preference thing and it doese not change how code works.(in my 2 exapls)

    Babkockdood is alot diferent than my exapls. He doesent trust logical OR operator so he useses 2x if's.(i mean he doesent trust the order in wich conditions are evaluated)
    or more explicit to say .. is that metod is worth using when one of the conditions to be evealuated depends on the other one or when u want/expect a short-circuit.

    ty ty all i think a bit clearer about this thing now.

    Edit
    edited coz my english suks and its a bit harder to type it all ok.
    sorry for my english to .
    Last edited by Flo; 07-04-2010 at 05:52 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Flo View Post
    Elysia didnt u posted before grumpy ?(if not, i think my eyes are kinda playing triks on me .. ).
    Maybe they do

    or more explicit to say .. is that metod is worth using when one of the conditions to be evealuated depends on the other one or when u want/expect a shortcut.
    I'm not so sure. The && and || operators can short-circuit. That is, if the left-operand is false or true respectively, no more of the expression needs to be evaluated.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    15
    or more explicit to say .. is that metod is worth using when one of the conditions to be evealuated depends on the other one or when u want/expect a short-circuit.(if u dont trust the order in wich is evaluated or if u want to be clear that its want/expected the short-circuit)

    and btw i typed shortcut instead of short-circuit. My bad .. english problem :P

    sorry for that, didnt explained why i said that to clear.
    Last edited by Flo; 07-04-2010 at 05:55 PM.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Babkockdood View Post
    Code:
             if (col == 1)
                cout << '*';
             if (col == height * 2)
                cout << '*';
             else
                cout << '0';
    That's how I do it.
    Too bad your code is not equivalent to those in the original post....
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    if (1 == col || (2*height) == col)
    I detest the order here. Unfortunately the style cops where I work force this upon us even though every single modern compiler will ask you if you really meant == instead of =. Not to mention that an equivalent/assignment bug is about the easiest thing to find in a debugger.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I'm with grumpy, but if the three cases were all different, I'd go for this:
    Code:
             if (col == 1) {
                cout << '*';
             } else if (col == height * 2) {
                cout << '+';
             } else {
                cout << '0';
             }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Jun 2010
    Posts
    15
    Quote Originally Posted by Bubba View Post
    I detest the order here. Unfortunately the style cops where I work force this upon us even though every single modern compiler will ask you if you really meant == instead of =. Not to mention that an equivalent/assignment bug is about the easiest thing to find in a debugger.
    i sure understand that and agree with you, as my good friend said that most of the time it is better to be a nice "read-able code" then to be "to-safe code".
    coz surely will need to be read/maintained in the future.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Preferably, you should have a mix of the two. Readability helps maintenance. But safe code helps catch bugs and saves time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I think the practice of switching the order does neither of the above.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Bubba View Post
    I think the practice of switching the order does neither of the above.
    Unfortunately, reported results from monitoring programmer activity and objectively measuring productivity by various measures do not agree with you. I remember reading a report from Microsoft that concluded several such techniques do have a positive effect on productivity - even allowing for tendancy of programmers to routinely complain if forced to use those techniques.

    A number of psychological studies have also concluded that humans are very poor at judging effectiveness of techniques they use in task-related activities, and often exhibit a strong bias towards using ineffective techniques in the mistaken belief they are most effective.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does a the "%" sign mean? as in "if (i % x==0)"?
    By thetinman in forum C Programming
    Replies: 2
    Last Post: 10-02-2006, 03:11 AM
  2. What does "#if 1" mean?
    By thetinman in forum C++ Programming
    Replies: 6
    Last Post: 04-04-2006, 05:48 AM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM