Slothrop
Slothrop

Reputation: 142

Difference between block and in-line if statement

In C++ is there any difference in the assembly code generated by a statement such as:

if (*expr*) { }

vs.

if (*expr*) return;  

Basically what I want to know is whether or not delimiting an if statement with brackets makes any difference to the underlying code generated than a simple return statement, as above.

Upvotes: 1

Views: 362

Answers (6)

Markus Kull
Markus Kull

Reputation: 1479

With poorly-written macros nasty things can happen.

// c-style antipattern. Better use scoped_ptr
#define CLEANUPANDRETURN delete p; return

if (*expr*) CLEANUPANDRETURN;

// preprocesses to: (with added linebreaks)
if (*expr*)
  delete p;
return;

Many functions of the C-Library may actually be macros. See Why use apparently meaningless do-while and if-else statements in macros? for a trick of somehow safer macros.

Upvotes: 0

Rakholiya Jenish
Rakholiya Jenish

Reputation: 3223

If there is only one statement, then there is no difference in using "block" and "writing inline".

Braces are used only to enclose a sequence of statements that are intended to be seen as a single process.

General syntax for if:

if ( condition_in_the_diamond )
    statement_to_execute_if_condition_is_true;

So to make multiple lines of code as a single process, we use braces.

Hence if you have only one statement to execute in if statement, the it would be similar.


Using braces are better because it reduces the chances of error. Suppose you are commenting a line of code in hurry to debug:

if(condition)
//    statement1;
statement2;        //this will bring statement2 in if clause which was not intended

or while adding a line of code:

if(condition)
    statement1;
    statement3;    // won't be included in if
statement2;

But if you are using inline statement as:

if(condition) statement1;

then it might prevent you from above error but it will make statement1 of limited length (assuming 80 character width code). That will make it sweet and simple to read though.

Hence unless you are using inline statement, using braces is suggested.

Source

Upvotes: 0

dlask
dlask

Reputation: 8982

This function …

void f1(int e) {
    if (e) {}
}

… compiles to …

f1:
    rep ret

… while this function …

void f2(int e) {
    if (e) return;
}

… compiles to …

f2:
    rep ret

… when optimization is enabled using the -O2 option.

Upvotes: 0

legends2k
legends2k

Reputation: 32904

If there's going to be only one statement within the block then both are identical.

if (e)
{
   stmt;
}

if (e) stmt;

are the same. However, when you've more than one statement to be executed, it's mandatory to wrap them in {}.

Upvotes: 2

Bathsheba
Bathsheba

Reputation: 234695

Aside from the return statement, there is no difference.

A compiler may optimise out the first case altogether if the expression is either compile-time evaluable (e.g. sizeof) or has no side-effects.

Similarly, the second case might be optimised out to a simple return;

Upvotes: 0

Kieren Pearson
Kieren Pearson

Reputation: 470

No, there is no difference between the two examples. You can use a profiler to see what assembly code is outputted and see for your self.

Upvotes: 1

Related Questions