# If and else question

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?

Related Programming and Computer Science 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.

rcgldr
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:
    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

DavidSnider
Gold Member
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:
jtbell
Mentor
It wastes screen space and therefore not recommended.

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.

D H
Staff Emeritus
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?

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.

mgb_phys
Homework Helper
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

DavidSnider
Gold Member
one line if statements can be safely done like:

Code:
(booleanExpression) ? ifTrue() : NULL;

It does. I forget the menu path, but I do it all the time. Are you using one of the "Express" versions?
Nope, Professional.

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.

D H
Staff Emeritus
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.)

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:
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 [Broken]

This is gnu make:

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

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:
D H
Staff Emeritus
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.