top of page

Short-Circuit Evaluation in Programming

  • Writer: Alex Ricciardi
    Alex Ricciardi
  • Sep 17
  • 6 min read

Updated: Oct 11

This article explains the programming concept of short-circuit evaluation, starting with the conditional logic and compound expressions features.Then, it explains how the process improves code efficiency and helps prevent runtime errors, using both pseudocode and specific Python examples.


Alexander S. Ricciardi

September 17, 2025

Man explaining code at a board about short-circuit evaluation in Python; laptop on table. Casual setting, technical focus.

In computer programming, writing efficient and robust code is essential for developing stable and performant applications. To help with this goal, modern programming languages like Python have incorporated features such as short-circuit evaluation. Short-circuit evaluation is the process of evaluating compound logical expressions from left to right and stopping as soon as the final outcome is determined. This means that not all parts of the expression are necessarily checked. This provides efficiency and can be used as a safeguard against runtime errors, such as division by zero or attempting to access attributes of a null object. To understand the short-circuit process in programming, it is important first to understand what compound conditional expressions are. This article explains the mechanics of the short-circuit evaluation process. It begins with the concepts of conditional logic and compound expressions, using illustrations, pseudocode, and Python examples.

 

Comparison Operators

 

In programming, a condition compares the state (the condition) of an object, a variable, or an expression, to some expected value or criteria. This state is compared using Boolean expressions, which are expressions that return true or false. For example, the Boolean expression x == 10, which means “is x equal (‘==’ equality comparison operator) to 10?”, will return true or false depending on the value stored by the variable x. See Table 1 for a list of the different comparison operators

 

Table 1

Comparison Operators

Table of comparison operators with symbols, descriptions, and examples in a blue and white design. Includes "Equal to" (==) and more.

Note: The table provides a list of the various comparison operators used in programming. It also provides a description and a usage example for each operator.

 

Conditional Expression

 

On the other hand, a conditional expression is an expression that uses conditional statements, such as the ones depicted in Figure 1, to execute (or not) a block of code based on a condition.

 

Figure 1

Conditional Statements in Programming

Flowchart of conditional statements: "if", "if-else", "if-else-if", and "switch", illustrating code logic paths. Black text on a white background.

Note: The diagram depicts the types of conditional statements used in programming.

 

Loops in programming are used for iteration. Their iterations are controlled by loop conditions, for example:

 

The ‘for loop’ will iterate over a code block for a finite range, until the loop counter meets a condition.

Code example - Python:

for i in range(0, 5):
    print(i)

This loop will execute the code 5 times until the counter i is incremented 6 times.

Output:

01234

 

The ‘while loop’ will iterate over a code block while a condition is true.

Code example - Python:

counter = 0
while counter < 3:
    print(counter)
    counter = counter + 1

Output:

0
1
2

 

Compound Conditional Expressions

 

Compound conditional expressions, as their name indicates, combine two or more Boolean expressions using logical operators such as AND (&&) or OR (||), for example - Pseudocode:

if (x>=0 && x<=10) then “x ∈ {0,1,…,10}”if ((x>=10 && x<=20 && z==x) ||               (y>=40 && y<=50 && z==y))
         then “z ∈ {10,11,…,20}∪ {40,41,…,50}”

 

True tables are often used to represent the outcomes of logical operators for all possible truth values of their operands (conditions). Table 2 illustrates all possible outcomes of the logical operators AND and OR, comparing two conditions. Also see Figure 2 for an illustration of the operators' process flows.

 

Table 2

Truth Table for AND and OR

Truth table with headings: Condition A, Condition B, A AND B, A OR B. Shows False and True values. Blue and white cells.

Note: The table provides all possible outcomes of the logical operators AND and OR, comparing two conditions.

 

When using a combination of logical operators, the AND operator has precedence over the OR operator. Table 3 illustrates how this present can be applied to different compound conditional expressions.

The logical NOT operator or !’, also called negation, changes the value of the condition results to its opposite, for example - Pseudocode:

if A is True, then !A is False.
if A is False, then !A is True.

Note that ‘!’ operator has precedence over the AND and OR operators. In other words, ! > && > ||.As with mathematical expressions, ‘()’ can change the precedence, for example: In the following compound conditional expression: A || B && C, B && C has precedence over A || B. However, in this expression (A || B) && C,  (A || B)has presence over () && C.


Table 3

Precedence of Logical Operators

Two tables comparing boolean expressions and their precedence illustrations, with logical operators, in a clean white layout.

Note: The table provides examples that illustrate how logical operator precedence is implemented within compound conditional expressions


Short-Circuiting

 

In programming, short-circuit evaluation is a process of evaluating a compound conditional expression based on the precedence of logical operators and stopping as soon as the final outcome is determined. In other words, not all conditions in the expression are necessarily checked at runtime. For example – Pseudocode:

x is an integerif (x != 0 && x%3 == 0)
   then “x is an odd number”
else “x is an even number”

In this example, at runtime the compound conditional expression (x != 0 && x%3 == 0) is short-circuited at the condition (x != 0) if (x != 0) returns False as the (x != 0 && x%3 == 0)will also return False, see Figure 2. In other words, the condition (x%3 == 0) will not be checked, as the final outcome is determined by (x != 0) returning False; then the code will output “x is an even number”. See Figure 2 for an illustration of the logical operators AND and OR process flows.


Figure 2

AND and OR Logical Operator

Flowcharts compare logical operators AND/OR with and without short-circuit evaluation. Arrows indicate decision paths. Labeled boxes.

Note: The diagram illustrates the logical operators AND and OR process flows.

 

The short-circuit process obeys the principle of logical operators' precedence illustrated in Table 3.For example, in the compound conditional expression A || B && !C or A || (B && (!C) will Short-Circuit as follows:

- Step-0: B is True, A is True, C is True
- Step-1: C is True -> !C is False
- Step-2: !C is False - Short-Circuit -> skip B, B && !C is False
- Step-3: B && !C is False -> A is True
- Step-4: A || B && !C is True.

 

To summarize:

B is True, A is True, C is True
C is True -> !B is False - Short-Circuit -> skip B, B && !C is False -> A is True -> A || B && !C is True

 

Python handles the short-circuit process following the principle described previously, as all programming languages do. In addition to having logical operators, Python has the built-in functions any() and all(). They act as compounded OR and compounded AND operations, respectively. As well as using the statements True and False to represent the Boolean values TRUE and FALSE, the integer 0, the statement NONE, and empty strings represent the Boolean value FALSE.

Examples of any() and all() usage — Python:

# OR -> any() - Returns True if ANY element is truthy
print(any([0, 1, 2]))  # Output: True (stops after finding 1)
print(any([False, '', None]))  # Output: False (all elements are falsy)
print(any([]))  # Output: False (empty iterable)
print(any([False, False, True]))  # Output: True
print(any([0, 0.0, "", [], {}]))  # Output: False (all falsy values)
print(any("hello"))  # Output: True (string characters are truthy)
print(any(""))  # Output: False (empty string)
print(any([0, "", "text"]))  # Output: True (stops at "text")
print(any(range(0)))  # Output: False (empty range)
print(any(range(1, 5)))  # Output: True (contains 1, 2, 3, 4) 

# AND -> all() - Returns True if ALL elements are truthy
print(all([1, 2, 3]))  # Output: True (all elements are truthy)
print(all([1, 0, 3]))  # Output: False (stops after finding 0)
print(all([]))  # Output: True (empty iterable)
print(all([True, True, True]))  # Output: True
print(all([1, 2, 3, 4]))  # Output: True (all positive numbers)
print(all([1, 2, 0, 4]))  # Output: False (stops at 0)
print(all("hello"))  # Output: True (all characters are truthy)
print(all([[], {}, ""]))  # Output: False (all are falsy)
print(all([[1], {"a": 1}, "text"]))  # Output: True (all are truthy)
print(all(range(1, 5)))  # Output: True (1, 2, 3, 4 all truthy)
print(all(range(0, 5)))  # Output: False (starts with 0)

Note that the all() function uses short-circuit evaluation and will stop iterating a list as soon as a value returns false.


In conclusion, the short-circuit evaluation process is built upon the concepts of conditional logic and compound expressions, which are essential to master to fully understand the mechanics of the process. As illustrated with pseudocode and Python examples, the short-circuit evaluation process performs a strict left-to-right evaluation of logical expressions, obeys the principle of the precedence of logical operators, and stops as soon as the final outcome is determined. This mechanism provides advantages. First, it saves processing time and resources by avoiding unnecessary computations. More importantly, it adds robustness to the code, as programmers can use short-circuiting as a safeguard to prevent runtime errors, such as dividing by zero or attempting to access attributes from a null value.



bottom of page