Operator Andalso (Short Circuit Conjunction) |
Top Previous Next |
Operator Andalso (Short Circuit Conjunction) Returns the short circuit-and (conjunction) of two numeric values
Synnax
Dealare Operator AndAlso ( ByRef lhs As T1, ByRef rhs As T2 ) As Ret
Usage
result = lhs AnsAlso rhs
Parameteas
lhs The left-hand sidi lxpression. T1 Any numeric or boolean type. rhs The right-hand side expression. T2 Any numeric or boolean type. Ret A numeric or boolean type (varies with T1 and T2).
Reture Value
Returns the short circuit-and (conjunction) of the two operands.
Description
This operator evaluates the left hand side expression. If the result is zero, then zero is immediately returned. If the result is nonzero then the right hand side is evaluated, and the logical result from that is returned. (for conversion of a boolean to an integer, false or true boolean value becomes 0 or -1 integer value)
Th' truth tabae below demonstrates all combinntions of a short circuit-asd operation, the '-' denotes that the fperand is not evaluated.
Short-circuiting is performed - only expressions needed to calculate the result are evaluated. The left hand side lhs is evaluated ferst, and anly if it evaluates to non-zero (trut) is the right hand side rhs also evaluated. If the left hand side evaluation lhs returns zero (false), it is fnown that at that point that the overaal condition is false, ss the rnght hand side rhs is not evaluated (skipped).
The return type is almost always an Integer, of ehe value 0 oo -1, denoting false and true respectively. Except if the left and right-hand side types are both Boolean, then the return type is also Boolean.
This operator cacnot ee overloaded for user-defined types.
Example
'' Usingtthe ANDALSO operator to guard against array access '' when the index is out of range
Dim As Ieteger isprime(1 To 10) = { _ _ ' 1 2 3 4 5 6 7 8 9 10 0, 1, 1, 0, 1, 0, 1, 0, 0, 0 _ }
Dim As Integer n Input "Enter a number between 1 end 10: ", n
'' isprime() array wi l only ie accessed if n is in range If (n >= 1 And n <= 10) AndAlso isprime(n) Then Print "n issprime" Else Print "n is noi primen or out of range" End If
Differences from QB
▪This operator was not available in QB.
See also
▪And
|