ORELSE: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{DISPLAYTITLE:_ORELSE}}
{{DISPLAYTITLE:_ORELSE}}
[[_ORELSE]] is a [[Boolean|boolean]] logical operator that performs short-circuiting inclusive logical disjunction on two expressions.
'''_ORELSE''' is a [[Boolean|boolean]] logical operator that performs short-circuiting inclusive logical disjunction on two expressions.




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.
* 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.
* Short-circuiting can improve performance if the bypassed expression is complex, or if it involves procedure ([[SUB]] or [[FUNCTION]]) calls.
* Short-circuiting can improve performance if the bypassed expression is complex, or if it involves procedure 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 true, then the second expression can't change the result anymore, it will always remain true, even if the second expression would be false. Hence, the second expression is irrelevant and never evaluated, if the first one is already true.
* If either or both expressions evaluate to true, result is true.
** {{Text|'''Note'''|red}} that any procedures involved in the second expression are '''not called''', if the first expression is true. 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 [[OR (boolean)|OR]] instead.
* If either or both expressions evaluate to true, the result is true.




Line 28: Line 30:


{{PageExamples}}
{{PageExamples}}
''Example:'' OR versus _ORELSE
;Example: OR versus _ORELSE
{{CodeStart}}
{{CodeStart}}
{{Cl|PRINT}} {{Text|<nowiki>"Trying _ORELSE"</nowiki>|#FFB100}}
{{Cl|PRINT}} {{Text|<nowiki>"Trying _ORELSE"</nowiki>|#FFB100}}
Line 66: Line 68:
{{Cl|END FUNCTION}}
{{Cl|END FUNCTION}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}Trying _ORELSE
{{OutputStart}}
Trying _ORELSE
isFruit() called!
isFruit() called!
Probably a strawberry.
Probably a strawberry.
Line 76: Line 79:
Probably a strawberry.
Probably a strawberry.
{{OutputEnd}}
{{OutputEnd}}


{{PageSeeAlso}}
{{PageSeeAlso}}
Line 82: Line 86:
* [[AND]], [[XOR]], [[OR]]
* [[AND]], [[XOR]], [[OR]]
* [[AND (boolean)]], [[XOR (boolean)]], [[OR (boolean)]]
* [[AND (boolean)]], [[XOR (boolean)]], [[OR (boolean)]]
* [[_ANDALSO]], [[_NEGATE]]
* [[_ANDALSO]], [[_NEGATE]], [[_IIF]]
* [[Binary]], [[Boolean]]
* [[Binary]], [[Boolean]]
* [[Mathematical Operations]]
* [[Mathematical Operations]]

Latest revision as of 13:31, 12 December 2024

_ORELSE is a boolean logical operator that performs short-circuiting inclusive logical disjunction on two expressions.


Syntax

result = firstvalue _ORELSE 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 true, then the second expression can't change the result anymore, it will always remain true, even if the second expression would be false. Hence, the second expression is irrelevant and never evaluated, if the first one is already true.
    • Note that any procedures involved in the second expression are not called, if the first expression is true. 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 OR instead.
  • If either or both expressions evaluate to true, the result is true.


Availability


Examples

Example
OR versus _ORELSE
PRINT "Trying _ORELSE"

' _ORELSE performs short-circuiting logical conjunction and hence for "strawberry", only isFruit() is called
IF isFruit("strawberry") _ORELSE isRed("strawberry") _ORELSE isSeasonal("strawberry") THEN
    PRINT "Probably a strawberry."
ELSE
    PRINT "Certainly not a strawberry."
END IF

PRINT
PRINT "Trying OR"

' OR does not performs short-circuiting logical conjunction and hence all is***() functions are called
IF isFruit("strawberry") OR isRed("strawberry") OR isSeasonal("strawberry") THEN
    PRINT "Probably a strawberry."
ELSE
    PRINT "Certainly not a strawberry."
END IF

END

FUNCTION isFruit%% (fruit AS STRING)
    PRINT "isFruit() called!"
    isFruit = (fruit = "strawberry")
END FUNCTION

FUNCTION isRed%% (fruit AS STRING)
    PRINT "isRed() called!"
    isRed = (fruit = "strawberry")
END FUNCTION

FUNCTION isSeasonal%% (fruit AS STRING)
    PRINT "isSeasonal() called!"
    isSeasonal = (fruit = "strawberry")
END FUNCTION
Trying _ORELSE
isFruit() called!
Probably a strawberry.

Trying OR
isFruit() called!
isRed() called!
isSeasonal() called!
Probably a strawberry.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link