Today I found the difference between and and Andalso

zhaozj2021-02-16  62

I have seen Andalso before, but I have never put it in my heart, and there is no investigation to see what is related to it.

Today, I found that the effect of using them is different. I thought that And and C && one (all short circuits) did not expect And the AND is not short, and Andalso is short-circuited. It seems that the use of Andalso will use Andalso with Andalso, which uses ands withless.

E.G. DIM I as Int32 DIM S () AS String

i = 0

IF i = 1 Andalso S (1) = "1" Then MessageBox.show ("It's impossible to here") Else MessageBox.show ("Andalso can be shorted") Endix

IF i = 1 and s (1) = "1" THEN 'error Other information: The object reference is not set to an instance of an object. Messagebox.show ("It's impossible to come here") Else MessageBox.show ("It's impossible to come here") end if

Oralse

The following is an explanation on the MSDN:

The short-circuit operator logic operator Andalso and Oralse are referred to as "short circuit" behavior. The short circuit operator first calculates the left expression. If the left expression makes the entire expression in Andalso or verifies (in ORELSE), the program execution process continues without calculating the right expression. See the following example: IF 45 <12 Andalso MyFunction (3) = 81 Then 'Add Code to Continue Execution. In this example, the operator calculates the left expression 45 <12. Because this expression is calculated as False, the entire logical expression is calculated as false. The program execution process thus starts executing the code in the IF block without calculating the right expression MyFunction (3). In this example, MyFunction () will never be called because the entire expression is fake because the left expression makes the entire expression. Similarly, if the left expression is calculated as true in the logical expression of ORELSE, the execution process continues the next row code without calculating the second expression because the first expression has enabled the entire expression. Conversely, when using logical operators and or OR, both sides of the logical operator must be calculated. For example, consider the following expression: if 45 <12 and myfunction (3) = 81 THEN 'Add Code to Continue Execution. In this example, myFunction () is called, even if the first expression is calculated as false. The Andalso operator performs simplified logic for two expressions. Remarks If the compiled code can skip the calculation of another expression based on the calculation result of an expression, the logic is called "short circuit". If the calculation result of the first expression can determine the end result of the calculation, no additional expression is required because it does not change the final result. If the skipped expression is complicated or related to the process call, the short circuit can improve performance. Example This example This example uses the Andalso operator to perform logical access to both expressions. The result is a boolean value that means that the entire joint expression is true. If the first expression is false, no second expression is calculated.

Dim A As Integer = 10Dim B As Integer = 8Dim C As Integer = 6Dim myCheck As BooleanmyCheck = A> B AndAlso B> C 'True.myCheck = B> A AndAlso B> C' False. Second expression not evaluated.myCheck = A > B Andalso C> b 'false. Second Expression Evaluated. Public Function FindValue (Byval SearchValue As Double) AS Double Dim i as integer = 0 while i <= ubound (arr) Andalso Arr (i <> SearchValue 'if i is greater tour (arr), searchvalue is not checked. I = 1 End while if i> = ubound (arr) Then i = -1 return ing iD function ORSE operator for two Expression performs short circuit logic analysis. Remarks If the compiled code can skip the calculation of another expression based on the calculation result of an expression, the logic is called "short circuit". If the calculation result of the first expression can determine the end result of the calculation, no additional expression is required because it does not change the final result. If the skipped expression is complicated or related to the process call, the short circuit can improve performance. Example This example uses an ORELSE operator to perform logic quote for two expressions. The result is a boolean value that indicates whether there is one in the two expressions. If the first expression is TRUE, the second expression is not calculated. Dim A As Integer = 10Dim B As Integer = 8Dim C As Integer = 6Dim myCheck As BooleanmyCheck = A> B OrElse B> C 'True. Second expression is not evaluated.myCheck = B> A OrElse B> C' True. Second expression Is evatated.mycheck = b> a oralse c> b 'false. if myfunction (5) = true orelse myotherfunction (4) = TRUE THEN' IF MyFunction (5) IS true, myotherfunction (4) is not caled. 'Insert Code TO BE EXECUTED IND IF Take a look at the AND and orand operators to perform logical access to both Boolean expressions, or perform a bitwise for two numerical expressions. Note that due to the priority of the logical / bit operator below the priority of other arithmetic and relational operators, any bitwise operation should be operated in parentheses to ensure accurate execution. If the operand includes a Boolean expression and a numerical expression, the Boolean expression will be converted to a value (-1 represents true, 0 means false, will eventually perform the standby operation.

转载请注明原文地址:https://www.9cbs.com/read-16183.html

New Post(0)