Are you aware between the difference between And and && and Or and ||? And and Or are mathematical operators, while && and || are logical operators, so they are quite different. The only place where the && and || operators are used are in conditional statements like If, While, and Until. And and Or are used in mathematical expressions (calculations) and are generally not meant to be used in the conditional commands If, While, and Until.
The following example shows the difference:
Local Int x = 2, y = 3 ' The inefficient way using And: If x == 0 And y == 3 ' do something EndIf ' The efficient way using &&: If x == 0 && y == 3 ' do something EndIf
The commands following the If statements aren't executed, but for different reasons.
The first If statement evaluates both expressions and then performs a mathematical operation on the result of both boolean expressions. The second If statement only evaluates the first expression and never bothers to check y == 3. Because And is a mathematical operator like +, -, *, / the expression x == 0 And y == 3 is completely calculated. First x == 0 is evaluated, which produces the boolean value False (0). Then y == 3 is evaluated, which returns the boolean value True(-1). After calculating both expressions the boolean results are combined by the And operator: 0 And -1 which then returns 0 (False), because only one expression is True. These are steps involved:
(1) x == 0 And y == 3
(2) FALSE And TRUE
(3) FALSE
The second If statement uses &&. Because && is not a mathematical operator the second expression is only evaluated if the first expression is True. Note that an AND operator requires both operands to be TRUE to result TRUE, as you can see from this AND table:
Value 1 | Value 2 | AND-Result |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
From the table you can see that if Value 1 equals 0 (FALSE) the result is always 0 (FALSE), despite Value 2. Only when Value 1 equals 1 (TRUE) Value 2 needs to be evaluated. The compiler produces code to do just that. It first evaluates the first expression x == 0 and because the result is FALSE the next expression y == 3 is never executed. This results in a considerable increase of performance, especially if the second expression calls a function. For instance, in the next example the function div3() is never called if x is 0 ( otherwise div3() would generate a division-by-zero exception):
Local Int x = 0 If x != 0 && div3(x) == 3 ' do something EndIf Function div3(ByVal n As Int) As Int foo = 3 / n
The same is true for Or and ||. Look at this OR-table, the result is always TRUE if the first value is TRUE.
Value 1 | Value 2 | OR-Result |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
In the next example the second expression (y == 4) isn't evaluated because the first expression (x == 2) is already TRUE, making the result TRUE despite the result of the second expression.
Local Int x = 2, y = 3 If x == 2 || y == 4 ' do something EndIf
If we used the mathematical operator Or, first both expressions would be evaluated and then the boolean results would be or-ed, an unnecessary extra operation.
The And operator may have a purpose in a conditional statement, but it would be used as a mathematical operator in the expression, for instance to test if certain bits are set:
Local Int x = %111 ' binary notation of 7 If x And %11 > 0 ' test if lower 2 bits are set ' do something EndIf
Here x (=7) is mathematically And-ed with 3 which results in 3, and 3 is larger than 0. The If statement will be executed.
Conclusion
Rather than using And and Or, use the logical operators && and || in conditional statements.
No comments:
Post a Comment