# Short-circuit evaluation in Programming

Last Updated : 16 Jul, 2022

Short-Circuit Evaluation: Short-circuiting is a programming concept in which the compiler skips the execution or evaluation of some sub-expressions in a logical expression. The compiler stops evaluating the further sub-expressions as soon as the value of the expression is determined. Below is an example of the same:

## C++

 `if` `(a == b || c == d || e == f) {` `    ``// do_something` `}`

Explanation: In the above expression, If the expression a == b is true, then c == d and e == f are not evaluated because the expression’s result has already been determined. Similarly, if the logical AND (&&) operator is used instead of logical OR (||) and the expression a == b is false, the compiler will skip evaluating other sub-expressions.

Example 1:

## C++

 `// C program to illustrate the concept` `// of short circuiting` `#include `   `// Driver Code` `int` `main()` `{` `    ``int` `x = 1;`   `    ``if` `(x || ++x) {` `        ``printf``(``"%d"``, x);` `    ``}`   `    ``return` `0;` `}`

Output

`1`

Explanation: In the above C program, inside the if statement, the value of the first sub-expression i.e., x is 1 (which means true in boolean), so the value of the second sub-expression does not affect the value of the expression and hence the compiler skips checking it. So the value of x is not incremented.

Example 2:

## C

 `// C program to illustrate the concept` `// of short circuiting` `#include `   `// Driver Code` `int` `main()` `{` `    ``int` `x = 1;` `    ``int` `y = -1;`   `    ``// Since x == 50 evaluates to false, the second sub-expression is` `    ``// also evaluated, value of y is incremented to 0 (false in boolean). ` `    ``// As a result else block is executed and the incremented value of y is ` `    ``// printed from within the else block` `    ``if` `(x == 50 || ++y) {` `        ``printf``(``"if block executed\n"``);` `        ``printf``(``"Value of y: %d"``, y);` `    ``}` `    ``else` `{` `        ``printf``(``"else block executed\n"``);` `        ``printf``(``"Value of y: %d"``, y);` `    ``}` `    ``return` `0;` `}`

Output

```else block executed
Value of y: 0```

Explanation: Since x == 50 evaluates to false, the second sub-expression is also evaluated, and the value of y is incremented to 0 (false in boolean). As a result else block is executed and the incremented value of y is printed from within the else block.

Applications: The concept of short-circuiting can be helpful in many scenarios. Some of them are listed below:

• Avoiding unexpected behavior: It can be used to avoid unexpected behavior due to the second argument. For example: Consider the below code snippet:

## C

 `// C program to illustrate the concept` `// of short circuiting` `#include `   `// Driver Code` `int` `main()` `{` `    ``float` `nr = 5, dr = 0;` `    ``dr && ``printf``(``"a/b = %.2f"``, nr / dr);` `}`

Output

`// No output`

Explanation: Here since the value of dr is 0 (false in boolean), the expression nr/dr inside printf will not be evaluated and runtime error will be avoided.

• Avoiding Expensive Computation: It can be helpful in avoiding expensive computations which are required to be executed only under specific conditions. Below is the code snippet to illustrate the same:

## C

 `int` `a = 0;`   `// myfunc(b) will not be called` `if` `(a != 0 && myfunc(b)) {` `    ``// do_something();` `}`

Explanation: In the above example, myfunc() will never be called since a != 0 evaluates to false. Thus, if an expensive operation is to be carried out using myfunc() only under certain conditions, then the expensive operation can be avoided from being carried out every time by using a suitable condition for the first argument.

This can also be used in file handling to avoid the expensive task of getting a file ready every time if the file is already in a ready state as

Advantages Of Short-Circuit Evaluation:

• It can be helpful in avoiding computationally expensive tasks under certain circumstances.
• It provides a check for the first argument without which the second argument may result in a runtime error.

Disadvantages Of Short-Circuit Evaluation:

• It can cause unexpected behavior if not used properly. If some operation that does some kind of allocation of system resources/memory allocation gets skipped due to short-circuiting, we may get unexpected behavior.
• Code execution becomes less efficient with short-circuited execution paths because in some compilers the new checks for short-circuits are extra execution cycles in themselves.