Short circuit evaluation

from Wikipedia, the free encyclopedia

Short-circuit evaluation (including related evaluation , English short-circuit evaluation ) is a term used in computer science and describes a strategy of evaluation of Boolean expressions . In general, the result of a Boolean expression without the use of short-circuit evaluation can only be determined after all partial expressions have been evaluated. Short-circuit evaluation enables an evaluation of a Boolean expression to be terminated prematurely as soon as the evaluation result is clearly determined by a partial expression.

The conjunction in the expression

should be evaluated from left to right. If the value is "true", it must also be evaluated in order to be able to determine the value for . However, if it has the value "false", it is already certain that the overall expression can no longer assume the value "true". The evaluation can therefore be canceled at this point without having to evaluate.

In contrast to the evaluation of a conjunction, in the case of a disjunction the overall result is already determined after the first “true” partial expression.

Various programming languages ​​use the short circuit evaluation as a means of optimization. The sometimes computationally intensive evaluation of more complex partial expressions can often be dispensed with. Conditional execution errors can also be suppressed in this way.

Example using an algorithm in pseudocode

The following example shows an application for short-circuit evaluation.

Setze A := 0
Setze B := 10
if (A != 0) AND ((B / A) >= 5) then
  An dieser Stelle im Algorithmus konnte B durch A geteilt werden.
else
  An dieser Stelle ist der Quotient (B / A) echt kleiner als 5 oder eine Division durch 0 wurde verhindert
endif

If the variable A contains the value 0, the evaluation of the Boolean expression is aborted after the check for inequality A! = 0. Because the first partial expression was evaluated as "false", the result of the conjunction AND is already clearly established. This prevents the second subexpression from being evaluated so that division by 0 is not carried out.

Short circuit evaluation in programming languages

In the programming language C , binary Boolean expressions are used with the operators && and || only evaluated according to the short circuit principle.

In the Java programming language , the operator are && and || also for a short circuit evaluation. In addition, there are the logical operators & and |, which force evaluation of both sides.

In Modula-2 , too , the logical operators AND and OR are short-circuit operators.

See also

Individual evidence

  1. ^ Peter Hofer, Peter Fischer: Lexicon of Computer Science . 15th edition. Springer, Berlin 2010, ISBN 3-642-15125-6 , pp. 81 ( limited preview in Google Book search).

literature

  • Michael L. Scott: Programming Language Pragmatics . 3. Edition. Elsevier LTD, Oxford 2009, ISBN 0-12-374514-4 , pp. 239 ( limited preview in Google Book search).