J. C. Leitão J. C. Leitão - 1 year ago 80
C Question

If-Else-Return or just if-Return?

Suppose I have an

statement with a
. From the efficiency perspective, should I use

if(A > B):
return A+1
return A-1


if(A > B):
return A+1
return A-1

Can someone explain me the difference? Is there any difference in a compiled language (e.g. C) vs an interpreted one (e.g. Python)?

Answer Source

Since the return statement terminates the execution of the current function, the two forms are equivalent (although the second one is arguably more readable than the first).

The efficiency of both forms is comparable, the underlying machine code has to perform a jump if the if condition is false anyway.

Note that Python supports a syntax that allows you to use only one return statement in your case:

return A+1 if A > B else A-1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download