Short-circuit evaluation is an evaluation strategy for boolean operators that consist of evaluation only the minimal terms needed to find out the result. This is an ultra-specialized tag and the more general tag evaluation should be preferred.
What is it?
Short-circuit evaluation is an evaluation strategy for boolean operators that consist of evaluation only the minimal terms needed to find out the result.
In C, C++, and Java for example, &&
and ||
have a short-circuit evaluation, unlike &
and |
which have a systematic evalauation of all terms.
How does it work?
For a succession of AND terms, the evaluation stops as soon as a term is false, since whatever the next terms will be, the overall result will no longer be true.
For a succession of OR terms, the evaluation stops as soon as a term is true, since whatever the next terms will be, the overall result will no longer be false.
This approach allow to combine in a single expression terms that shall only be evaluated in case of some conditions that ensure their feasibility. For example: a!=0 && b/a>10
the second term can only be calucalated if the first term is successful. The short circuit ensures that the second term will not be evaluated if the first term fails.
Related tags
- This tag is ultra-specialized an the more general tag evaluation should be preferred.
- For more general questions about evaluation strategies, in which short-circuit is only a part, use evaluation-strategy
See also
- Short-circuit evaluation on Wikipedia