ANDALSO: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
No edit summary |
No edit summary |
||
(4 intermediate revisions by the same user not shown) | |||
Line 9: | Line 9: | ||
{{PageDescription}} | {{PageDescription}} | ||
* A logical operation is said to be short-circuiting if the compiled code can bypass the evaluation of one expression depending on the result of another expression. | * A logical operation is said to be short-circuiting if the compiled code can bypass the evaluation of one expression depending on the result of another expression. | ||
* Short-circuiting can improve performance if the bypassed expression is complex, or if it involves procedure ([[SUB]] or [[FUNCTION]]) calls. | |||
* If the result of the first expression evaluated determines the final result of the operation, there is no need to evaluate the second expression, because it cannot change the final result. E.g. if the first expression is already false, then the second expression can't change the result anymore, it will always remain false, even if the second expression would be true. Hence, the second expression is irrelevant and never evaluated, if the first one is already false. | * If the result of the first expression evaluated determines the final result of the operation, there is no need to evaluate the second expression, because it cannot change the final result. E.g. if the first expression is already false, then the second expression can't change the result anymore, it will always remain false, even if the second expression would be true. Hence, the second expression is irrelevant and never evaluated, if the first one is already false. | ||
** {{Text|'''Note | ** {{Text|'''Note'''|red}} that any procedures involved in the second expression are '''not called''', if the first expression is false. This behavior is intended and the reason for the better performance, but it may cause unexpected failures if you're not aware of it. | ||
** In fact, if any | ** In fact, if any procedures in the second expression '''must be called''' regardless of the truth of the first expression, then you must use the regular [[AND (boolean)|AND]] instead. | ||
* Only if both expressions evaluate to true, then the result is true too. | * Only if both expressions evaluate to true, then the result is true too. | ||
Line 73: | Line 73: | ||
* [[AND]], [[XOR]], [[OR]] | * [[AND]], [[XOR]], [[OR]] | ||
* [[AND (boolean)]], [[XOR (boolean)]], [[OR (boolean)]] | * [[AND (boolean)]], [[XOR (boolean)]], [[OR (boolean)]] | ||
* [[_ORELSE]], [[_NEGATE]] | * [[_ORELSE]], [[_NEGATE]], [[_IIF]] | ||
* [[Binary]], [[Boolean]] | * [[Binary]], [[Boolean]] | ||
* [[Mathematical Operations]] | * [[Mathematical Operations]] |
Latest revision as of 13:30, 12 December 2024
_ANDALSO is a boolean logical operator that performs short-circuiting logical conjunction on two expressions.
Syntax
- result = firstvalue _ANDALSO secondvalue
Description
- A logical operation is said to be short-circuiting if the compiled code can bypass the evaluation of one expression depending on the result of another expression.
- Short-circuiting can improve performance if the bypassed expression is complex, or if it involves procedure (SUB or FUNCTION) calls.
- If the result of the first expression evaluated determines the final result of the operation, there is no need to evaluate the second expression, because it cannot change the final result. E.g. if the first expression is already false, then the second expression can't change the result anymore, it will always remain false, even if the second expression would be true. Hence, the second expression is irrelevant and never evaluated, if the first one is already false.
- Note that any procedures involved in the second expression are not called, if the first expression is false. This behavior is intended and the reason for the better performance, but it may cause unexpected failures if you're not aware of it.
- In fact, if any procedures in the second expression must be called regardless of the truth of the first expression, then you must use the regular AND instead.
- Only if both expressions evaluate to true, then the result is true too.
Availability
Examples
- Example
- AND versus _ANDALSO
DIM AS LONG index, values(1 TO 10), v FOR index = 1 TO 10 values(index) = RND * 255 NEXT index ' value of index is now > 10 PRINT "Trying _ANDALSO" ' _ANDALSO performs short-circuiting logical conjunction and hence the GetArrayValue check is completely bypassed IF index >= 1 _ANDALSO index <= 10 _ANDALSO GetArrayValue(values(), index, v) THEN PRINT "_ANDALSO: Value ="; v ELSE PRINT "_ANDALSO: Outside range." END IF PRINT PRINT "Trying AND" ' AND does not performs short-circuiting logical conjunction and hence QB64-PE will throw a runtime error: Subscript out of range IF index >= 1 AND index <= 10 AND GetArrayValue(values(), index, v) THEN PRINT "AND: Value ="; v ELSE PRINT "AND: Outside range." END IF END FUNCTION GetArrayValue%% (arr() AS LONG, idx AS LONG, value AS LONG) value = arr(idx) GetArrayValue = -1 ' return true END FUNCTION |
See also
- Featured in our "Keyword of the Day" series
- _BIT, &B, _BYTE
- AND, XOR, OR
- AND (boolean), XOR (boolean), OR (boolean)
- _ORELSE, _NEGATE, _IIF
- Binary, Boolean
- Mathematical Operations