Can I Use Multiple Statements in an if-else?

  • Thread starter Thread starter Rhine720
  • Start date Start date
AI Thread Summary
The discussion centers around the use of braces in C++ if-else statements. While it is technically permissible to omit braces for single statements, many programmers advocate for always using them for consistency and to prevent errors, especially when modifying code later. The debate touches on readability, screen space, and coding standards, with some arguing that braces enhance clarity and reduce the risk of mistakes when adding new statements. Others mention that certain coding guidelines, such as those in the automotive and aerospace industries, mandate the use of braces to avoid potential issues. The conversation also highlights personal preferences in coding style and the importance of adhering to team standards when collaborating on code. Ultimately, the consensus leans towards using braces for better code safety and maintainability, regardless of personal style.
Rhine720
Messages
88
Reaction score
0
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?
 
Technology news on Phys.org
Yes, you can. It wastes screen space and therefore not recommended.
 
just screen space? Is wasting it bad? Like..memory wise or? Just scrolling through code? I think it looks a little more organized
 
no, just readability. There are no functional differences one way or another.
 
If you had a series of "if" statements, some with multiple action statements 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:
    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:
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.

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
 
Wetmelon said:
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

It does. I forget the menu path, but I do it all the time. Are you using one of the "Express" versions?
 
Last edited:
hamster143 said:
It wastes screen space and therefore not recommended.

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.
 
hamster143 said:
Yes, you can. It wastes screen space and therefore not recommended.
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?

jtbell said:
Ultimately it's a matter of personal preference, unless of course you're working in a group that has rules about it.
As a result of the above, many places, including where I work, make if (expression) do_something(); illegal. The braces are made mandatory.
 
  • #10
ALWAYS enclose if in {} even if it's just one line.
otherwise this will happen to you.
Code:
if ( blah ) 
    dosomething()
Somebody (probably you on a bad day) will change it to
Code:
if (blah )
   cout << blah
   dosomething()
Much safer to do
Code:
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
 
  • #11
one line if statements can be safely done like:

Code:
(booleanExpression) ? ifTrue() : NULL;
 
  • #12
DavidSnider said:
It does. I forget the menu path, but I do it all the time. Are you using one of the "Express" versions?

Nope, Professional.
 
  • #13
D H said:
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?

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.
 
  • #14
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.)
 
  • #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

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:
  • #16
hamster143 said:
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.


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.

hamster143 said:
Here's part of the source of Linux kernel:

http://www.cs.fsu.edu/~baker/devices...x/kernel/cpu.c

This is gnu make:

http://github.com/rocky/remake/blob/...ef2d2e8/main.c

As you can see, if() is extensively used without superfluous brackets.

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:
  • #17
shoehorn said:
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.
That was exactly my point.
 
Back
Top