Short-Circuiting in C++ and Linux
Last Updated :
17 Dec, 2021
Short-circuiting is one of the optimization steps of the compiler, in this step unnecessary calculation is avoided during the evaluation of an expression. Expression is evaluated from left to right. It works under certain cases when the value of the expression can be calculated certainly by only evaluating parts of the expression.
Short-circuiting in C++
In C++ short-circuiting occurs while evaluating '&&' (AND) and '||'(OR) logical operators. While evaluating '&&' operator if the left-hand side of '&&' gives false, then the expression will always yield false irrespective of the value of the right-hand side of '&&', so checking right-hand side of '&&' makes no sense. So, in this situation evaluation of the right-hand side is avoided by the compiler. Similarly, in the case of logical OR '||' operation when the left-hand side gives 'true', the result of the expression will always be true irrespective of the value of the right-hand side.
Short-circuiting using AND(&&) and OR(||) logical operator.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Short circuiting
// logical "||"(OR)
int a = 1, b = 1, c = 1;
// a and b are equal
if (a == b || c++) {
cout << "Value of 'c' will"
<< " not increment due"
<< " to short-circuit"
<< endl;
}
else {
cout << "Value of 'c' "
<< " is incremented as there"
<< " is no short-circuit"
<< endl;
}
// Short circuiting
// logical "&&"(OR)
if (a == b && c++) {
cout << "Value of 'c' will"
<< " increment as there"
<< " is no short circuit"
<< endl;
}
else {
cout << "Value of 'c' will"
<< " not increment due to short circuit"
<< endl;
}
}
Output:Value of 'c' will not increment due to short-circuit
Value of 'c' will increment as there is no short circuit
Short Circuiting in Linux
In Linux, short-circuiting takes place while evaluating '&&' and '||' operators just like C++.
Logical AND(&&) short circuiting:
PHP
if [[ "$1" -gt 5 ]] && [[ "$1" -lt 10 ]]; then
echo "This output will not be printed"
else
echo "This output will be printed"
" actually due to short circuit"
fi
if [[ "$1" -lt 5 ]] && [[ "$1" -lt 10 ]]; then
echo "This output will be printed"
" as there will be no short circuit"
else
echo "This output will be printed"
" actually due to short circuit"
fi
Output:
Logical OR(||) short-circuiting:
PHP
if [[ "$1" -lt 5 ]] || [[ "$1" -gt 10 ]]; then
echo "This output will be printed"
" actually due to short circuit"
else
echo "This output will be printed"
" as there will be no short circuit"
fi
if [[ "$1" -gt 5 ]] || [[ "$1" -lt 10 ]]; then
echo "This output will be printed"
" as there will be no short circuit"
else
echo "This output will be printed"
" actually due to short circuit"
fi
Output:

Note: '&&' operator is often used as a replacement to 'if' statement.
For example, to check for the existence of a file you could use either of the below:
- if [[ -e "$filename" ]]; then
echo "exists"
fi - [[ -e "$filename" ]] && echo "exists"
In this case, bash first executes the expression on the left of the &&(and). If that expression has a non-zero return value (i.e. it failed), then there is no need to evaluate the right side of the && because the overall exit status is already known to be non-zero since both sides have to return a success indicator for a logical and to return success.
Similar Reads
How To Compile And Run a C/C++ Code In Linux C Programming Language is mainly developed as a system programming language to write kernels or write an operating system. C++ Programming Language is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance. In this article, we will be compiling and exe
4 min read
How to Compile and Run C/C++/Java Programs in Linux C is a procedural programming language. It was initially developed by Dennis Ritchie between 1969 and 1973. It was mainly developed as a system programming language to write an operating system. The main features of C language include low-level access to memory, a simple set of keywords, and clean s
3 min read
Understanding OS using LINUX Operating Systems (OS) are the backbone of modern computing. They help manage hardware, software, and provide an interface for users to interact with a computer. Linux, a widely-used and open-source operating system, is an excellent example to understand the concepts of an OS. In this article, we wi
7 min read
Fork() Bomb Prerequisite : fork() in CFork Bomb is a program that harms a system by making it run out of memory. It forks processes infinitely to fill memory. The fork bomb is a form of denial-of-service (DoS) attack against a Linux based system.Once a successful fork bomb has been activated in a system it may
3 min read
Unformatted input/output operations In C++ In this article, we will discuss the unformatted Input/Output operations In C++. Using objects cin and cout for the input and the output of data of various types is possible because of overloading of operator >> and << to recognize all the basic C++ types. The operator >> is overlo
4 min read
Setting up a C++ Competitive Programming Environment In this article, we will learn about how to setup all in one Competitive Programming Environment Operating System It is always recommended to use a Linux based OS. It is so because not only you will learn some better know-hows of the system but also will be able to get some pre-installed coding tool
5 min read