Relational operators connect two variables to form a relational expression. A relational expression returns True if it holds, otherwise False.
【Excel VBA】
Relational operators in Excel VBA are shown in Table 3-3, where a and b are specified variables.
Table 3-3 Relational Operators in Excel VBA
Below, specify two numbers and compare their sizes. The sample file path is Samples\ch03\Excel VBA\Relational Operators.xlsm.
Sub Test2()
Dim intA As Integer
Dim intB As Integer
intA = 10
intB = 20
Debug.Print intA > intB ' Compare if intA is greater than intB
End Sub
Running the procedure outputs False in the Immediate Window.
If comparing two strings, characters are compared one by one based on their ASCII codes. The sample file path is Samples\ch03\Excel VBA\Relational Operators.xlsm.
Sub Test3()
Dim strA As String
Dim strB As String
strA = "abc"
strB = "adc"
Debug.Print strA < strB ' Compare strings
End Sub
Running the procedure outputs True. The first characters of both strings are the same; the ASCII code of the second character in strA is smaller than that in strB.
【Python】
Relational operators in Python are shown in Table 3-4, where a and b are specified variables.
Table 3-4 Relational Operators in Python
Continued Table 3-4
Below, compare the sizes of two numbers:
>>> a = 10; b = 20
>>> a > b
False
Compare two strings:
>>> a = 'abc'; b = 'adc'
>>> a < b
True
Note: For relational operations with more than two variables, a single expression can be used. For example:
>>> a = 3; b = 2; c = 5; d = 9
>>> b < a < c < d # Chain comparison
True
>>> a = 3; b = 3; c = 3
>>> a == b == c == 3 # Chain equality check
True