Difference between revisions of "Or"
From Xojo Documentation
m (1 revision) |
m |
||
(26 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{OperatorBox}} | ||
+ | {{Description | ||
+ | |text = Used to perform a logical '''Or''' comparison between two [[Boolean]] expressions or a bitwise '''Or''' comparison between two [[Integer|integers]]. }} | ||
+ | == Usage == | ||
+ | ''result'' = ''expression1'' '''Or''' ''expression2'' | ||
+ | {| cellpadding="6" cellspacing="0" border="1" | ||
− | + | ! width=15% style="background-color:#e0e0e0" | Part | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | ! width= | ||
! width=15% style="background-color:#e0e0e0" | Type | ! width=15% style="background-color:#e0e0e0" | Type | ||
Line 18: | Line 15: | ||
|result | |result | ||
− | |[[ | + | |[[Boolean]] or [[Integer]] |
− | |A [[ | + | |A [[Boolean]] value if ''expression1'' and ''expression2'' are [[Boolean|boolean]] and an [[Integer|integer]] if ''expression1'' and ''expression2'' are [[Integer|integers]]. |
|- | |- | ||
|expression1 | |expression1 | ||
− | |[[ | + | |[[Boolean]] or [[Integer]] |
− | |Any valid [[ | + | |Any valid [[Boolean]] or [[Integer|integer]] expression. ''Expression1'' and ''expression2'' must be of the same data type. |
|- | |- | ||
|expression2 | |expression2 | ||
− | |[[ | + | |[[Boolean]] or [[Integer]] |
− | |Any valid [[ | + | |Any valid [[Boolean]] or [[Integer|integer]] expression. ''Expression1'' and ''expression2'' must be of the same data type. |
|- | |- | ||
|} | |} | ||
+ | == Notes == | ||
+ | The '''Or''' operator is overloaded. If it is passed two [[Boolean|booleans]], it performs a logical Or and returns a [[Boolean|boolean]]. If it is passed two [[Integer|integers]], it performs a bitwise Or operation on the [[Integer|integers]] and returns an [[Integer|integer]]. In this case, it is equivalent to calling the BitOr method of the [[Bitwise]] class. | ||
− | + | In the first instance, if either boolean expression evaluates to [[True]], then ''result'' is [[True]]. If both expressions evaluate to [[False]] then ''result'' is [[False]]. The truth table for logical operators is shown below. | |
− | |||
− | |||
{| cellpadding="8" cellspacing="0" border="1" | {| cellpadding="8" cellspacing="0" border="1" | ||
− | ! width= | + | ! width=15% style="background-color:#e0e0e0" | Expression1 |
− | ! width= | + | ! width=15% style="background-color:#e0e0e0" | Expression2 |
− | ! width= | + | ! width=15% style="background-color:#e0e0e0" | Or |
− | ! width= | + | ! width=15% style="background-color:#e0e0e0" | [[And]] |
− | ! width= | + | ! width=15% style="background-color:#e0e0e0" | [[Xor]] |
|- | |- | ||
|True | |True | ||
Line 101: | Line 98: | ||
If you pass two [[Integer|integers]], '''Or''' returns an [[Integer|integer]] that is the result of comparing each bit of the two [[Integer|integers]] passed and assigning 1 to the bit position in the [[Integer|integer]] returned if either of the bits in the same position in the [[Integer|integers]] passed are 1. Otherwise, 0 is assigned to the bit position. | If you pass two [[Integer|integers]], '''Or''' returns an [[Integer|integer]] that is the result of comparing each bit of the two [[Integer|integers]] passed and assigning 1 to the bit position in the [[Integer|integer]] returned if either of the bits in the same position in the [[Integer|integers]] passed are 1. Otherwise, 0 is assigned to the bit position. | ||
− | The following table shows the results for | + | The following table shows the results for bitwise operators: |
{| cellpadding="8" cellspacing="0" border="1" | {| cellpadding="8" cellspacing="0" border="1" | ||
− | ! width= | + | ! width=15% style="background-color:#e0e0e0" | Integer1 |
− | ! width= | + | ! width=15% style="background-color:#e0e0e0" | Integer2 |
− | ! width= | + | ! width=15% style="background-color:#e0e0e0" | Or |
− | ! width= | + | ! width=15% style="background-color:#e0e0e0" | [[And]] |
− | ! width= | + | ! width=15% style="background-color:#e0e0e0" | [[Xor]] |
|- | |- | ||
|0 | |0 | ||
Line 160: | Line 157: | ||
|} | |} | ||
− | + | == Sample Code == | |
− | == | ||
This example uses the '''Or''' operator to perform logical comparisons | This example uses the '''Or''' operator to perform logical comparisons | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | <rbcode> | ||
+ | Var a As Boolean | ||
+ | Var b As Boolean | ||
+ | Var c As Boolean | ||
+ | Var d As Boolean | ||
+ | a = True | ||
+ | b = False | ||
+ | d = a Or b // d = True | ||
+ | d = b Or c // d = False | ||
+ | </rbcode> | ||
− | == | + | Using Or with Integer values: |
− | + | <rbcode> | |
+ | Var i1 As Integer = 4 | ||
+ | Var i2 As Integer = 5 | ||
+ | |||
+ | Var result As Integer | ||
+ | result = i1 Or i2 // result = 5 | ||
+ | </rbcode> | ||
+ | == See Also == | ||
+ | [[And]], [[Not]], [[Xor]] operators; [[Bitwise]] class; [[Operator_Or]] function; [[Operator_Precedence|Operator precedence]] | ||
− | [[Category: | + | [[Category:Language_Boolean]] |
− |
Latest revision as of 20:02, 22 November 2019
Used to perform a logical Or comparison between two Boolean expressions or a bitwise Or comparison between two integers.
Usage
result = expression1 Or expression2
Part | Type | Description |
---|---|---|
result | Boolean or Integer | A Boolean value if expression1 and expression2 are boolean and an integer if expression1 and expression2 are integers. |
expression1 | Boolean or Integer | Any valid Boolean or integer expression. Expression1 and expression2 must be of the same data type. |
expression2 | Boolean or Integer | Any valid Boolean or integer expression. Expression1 and expression2 must be of the same data type. |
Notes
The Or operator is overloaded. If it is passed two booleans, it performs a logical Or and returns a boolean. If it is passed two integers, it performs a bitwise Or operation on the integers and returns an integer. In this case, it is equivalent to calling the BitOr method of the Bitwise class.
In the first instance, if either boolean expression evaluates to True, then result is True. If both expressions evaluate to False then result is False. The truth table for logical operators is shown below.
Expression1 | Expression2 | Or | And | Xor |
---|---|---|---|---|
True | True | True | True | False |
True | False | True | False | True |
False | True | True | False | True |
False | False | False | False | False |
If you pass two integers, Or returns an integer that is the result of comparing each bit of the two integers passed and assigning 1 to the bit position in the integer returned if either of the bits in the same position in the integers passed are 1. Otherwise, 0 is assigned to the bit position.
The following table shows the results for bitwise operators:
Integer1 | Integer2 | Or | And | Xor |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 1 | 0 | 1 |
1 | 0 | 1 | 0 | 1 |
1 | 1 | 1 | 1 | 0 |
Sample Code
This example uses the Or operator to perform logical comparisons
Var b As Boolean
Var c As Boolean
Var d As Boolean
a = True
b = False
d = a Or b // d = True
d = b Or c // d = False
Using Or with Integer values:
See Also
And, Not, Xor operators; Bitwise class; Operator_Or function; Operator precedence