Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

If and else question

  1. Mar 2, 2010 #1
    So, in my C++ book it says only a single statement can be used in an if-else like

    if (blah==blahblah)
    cout<<blahblahblah
    else
    cout<<blah he ha

    unless you do

    if (blah==blahblah)
    {
    cout<<blahblahblah
    cout<<also blah
    }
    else
    {
    cout<<blah he ha
    cout<<blah le ha ha
    }

    But why can't i do

    if (blah==blahblah)
    {
    cout<<blahblahblah
    }
    else
    {
    cout<<blah he ha
    }


    ..or can i?
     
  2. jcsd
  3. Mar 2, 2010 #2
    Yes, you can. It wastes screen space and therefore not recommended.
     
  4. Mar 2, 2010 #3
    just screen space? Is wasting it bad? Like..memory wise or? Just scrolling through code? I think it looks a little more organized
     
  5. Mar 2, 2010 #4
    no, just readability. There are no functional differences one way or another.
     
  6. Mar 3, 2010 #5

    rcgldr

    User Avatar
    Homework Helper

    If you had a series of "if" statements, some with multiple action statments and others with single action statements, it might look better to use braces on all the if's.

    Although the braces aren't required, it might look neater, and if later you need to add additional statements, the braces are already there.

    I'm a bit old school, and sometimes to save screen space I rely on indentation, and not put braces on separate lines. The idea is that the braces are for the compiler, the indentation is for the reader. Adding lines to the end of a section requires moving the final brace, but this hasn't been an editting issue since the days of using punched cards for programs. It would look like this:

    Code (Text):

        if(...){
            ...;
            ...;}
        else{
            ...;
            ...;}
     
    If I'm writing code as part of a group, I go by the standards established by the group. There are also source code "beautifier" tools, that can make a copy of a source file that complies with a set of user defined rules for indentation and braces, although I haven't seen one in a long time.

    wiki article:

    http://en.wikipedia.org/wiki/Indent_style
     
    Last edited: Mar 3, 2010
  7. Mar 4, 2010 #6
    Really? Would be useful for me... VS 2008 doesn't have anything to clean up the file at the end of the day. Not that it needs MUCH cleaning, but two clicks can save some time and make sure it's formatted properly :P
     
  8. Mar 4, 2010 #7

    DavidSnider

    User Avatar
    Gold Member

    It does. I forget the menu path, but I do it all the time. Are you using one of the "Express" versions?
     
    Last edited: Mar 4, 2010
  9. Mar 4, 2010 #8

    jtbell

    User Avatar

    Staff: Mentor

    Depends on who you ask. :smile:

    I almost always enclose if- and else- blocks in curly braces even if they have just one statement, for consistency. It also makes things simpler if I want to add more statements to a one-statement block.

    I do make exceptions for things like long if/else/else/else chains in which each block contains a single statement and I think it's highly likely that it will stay that way.

    Ultimately it's a matter of personal preference, unless of course you're working in a group that has rules about it.
     
  10. Mar 4, 2010 #9

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    This is but one of the several bad design decisions of the C language that has led to several dramatic errors. There are many problems with if (expression) do_something(); Just a couple, what if do_something() is a (poorly-constructed) macro that expands into multiple statements? What if the statement is written in two lines, properly indented, and some maintenance programmer, not seeing the lack of braces, adds a statement before the do_something() statement?

    As a result of the above, many places, including where I work, make if (expression) do_something(); illegal. The braces are made mandatory.
     
  11. Mar 4, 2010 #10

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    ALWAYS enclose if in {} even if it's just one line.
    otherwise this will happen to you.
    Code (Text):

    if ( blah )
        dosomething()
     
    Somebody (probably you on a bad day) will change it to
    Code (Text):

    if (blah )
       cout << blah
       dosomething()
     
    Much safer to do
    Code (Text):

    if ( blah ) {
        dosomething()
    }
     
    ps the position of the braces, on which line, has no effect - and so is argued over fiercely http://en.wikipedia.org/wiki/Indent_style
     
  12. Mar 4, 2010 #11

    DavidSnider

    User Avatar
    Gold Member

    one line if statements can be safely done like:

    Code (Text):

    (booleanExpression) ? ifTrue() : NULL;
     
     
  13. Mar 4, 2010 #12
    Nope, Professional.
     
  14. Mar 4, 2010 #13
    By that logic, you should always write " a = (b) * (c); " instead of "a = b * c; " because there's risk that someone will make 'b' and 'c' into poorly-constructed macros.
     
  15. Mar 4, 2010 #14

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Nice that you omitted the second case.

    The advice you gave in post #2 is contrary to the automotive industry (google MISRA-C rule 59), NASA, DoD, and several other organizations. They explicitly outlaw the use of an if statement sans the braces. (Similarly, else, do, while, and for require braces per those rules.)
     
  16. Mar 4, 2010 #15
    Any programmer who's clueless enough to insert code between if() and the conditional statement can just as easily insert code between if() and the opening brace! You can't protect code from modification by clueless programmers. You can, however, make sure that the code is readable and transparent, by avoiding superfluous brackets and adhering to ANSI indent style.

    Here's part of the source of Linux kernel:

    http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/kernel/cpu.c [Broken]

    This is gnu make:

    http://github.com/rocky/remake/blob/9c5a95cb13813a857f19e91dcdf159122ef2d2e8/main.c

    As you can see, if() is extensively used without superfluous brackets.
     
    Last edited by a moderator: May 4, 2017
  17. Mar 4, 2010 #16

    DH's point, if I read it correctly, is that nobody is attempting to prevent clueless programmers from making mistakes with if/else. The purpose of imposing the use of brackets with such structures as a coding standard is that it helps to prevent all programmers, even those with plenty of clue, from making thoughtless errors. Even ignoring the fact that this is sound advice, I've not personally ever worked in an environment where this particular code convention was not followed.

    That's wonderful; whatever works for them is presumably fine with them. However, if I'm paying you to work for me coding financial models, I expect you to follow my coding guidelines. Anyone who deliberately ignored those guidelines in favour of their own, "better," way of doing things would find a P45 on their desk pretty sharpish.
     
    Last edited by a moderator: May 4, 2017
  18. Mar 4, 2010 #17

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    That was exactly my point.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: If and else question
  1. Java If Else Indentation (Replies: 15)

Loading...