Can I Use Multiple Statements in an if-else?

  • Thread starter Rhine720
  • Start date
In summary, in C++, only a single statement can be used in an if-else statement unless you use braces. The use of braces is a matter of personal preference and can help with readability and consistency. However, some organizations or industries may have rules against using if statements without braces. Ultimately, the decision to use braces or not is up to the programmer, but using braces can prevent potential errors and make the code more readable.
  • #1
Rhine720
88
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
  • #2
Yes, you can. It wastes screen space and therefore not recommended.
 
  • #3
just screen space? Is wasting it bad? Like..memory wise or? Just scrolling through code? I think it looks a little more organized
 
  • #4
no, just readability. There are no functional differences one way or another.
 
  • #5
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:
  • #6
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
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:
  • #8
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.
 
  • #9
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.
 

Related to Can I Use Multiple Statements in an if-else?

1. Can I use multiple statements in an if-else statement?

Yes, you can use multiple statements in an if-else statement by enclosing them in curly braces ({ }). This allows you to execute more than one statement depending on the condition.

2. How many conditions can I have in an if-else statement?

An if-else statement can have one condition and one else statement. However, you can use multiple else-if statements to add more conditions to your code.

3. Can I use logical operators in an if-else statement?

Yes, you can use logical operators such as && (AND), || (OR), and ! (NOT) to combine multiple conditions in an if-else statement. This allows you to create more complex conditions and execute different statements accordingly.

4. Is it possible to have nested if-else statements?

Yes, you can have nested if-else statements where one if-else statement is placed inside another. This allows you to have multiple levels of conditions and execute different statements based on those conditions.

5. Can I use if-else statements in other programming languages?

Yes, if-else statements are a fundamental control structure in most programming languages and can be used in languages like Java, Python, C++, and many others.

Similar threads

  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
2K
Replies
10
Views
995
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
4
Views
813
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
Back
Top