Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the AND
function evaluates to false
, the overall value must be false
; and when the first argument of the OR
function evaluates to true
, the overall value must be true
.
In programming languages with lazy evaluation (Lisp, Perl, Haskell), the usual Boolean operators short-circuit. In others (Ada, Java, Delphi), both short-circuit and standard Boolean operators are available. For some Boolean operations, like exclusive or (XOR), it is impossible to short-circuit, because both operands are always needed to determine a result.
Short-circuit operators are, in effect, control structures rather than simple arithmetic operators, as they are not strict. In imperative language terms (notably C and C++), where side effects are important, short-circuit operators introduce a sequence point: they completely evaluate the first argument, including any side effects, before (optionally) processing the second argument. ALGOL 68 used proceduring to achieve user-defined short-circuit operators and procedures.
The use of short-circuit operators has been criticized as problematic:
In any programming language that implements short-circuit evaluation, the expression ''x'' and ''y''
is equivalent to the conditional expression if ''x'' then ''y'' else ''x''
, and the expression ''x'' or ''y''
is equivalent to if ''x'' then ''x'' else ''y''
. In either case, x is only evaluated once.
The generalized definition above accommodates loosely typed languages that have more than the two truth-values True
and False
, where short-circuit operators may return the last evaluated subexpression. This is called "last value" in the table below. For a strictly-typed language, the expression is simplified to if ''x'' then ''y'' else '''false'''
and if ''x'' then '''true''' else ''y''
respectively for the boolean case.
Although takes precedence over in many languages, this is not a universal property of short-circuit evaluation. An example of the two operators taking the same precedence and being left-associative with each other is POSIX shell's command-list syntax.[1]
The following simple left-to-right evaluator enforces a precedence of over by a :
function short-circuit-eval (operators, values) let result := True for each (op, val) in (operators, values): if op = "AND" && result = False continue else if op = "OR" && result = True return result else result := val return result
Short-circuit logic, with or without side-effects, have been formalized based on Hoare's conditional. A result is that non-short-circuiting operators can be defined out of short-circuit logic to have the same sequence of evaluation.[2]
As you look at the table below, keep in mind that bitwise operators often do not behave exactly like logical operators, even if both arguments are of 0
, 1
or Boolean type.
Examples:
false
:<nowiki>(true & true) === (true && true)</nowiki>
,<nowiki>(false | false) === (false || false)</nowiki>
,<nowiki>(1 & 2) === (1 && 2)</nowiki>
.false
:<nowiki>(true & true) === (true && true)</nowiki>
,<nowiki>(0 | 0) === (0 || 0)</nowiki>
,<nowiki>(1 & 2) === (1 && 2)</nowiki>
.Advanced Business Application Programming (ABAP) | none | and , or | Boolean |
Ada | and , or | and then , or else | Boolean |
ALGOL 68 | and, &, ∧ ; or, ∨ | Boolean | |
APL | ∧ , ∨ , ⍲ (nand), ⍱ (nor), etc. | :AndIf , :OrIf | Boolean |
awk | none | && , <nowiki>||</nowiki> | Boolean |
Bash | none | && , <nowiki>||</nowiki> | Boolean |
C, Objective-C | & , <nowiki>|</nowiki> | && , <nowiki>||</nowiki> , <nowiki>?</nowiki> [3] | int (& , <nowiki>|</nowiki> , && ,<nowiki>||</nowiki> ), opnd-dependent (<nowiki>?</nowiki> )|-| C++ |
&&
, <nowiki>||</nowiki>
, <nowiki>?</nowiki>
[4] | Boolean (&&
,<nowiki>||</nowiki>
), opnd-dependent (<nowiki>?</nowiki>
)|-| C#| &
, <nowiki>|</nowiki>
| &&
, <nowiki>||</nowiki>
, <nowiki>?</nowiki>
, <nowiki>??</nowiki>
| Boolean (&&
,<nowiki>||</nowiki>
), opnd-dependent (<nowiki>?</nowiki>
, <nowiki>??</nowiki>
)|-| ColdFusion Markup Language (CFML)| none| AND
, OR
, &&
, <nowiki>||</nowiki>
| Boolean|-| D| &
, <nowiki>|</nowiki>
| &&
, <nowiki>||</nowiki>
, <nowiki>?</nowiki>
| Boolean (&&
,<nowiki>||</nowiki>
), opnd-dependent (<nowiki>?</nowiki>
)|-| Eiffel| and
, or
| and then
, or else
| Boolean|-| Erlang| and
, or
| andalso
, orelse
| Boolean|-| Fortran| .and.
, .or.
| .and.
, .or.
| Boolean|-| Go, Haskell, OCaml| none| &&
, <nowiki>||</nowiki>
| Boolean|-| Java, MATLAB, R, Swift| &
, <nowiki>|</nowiki>
| &&
, <nowiki>||</nowiki>
| Boolean|-| JavaScript| none| &&
, &&=
, <nowiki>||</nowiki>
, <nowiki>||=</nowiki>
| Last value|-| Julia| none| &&
, <nowiki>||</nowiki>
| Last value|-| Lasso| none| and
, or
, &&
, <nowiki>||</nowiki>
| Last value|-| Kotlin| and
, or
| &&
, <nowiki>||</nowiki>
| Boolean|-| Lisp, Lua, Scheme| none| and
, or
| Last value|-| MUMPS (M)| &
, !
| none| Numeric|-| Modula-2| none| AND
, OR
| Boolean|-| Oberon| none| &
, OR
| Boolean|-| OCaml| land
, lor
[5] | &&
, <nowiki>||</nowiki>
| Boolean|-| Pascal| and
, or
| and_then
, or_else
| Boolean|-| Perl| &
, <nowiki>|</nowiki>
| &&
, and
, <nowiki>||</nowiki>
, or
| Last value|-| PHP| none| &&
, and
, <nowiki>||</nowiki>
, or
| Boolean|-| POSIX shell (command list)| none| &&
, <nowiki>||</nowiki>
| Last value (exit)|-| PowerShell Scripting Language| none| -and
, -or
| Boolean|-| Python| &
, <nowiki>|</nowiki>
| and
, or
| Last value|-| Ruby| &
, <nowiki>|</nowiki>
| &&
, and
, <nowiki>||</nowiki>
, or
[6] | Last value|-| Rust| &
, <nowiki>|</nowiki>
| &&
, <nowiki>||</nowiki>
[7] | Boolean|-| Smalltalk| &
, <nowiki>|</nowiki>
| and:
, or:
| Boolean|-| Standard ML| | andalso
, orelse
| Boolean|-| TTCN-3| none| and
, or
[8] | Boolean|-|Beckhoff TwinCAT® (IEC 61131-3)|AND
, OR
|AND_THEN
,[9] OR_ELSE
[10] |Boolean|-| Visual Basic .NET| And
, Or
| AndAlso
, OrElse
| Boolean|-| Visual Basic, Visual Basic for Applications (VBA)| And
, Or
| Select Case
| Numeric|-| Wolfram Language| And @@ {...}
, Or @@ {...}
| And
, Or
, &&
, <nowiki>||</nowiki>
| Boolean|-| ZTT| &
, <nowiki>|</nowiki>
| none| Boolean|}Usual example, using a C-based language:
Consider the following example:
In this example, short-circuit evaluation guarantees that myfunc(b)
is never called. This is because a != 0
evaluates to false. This feature permits two useful programming constructs.
Both are illustrated in the following C snippet where minimal evaluation prevents both null pointer dereference and excess memory fetches:
bool is_first_char_valid_alpha(const char *p)
Since minimal evaluation is part of an operator's semantic definition and not an optional optimization, a number of coding idioms rely on it as a succinct conditional construct. Examples include:
Perl idioms:
POSIX shell idioms:[11] echo
cannot fail.
Despite these benefits, minimal evaluation may cause problems for programmers who do not realize (or forget) it is happening. For example, in the codemyfunc(b)
is supposed to perform some required operation regardless of whether do_something
is executed, such as allocating system resources, and expressionA
evaluates as false, then myfunc(b)
will not execute, which could cause problems. Some programming languages, such as Java, have two operators, one that employs minimal evaluation and one that does not, to avoid this problem.
Problems with unperformed side effect statements can be easily solved with proper programming style, i.e., not using side effects in boolean statements, as using values with side effects in evaluations tends to generally make the code opaque and error-prone.[12]
Short-circuiting can lead to errors in branch prediction on modern central processing units (CPUs), and dramatically reduce performance. A notable example is highly optimized ray with axis aligned box intersection code in ray tracing. Some compilers can detect such cases and emit faster code, but programming language semantics may constrain such optimizations.
An example of a compiler unable to optimize for such a case is Java's Hotspot virtual machine (VM) as of 2012.[13]