Short-circuit evaluation

Short-circuit evaluation (also conditional evaluation, English short- circuit evaluation ) is a term used in computer science and refers to a strategy of evaluation of Boolean expressions. In general, the result of a Boolean expression without the use of short-circuit evaluation only after the evaluation of all subexpressions is fixed. Short-circuit evaluation allows early Cancel an evaluation of a Boolean expression when the evaluation result is uniquely determined by a subexpression.

The term

Is to be evaluated from left to right. Does the "true" value, so also must be evaluated in order to determine the value for can. However, if the value " false", has already it is clear that the overall expression can no longer assume the value "true". The analysis can thus be canceled at this point, without the need to evaluate.

Different programming languages ​​use short-circuit evaluation as a means of optimization. Can be often omitted the sometimes computationally intensive analysis of more complex sub-expressions. Also due execution occurring error can be suppressed in this way (see example below).

For example using an algorithm in pseudo-code

The following example shows a use case for short-circuit evaluation.

Set A: = 0 Set B: = 10 if (a! = 0) AND ( ( B / A) > = 5), then    At this point in the algorithm could be B divided by A. else    At this point, the ratio ( B / A) is strictly smaller than 5 or a division by 0 was prevented endif Evaluating the Boolean expression in the case that the variable A has the value 0 involves after the test for inequality A! = 0 canceled. The fact that the first subexpression is evaluated to "false", the result of the conjunction AND is already clearly defined. In particular, the second part of the expression can not be evaluated, since division would be mathematically incorrect by 0.

In contrast to the evaluation of a conjunction is the disjunction in the overall result after the first "true" subexpression fixed.

Practice

In the C programming language boolean expressions are evaluated only after the short circuit principle. The && operator is the only one who realized a conjunction of Boolean expressions. In the Java language, the && operator is also available for short-circuit evaluation. However, there is also the operator and with which the overall evaluation of the Boolean expression is forced.

492522
de