time complexity of empty block

does an empty block of statement also affect the time complexity of the code ?
eg if(condition) { } else { }

Time complexity is measured as a function of the input. So it needs to depend on the role of the input in the condition.
Anyway, the statement definitely contributes to the running time of the program, since the condition has to be evaluated if the code is reachable.

Same thought as @ajayshan2004. But, if condition fails and controls come to else part for execution and it is empty. Then it depends on compiler. How he handle the code, my guess if its empty then at compile time, compiler just ignore this code. And it is meaningless. So, it will not add any execution time.

@vinayak garg : guess i missed a few words, i need to know 'bout both.
runtime obviously for smarter codes, and compilation time cause i’ve always wanted to build a much easier-faster-lightweight programming language.

what if the condition statement as well as the body is missing and there will be no condition to evaluate.
eg. if() {}

@codeanindya it will give compile error.

1 Like

okay yeah it will. But what in case of else { }, when if case doesn’t match the control comes to else for execution and else is empty, will there be some execution time spent to move through it ?

@codeanindya: Today’s compiler are smart enough to do trivial optimizations like eliminating unreachable code, loop unrolling etc.

Now you can imagine even a simplest compiler will not emit code for empty block.

2 Likes

@codeanindya: Are you sure you want to ask about compilation time instead of runtime?