VBScript – Logical, Comparison, and Concatenation Operators

Logical Operators

To compare two different variables and also to evaluate the Boolean expressions logical operators are used.

Let us have look at these one by one:

AND

expression1 AND expressions2

There will be four different combinations,

  1. E1 AND E2 (E1=True, E2=True) = True.  If both the expressions are true then the result will be true.
  2. E1 AND E2 (E1=True, E2=False or vice versa)= False. If any one of the expression is false then result will be false.
  3. E1 AND E2 (E1=False, E2=False) = False. If both the expressions are false then the result will be false.

OR

expression1 OR expression2

Different combinations are as follows:

  1. E1 OR E2 (E1=True, E2=True) = True.  If both the expressions are true then the result will be true.
  2. E1 OR E2 (E1=True, E2=False or vice versa)= True. If any one of the expression is true then result will be true.
  3. E1 OR E2 (E1=False, E2=False) = False. If both the expressions are false then the result will be false.

NOT

This is a negation operator.

  1. NOT(True) = False
  2. NOT(False) = True

XOR

Different combinations for this operator are as follows:

  1. E1 OR E2 (E1=True, E2=True) = False.  If both the expressions are true then the result will be false.
  2. E1 OR E2 (E1=True, E2=False or vice versa)= True. If any one of the expression is true then result will be true.
  3. E1 OR E2 (E1=False, E2=False) = False. If both the expressions are false then the result will be false.

Comparison Operators

These are used to compare two variables or expressions

Operator Usage
> greater than
< less than
= equal to
<> not equal to
<= less than or equal to
>= greater than or equal to

Concatenation Operators

  1. +
  2. &

These are used to concatenate two strings.

We will try to understand the all of the above together with an example. XOR operator is very less used as far as my knowledge.

If (True AND True) Then
    msgbox "Result is True"
End If

if (True AND False) then
msgbox "Result is False"
End If

If (False AND True) then 
msgobx "Result is False"
End If

If (False AND False) then
msgobx "Result is False"
End If

In the above code, only the first condition gets executed as the expression results in true.  All the other will be false hence do not get executed.

Replace AND with OR and XOR and check the results.

A=10
B=20

C=A+B
msgbox "Summation of A & B is: "&C

firstname="Karthik"
lastname="Shetty"

friendname=firstname+" "+lastname

msgbox friendname

The difference between & and + is the former can be used to concatenate same type of variables or the different type of variables but the latter can be used to concatenate only strings as demonstrated in the example.  I don’t think that comparison operators need example as they are self explanatory.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.