If and else question

  • Thread starter Rhine720
  • Start date
  • #1
88
0

Main Question or Discussion Point

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?
 

Answers and Replies

  • #2
907
2
Yes, you can. It wastes screen space and therefore not recommended.
 
  • #3
88
0
just screen space? Is wasting it bad? Like..memory wise or? Just scrolling through code? I think it looks a little more organized
 
  • #4
907
2
no, just readability. There are no functional differences one way or another.
 
  • #5
rcgldr
Homework Helper
8,670
506
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:
  • #6
154
0
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
 
  • #7
DavidSnider
Gold Member
485
128
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:
  • #8
jtbell
Mentor
15,501
3,296
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.
 
  • #9
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
682
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.
 
  • #10
mgb_phys
Science Advisor
Homework Helper
7,774
12
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
DavidSnider
Gold Member
485
128
one line if statements can be safely done like:

Code:
(booleanExpression) ? ifTrue() : NULL;
 
  • #12
154
0
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
907
2
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
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
682
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
907
2
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:
  • #16
422
1
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:
  • #17
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
682
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.
 

Related Threads for: If and else question

  • Last Post
Replies
2
Views
13K
Replies
6
Views
21K
  • Last Post
Replies
20
Views
1K
  • Last Post
Replies
15
Views
4K
Replies
9
Views
957
Replies
5
Views
1K
Replies
8
Views
3K
Replies
18
Views
802
Top