-1

I know this is silly, but I'm quite curious to know that is there any speed difference using conditional statement (i.e. for, if, while, do while) in C++ with braces and without braces.

FYI : I know that without braces only single statement is executed. Also, it is not a good practice to use conditional blocks w/o braces.

But the confusion is about the speed. Suppose we have only a single statement to be executed either in a for loop of if block, then which one of the two approaches is faster.

Example:

CASE 1:

 if(condition){
    only one statement here
 }

CASE 2:

 if(condition)
    only one statement here

Do they really have a difference in their execution time? If yes, which one is faster?

1
  • 1
    It's purely a matter of taste, but I do tend to just use brackets all the time. it makes the code easier to expand later, easier to read if the single statement for the condition wraps lines, and saves you the trouble of making the decision every time you have a single line conditional. It also saves you if you paste the code to another editor where whitespace is not expressed the same way. Commented Aug 18, 2020 at 10:10

1 Answer 1

3

No difference in execution time, the assembly that the compiler generates is the exact same for both. The only reasons why you use one or the another are readability related.

0

Not the answer you're looking for? Browse other questions tagged .