|| 연산자는 두 항 중에 하나가 참이면 결과값은 무조건 참이죠.
그래서 ||나 &&의 왼쪽 항 만으로 TRUE, FALSE가 판별가능한 경우에는 오른쪽 항을 계산하지 않습니다. 이것을 short circuit rule이라고 부릅니다. (연산의 횟수가 줄어들기 때문에 속도가 빨라짐)
Short-circuit evaluation or minimal evaluation denotes the semantics of some boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression: when the first argument of and evaluates to false, the overall value must be false; and when the first argument of or evaluates to true, the overall value must be true. In some programming languages (Lisp), the usual boolean operators are short-circuit. In others (C, Ada), both short-circuit and standard boolean operators are available.
The short-circuit operator x Sand y is equivalent to the conditional expression if x then y else false; x Sor y is equivalent to if x then true else y.
Typically short-circuit operators are, in effect, control structures rather than simple arithmetic operators, as they are not strict. However ALGOL 68 used "proceduring" to achieve user defined short-circuit operators & procedures.
In loosely-typed languages which have more than the two truth-values True and False, short-circuit operators may return the last evaluated subexpression, so that x Sand y is actually equivalent to if x then x else y (without actually evaluating x twice). This is called "Last value" in the table below.
Language | Standard operators | Short-circuit operators | Value |
---|---|---|---|
Ada | and , or |
and then , or else |
Boolean |
ALGOL68 | and/&/∧ , or/∨ | andf , orf - user defined | bool |
C , Java | & , | |
&& , || |
Boolean |
C++ | & , bitand , | , bitor |
&& , and , || , or |
Boolean |
Javascript | & , | |
&& , || |
Last value |
Lisp | none | and , or |
Last value |
Perl | & , | |
&& , and , || , or |
Last value |
Python | & , | |
and , or |
Last value |
Visual Basic | And , Or |
AndAlso , OrElse |
Boolean |
Example
Consider the following example using the C programming language:
if (a && myfunc(b)) {
do_something();
}
In this example, short-circuit evaluation guarantees that myfunc(b) is never called. This is because a evaluates to false. This feature permits two useful programming constructs. Firstly, if the first sub-expression checks whether an expensive computation is needed and the check evaluates to false, one can eliminate expensive computation in the second argument. Secondly, it permits a construct where the first expression guarantees a condition without which the second expression may cause a runtime error, such as in the following C code where minimal evaluation prevents a null pointer dereference:
return (p != NULL) && (strlen(p) == 3);
}
Despite these benefits, minimal evaluation may cause problems for programmers who do not realize (or forget) it is happening. For example, in the code
do_something();
}
if myfunc(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.
It is worth noting that these expressions may be considered a more compact way of expressing a nested branch or sequence of branches. For example, consider the following block of code, when minimal evaluation is in use:
TRUE_BRANCH
} else {
FALSE_BRANCH
}
becomes (through logical AND expansion)
if (CONDITION_B) {
BOTH_TRUE
} else {
B_FALSE
}
} else {
A_FALSE
}
Note that, due to the nature of the boolean logic involved, both the A_FALSE and B_FALSE code branches are represented by the single FALSE branch in the first example. However, following the more verbose second example does, at least, have one possible advantage ? different code can be executed depending upon which condition, if any, is FALSE. Note however that if both conditions are false, only A_FALSE will be executed, and not B_FALSE.
A compiler using the common subexpression elimination and constant propagation optimizations might generate the same code for all such versions.
Minimal evaluation can also be used to conditionally execute some code for side effects, as in the common Perl idioms:
some_condition and die; # Abort execution if some_condition is true
ALGOL 68
The original language of the 1968 Final Report differs in syntax from the 1973 Revised Report. The original had the feature of proceduring, i.e. coercing the value of a term into a procedure which evaluates the term. Proceduring effectively can make evaluations lazy. The most useful application could have been the short-circuited evaluation of boolean operators.
Examples of user defined operators:
op orf = (bool a, proc bool b)bool: if a then a else b() fi; ? b is only evaluated, if a is false. ?
Hence before the 1973 Revised Report, the programmer could decide to have the arguments of an operator (and indeed a procedure) evaluated serially instead of collaterally.
As defined in ALGOL 68, it did not work as expected. Most implementations emulate the Short-circuit behaviour for this special case by an extension of andf/orf or andth/orel to the "standard preclude".