Short-Circuit Operators


Short-circuit operators are logical operators that when evaluating two (or more) conditions, may or may not evaluate the entire statement based upon the resulting evaluation from a previous condition. To explain further, given the case of a logical and statement, as soon as any condition evaluates to False, the entire statement is False, no matter what the rest of conditions yield. Similarly, given the case of a logical or, as soon as a condition results in True, the entire statement is True, no matter what rest of the conditions yield. Short-cricuit operators take advantage of these results and terminate evaluation as soon as a defining result occurs.

The normal And and Or statements in VB are not short-circuit operators. However, VB does provide two short-circuit operators, specifically AndAlso and OrElse. In the case of the AndAlso, as soon as a condition evaluates to False, evaluation of the rest of the statement ceases (short-circuits) at that point and the entire statement evaluates to False. Similarly, in the case of the OrElse, as soon as a condition evaluates to True, evaluation of the rest of the statement ceases and the entire statement evaluates to True.

An example of this can be seen in the VB program as follows:


 Option Explicit On
 Option Strict On

 'program name: ShortCircuit

 Module ShortCircuit

    Function TrueValue(ByVal intOp As Integer) As Boolean

	'function to always return a True value
        Console.Write("Operand" & intOp & "=True ")
        Return True

    End Function

    Function FalseValue(ByVal intOp As Integer) As Boolean

	'function to always return a False value
        Console.Write("Operand" & intOp & "=False ")
        Return False

    End Function

    Sub Main()

	Console.WriteLine(vbCrLf & "Short Circuit Example" & vbCrLf)

        Console.Write("And statement: ")
        If FalseValue(1) And TrueValue(2) Then
	End If
        Console.WriteLine(vbCrLf)

        Console.Write("Or statement: ")
        If TrueValue(1) Or FalseValue(2) Then
        End If
        Console.WriteLine(vbCrLf)

        Console.Write("AndAlso statement: ")
        If FalseValue(1) AndAlso TrueValue(2) Then
        End If
        Console.WriteLine(vbCrLf)

        Console.Write("OrElse statement: ")
        If TrueValue(1) OrElse FalseValue(2) Then
        End If

    End Sub

 End Module

The two functions in the program above simply return True and False values respectively, as well as report what value they are returning. The code in the Main module calls each of these with the two non-short circuit logical operators, followed by the two short circuit logical operators mentioned above.

As you can see from the output below, when the logical And statement encounters a False value, it continues evaluation even though continued evaluation will not change the outcome. The AndAlso statement on the other hand, ceases evaluation as soon as the False value is evaluated. The same can be seen with the Or and the OrElse when it evaluates an initial True value.




Back


©2009, Mark A. Thomas. All Rights Reserved.