In this blog post, you will learn the concept of short circuit evaluation. We will cover what short circuit evaluation is and its benefits in programming.
Short Circuit Evaluation:
The short-circuit evaluation has, minimal evaluation, or McCarthy evaluation is the concept of programming language in which an expression is stopped from being evaluated as soon as its outcome is determined.
C/C++ programming language has the following short circuit operators, &&, ||, ?
. You can use these operators for short-circuit evaluation.
Consider the below example,
if ((x != y )|| (y == z)) { //Code }
In the above conditional expression if (x !=y) is true, then the expression (y==z) is never-ever evaluated.
Now you are thinking why?
The reason is that expression’s outcome has already been determined by the expression(x!=y).
But if (x!=y) is false, then (y==z) is evaluated.
Let’s see a C program to understand the above code.
/* C program to explain the concept of short circuiting */ #include <stdio.h> int main() { int x = 10; if ((x==10) || ++x) { printf("%d", x); } return 0; }
Output: 10
Explanation:
In the above code expression (x==10) is true, so the expression ++x is not evaluated. The evaluation of ++x does not affect the value of the controlling expression it is the reason the compiler skips evaluating it. So the final output is 10.
Advantages Of Short-Circuit Evaluation:
Let’s see some good examples that illustrate the benefits of short-circuiting. This example may help to visualize the benefits of short-circuiting.
1. One of the great examples of short-circuiting null pointer check:
Consider the below example code,
/* C program to explain the concept of short circuiting */ #include <stdio.h> struct Date { long unsigned int timeStampl; }; void display(const struct Date *pTr) { if((pTr != NULL) && (pTr->timeStampl == 100)) { printf("Time Stamp = %u\n",pTr->timeStampl); } else { printf("Hey Aticleworld:- Null pointer\n"); } } int main() { // Calling display with null pointer display(NULL); // Now calling with valid address struct Date date = {100}; printf("\n\nNow calling display with valid address\n"); display(&date); }
Output:
Hey Aticleworld:- Null pointer Now calling display with valid address Time Stamp = 100
Explanation:
If the control expression is not a short circuit then the behavior of the code pTr->timeStampl
will be undefined when pTr is null. But because the expression short-circuits if pTr!= NULL
is false, then the pTr->timeStampl
code never evaluates and you will not get UB.
2. Another great example of avoiding unexpected behavior:
So let’s see the example code to understand how to short-circuit and avoid the undefined behavior.
/* C program to explain the concept of short circuiting */ #include <stdio.h> int main() { int nr, dr; printf("Enter Nr and Dr value\n"); scanf("%d%d",&nr,&dr); const int retVal = (dr!=0) && ((nr/dr)>1); if(retVal) { printf("Nr is greater than Dr\n"); } else { printf("Enter valid input\n"); } return 0; }
Output:
Enter Nr and Dr value 1 0 Enter valid input
As you know the behavior is undefined when the denominator is 0. So here in the expression const int retVal = (dr!=0) && ((nr/dr)>1)
. The second expression ((nr/dr)>1)
will not evaluate when the value of the denominator would be 0.
3. Avoiding Expensive Computation:
You can avoid expensive calls with the help of short-circuit techniques. Let’s see an example for the illustration,
/* C program to explain the concept of short circuiting */ #include <stdio.h> int main() { int indicator = 0; //some code to update indicator /* expensiveCall() will only execute when indicator is not 0 */ if (indicator != 0 && expensiveCall(Parameters)) { // do_something(); } return 0; }
Explanation: In the mentioned C code, expensiveCall() will call only when the indicator flag is not zero. Thus by using the above techniques you can avoid any expensive operation which should be evaluated at a certain condition.
Disadvantages Of Short-Circuit Evaluation:
1. If short-circuit is not used correctly, then it may be caused unexpected behavior. Let’s consider an example to understand it.
/* C program to explain the concept of short circuiting */ #include <stdio.h> int main() { int indicator; // Code make indicator Zero /* Because indicator is zero, so FunAllocateMemory() will never execute */ if (indicator != 0 && FunAllocateMemory(Parameters)) { // do_something(); } //Now Using of the memory that suppose to allocate in FunAllocateMemory() //but not allocate due to indicator //It would be dangerous 😓 return 0; }
2. It affects the code readability. And if you mix many conditions then it would be hard to read and can be buggy if not handled properly.
3. Short-circuiting can lead to errors in branch prediction on modern CPUs, and dramatically reduce performance
Recommended Post
- Logical Operator in C/C++.
- Equality and Relational Operators in C.
- Ternary operator in C.
- Undefined Behavior in C and C++.
- Operator Precedence And Associativity In C.
- Token Pasting Operator in C/C++ programming.
- C Programming Courses And Tutorials.
- CPP Programming Courses And Tutorials.
- Best Gifts for the programmer and techies.
- Python Courses and Tutorials.
- Difference Between C Structure and C++ Structure.
- Rvalue reference declarator: &&.
- Difference Between C and C++ programming language.
- What is a dynamic constructor in C++?