Constants, Operators, and Precedence of Operators in VBScript

This is tutorial #3 in VBScript training series.

In my previous tutorial, we learned about ‘Variables’ in VBScript‘. In this tutorial, I will be covering Constants, Operators and Operators Precedence in the VBScript which play an important role in VBScript. Hence it is advisable to have a good understanding of these concepts along with all the various aspects involved in them.

This will also help you to understand all the upcoming topics that are covered in this VBScript series.

This tutorial will give you a complete overview of Constants, Operators & Operators Precedence and their types along with simple examples for your easy understanding.

VBScript

Constants, Operators & Operators Precedence in VBScript

Constants are named memory locations within a program that never changes their values during the execution of the script.

Operators, as the name suggests, are used for performing some operations on values or we can say these are used to change the variables and values.

When working with an expression, there are some rules that are to be followed to in order to evaluate each part of the expression in the VBScript language. And any predetermined order that is followed to evaluate the various operations that are performed is known as Operator Precedence.

Moving forward we will discuss them all in detail.

Declaring and Assigning Values to Constants

Constants are declared in the same manner as Variables, but with a small difference that the value of the constant remains same throughout its lifetime i.e., you cannot change its value, unlike variable.

You can use ‘const’ keyword to declare Constants in your script. Constants can be declared as public or private depending on its usage. If it’s declared Public then it can be used everywhere in a particular script and also in all the other scripts and procedures whereas if it’s declared as Private then it can be used only in one particular script in which you are working including its procedures and classes.

Using Const keyword with some meaningful names, you can create numeric, string and date type constants and also assign values to them.

Let’s understand the declaration and usage of a Constant with the help of a Simple Example:

<html>
<head>
<title>Let’s learn assigning values to constants</title>
</head>
<body>
<script language=”vbscript” type=”text/vbscript”>
const val = 10
const val1 = “Hello Everyone”
const val2 = #09/09/1987#
Msgbox val ‘this will show 10 in the message box
Msgbox val1 ‘this will show Hello everyone in the message box
Msgbox val2 ‘this will show 09/09/1987 in the message box
</script>
</body>
</html>

Constants

These are User Defined Constants. There are various predefined Constants that are provided by the VBScript in order to help the users to directly use them in the scripts without any declaration as they are already defined in the VBScript.

Let’s take a look at some of the important VBScript Constants.

VBScript Data Type Constants

Given below are some of the Data Type Constants in the VBScript.

  • vbEmpty: This is used for a data type which is not initialized with the value as 0.
  • vbNull: This is used when there is no valid data with the value as 1.
  • vbBoolean: This is used for a Boolean data type with the value as 11.
  • vbByte: This is used for byte data type with the value as 17.
  • vbInteger: This is used for integer data type with the value as 2.
  • vbLong: This is used for the long data type with the value as 3.
  • vbSingle: This is used for a single data type with the value as 4.
  • vbDouble: This is used for a double data type with the value as 5.
  • vbDate: This is used for date data type with the value as 7.
  • vbString: This is used for string data type with the value as 8.
  • vbObject: This is used for object data type with the value as 9.
  • vbArray: This is used for array data type with the value as 8192.

VBScript String Constants

Given below are some of the String Constants in the VBScript.

  • vbCr: This is used for print and display functions representing a carriage return character, basically for the purpose of returning to the beginning of the line. Value of this is Chr(13).
  • vbCrLf: This is also used for print and display functions representing a carriage return with line feed character having values as Chr(13) & Chr(10). This works in the same manner as in the above case of pressing the Enter key.
  • vbformfeed: This is used for ‘form feed’ which means for advancing downwards to the next page or in more common terms, works as a page separator. This has the value of Chr(12) and it does not work in OS Windows.
  • vbLf: This is used for advancing downwards to the next line i.e. to go to the next line. This represents a line feed character with the value of Chr(10).
  • vbNewLine: This is used at times in the combination of vbCrLf and vbLf i.e. Chr(13) & Chr(10) and sometimes only vbLf works i.e. Chr(10). This is a platform-dependent one.
  • vbNullChar: This represents the character which is having the value as 0. This has its value as ‘0’.
  • vbNullString: This represents a string which is having value 0. This is not equivalent to zero length string
  • vbTab: This is used for providing horizontal tab, having its value as Chr(9).

VBScript Date and Time Constants

Given below are some of the Date and Time Constants in the VBScript.

  • vbSunday: This represents the week of the day ‘Sunday’ holding the value as 1. If you want to work in the script having the usage of Sunday then can use this constant.
  • vbMonday: This represents the week of the day ‘Monday’ holding the value as 2. If you want to work in the script having the usage of Monday then can use this constant.
  • vbTuesday: This represents the week of the day ‘Tuesday’ holding the value as 3. If you want to work in the script having the usage of Tuesday then can use this constant.
  • vbWednesday: This represents the week of the day ‘Wednesday’ holding the value as 4. If you want to work in the script having the usage of Wednesday then can use this.
  • vbThursday: This represents the week of the day ‘Thursday’ holding the value as 5. If you want to work in the script having the usage of Thursday then can use this constant.
  • vbFriday: This represents the week of the day ‘Friday’ holding the value as 6. If you want to work in the script having the usage of Friday then can use this constant.
  • vbSaturday: This represents the week of the day ‘Saturday’ holding the value as 7. If you want to work in the script having the usage of Saturday then can use this constant.
  • vbFirstFourDays: This makes use of the week that has at least 4 days in the new year, holding the value as 2.

Let’s see a simple Example using some of the user-defined constants that we have discussed above.

<html>
<head>
<title>Let’s see the usage of user defined constants</title>
</head>
<body>
<script language=”vbscript” type=”text/vbscript”>
Dim val, val1, val2
val = “abcd”
val1=”hello” & vbNewLine & “how are you?”
val2 =WeekdayName(1)
Select Case VarType(val)
Case vbEmpty
Msgbox “this is an empty variable”
Case vbNull
Msgbox “this is a null variable”
Case vbString
Msgbox “this is a string variable”
Case vbByte
Msgbox “this is a variable of byte type”
End Select
Msgbox val1 ‘this will show result as follows as vbNewLine is used:
‘hello
‘how are you?
Msgbox val2 ‘this will show Sunday in the message box
</script>
</body>
</html>

DataTypes

Note: In the above example in val2, I have passed ‘1’ in weekdayname which is same as passing as vbSunday. ‘VarType’ is a function which returns a subtype of the variable. I will discuss this in functions in one of my next tutorials.


Let’s move on to Operators now.

Different Types of Operators

Operators are very important in any programming language as without them the tasks cannot be performed.

If you want to perform a simple operation of multiple then the following is the way to do so:

multiply = a * b

Here, a, b and multiply are the Operands while ‘=’ and ‘*’ are the Operators.

There are mainly 4 types of Operators in the VBScript language.

Let’s discuss them in detail by taking 2 operands as 1 and 2.

#1) Arithmetic Operators

All of us are already aware of arithmetical operators in Mathematics, same applies here too.

Different arithmetic operations are:

  • Addition: This performs addition of 2 operands using ‘+’ sign. In our case, addition will be 1 + 2 = 3.
  • Subtraction: This performs subtraction among 2 operands using ‘-‘sign and hence in our case, the result of the subtraction will be 1-2 = -1.
  • Multiplication: This performs multiplication using ‘*’ among the 2 operands. Here, it is 1 * 2 = 2.
  • Division: This performs division of the numerator by the denominator using ‘/’. In our case, if we assume numerator is 2 and denominator is 1 then the result will be 2/1 = 2.
  • Modulus: This is not used very often but it’s good to know about this. This provides ‘remainder’ as a result of division using ‘%’ operator i.e. after dividing 2 by 1, the remainder will be 0 and hence this will give the result as 0.
  • Exponential: This is used to calculate exponential using ‘^’ operator. In our case, 2^1 will give the exponential result as 2.

#2) Comparison Operators

We are all already aware of some of the comparison operators in Mathematics, let’s take a look at the various comparison operators in the VBScript language using values as 1 and 2 respectively.

Various Comparison Operators in the VBScript:

  • Equal: This is used to compare if the values of any 2 operands are equal or not. It produces true if the value of both matches otherwise false. Uses ‘==’ notation i.e. in our case, if we check 1 == 2 then it will give False.
  • Not Equal: This is the reverse of above one i.e. this checks if the values of both the operands do not match and produces true if the condition satisfies using ‘<>’ symbol. In our case, if we match 1<> 2 then it will give True.
  • Greater than: This checks which of the value out of any of the 2 operands is greater than the other one. This uses ‘>’ symbol and verifies if the left-hand side operand is greater than the right-hand side and produces True if this happens. In our case, if we verify 1>2 then this will produce False.
  • Less than: This verifies if the operand on the right-hand side is greater than the one on the left-hand side and produces True if this happens. This uses ‘<’ symbol. In our case, if we verify 1<2 then this will produce True.
  • Greater than, Equal to: This verifies if the operator on the left-hand side is either greater than or equal to the operand on the right-hand side and if this happens, this produces True. ‘>=’ symbol is used for this. In our case, if we check 1 >=2 then this will produce False as neither 1> 2 nor 1=2.
  • Less than, Equal to: This verifies if the operator on the right-hand side is either greater than or equal to the operand on the left-hand side and if this happens, this produces True. ‘<=’ symbol is used for this. In our case, if we check 1 <=2 then this will produce True as 1 < 2 even if 1 is not equal to 2.

#3) Logical Operators

Most of us are already aware of most of the logical operators like AND, OR, etc. and we have also used these in many programming languages. Let’s see the list of various logical operators in VBScript language using 2 variables as x and y with values as 1 and 2 respectively.

Logical Operators include:

  • AND: This is the logical AND operator and this produces True only when both the conditions are true or when both the conditions are satisfied i.e. if in our case, I take one condition as (x>0) AND (x==y) then this will produce False as both the conditions are not true. 1> 0 is true but 1=2 is not true, hence the result is False.
  • OR: This is the logical OR operator and this produces True even if one of the conditions is true or when either of the conditions is satisfied i.e. if in our case, I take one condition as (x>0) OR (x==y) then this will produce True as one of the condition is true . 1> 0 is true, hence the result is True.
  • NOT: This is the logical NOT operator and this works to change the value of the operand or I can say this changes the state of the condition i.e. if the condition is True then this will change it to False and vice versa. If the condition is NOT (x>0) then this will produce False as 1>0 which is true and NOT of true is false.

#4) Concatenation Operators

There are 2 Concatenation operators in the VBScript. As the name itself suggests, concatenation means joining. This works for numeric and string values.

Let’s see its operators as follows:

  • +: This is our arithmetic operator ‘+’ only. If we take 2 strings as “Good” and “Bad” then this operator will concatenate this to Good + Bad = GoodBad and if we take 2 numeric values as 1 and 2 so concatenation in this case using ‘+’ will be 1 + 2 = 3.
  • &: This is our normal ‘&’ symbol. This concatenates 2 values using this symbol. If we consider the above example itself, then String concatenation will be Good & Bad = GoodBad and Numeric concatenation will be 1 & 2 = 12

Let’s clearly understand working with the Operators with the help of a Simple Example:

<html>
<head>
<title>Let’s see usage of different type of operators in the VBScript</title>
</head>
<body>
<script language=”vbscript” type=”text/vbscript”>
Dim val, val1, val2, val3
Const a = 5
Const b = 10
Const c = “hey”
val = a + b
val1 = (a == b) OR (a<=10)
val2 = a & c
val3 = a & b
Msgbox val ‘this will give result as 15
Msgbox val1 ‘this will give result as True
Msgbox val2 ‘this will give result as 5hey
Msgbox val3 ‘this will give result as 510
</script>
</body>
</html>

Operations

We have seen all about the Operators. Next, let’s discuss Precedence of Operators in the VBScript.

Operators Precedence

It’s become very important to follow the precedence of operators when there are various operators in a single expression. Hence there is a need of some predetermined order on the basis of which expression can be evaluated easily and quickly.

We have seen regarding the various types of operators, and as per precedence law,

  • Arithmetic Operators are evaluated firstly as compared to the other operators.
  • Next comes the turn of Comparison Operators for evaluation.
  • Lastly, Logical Operators are evaluated.

There is a fixed order for arithmetic operators in which they are evaluated.

Order of Arithmetic Operators

  • Exponentiation
  • Multiplication
  • Division
  • Modulus
  • Addition and Subtraction
  • Concatenation

Logical Operators are Evaluated as

  • NOT
  • AND
  • OR
  • XOR

Note: If you use parenthesis in an expression then its precedence is above all of these and the expression which is held inside the bracket is evaluated first.

I will explain this concept with help of a Simple Example

<html>
<head>
<title>Let’s understand about precedence of operators</title>
</head>
<body>
<script language=”vbscript” type=”text/vbscript”>
Dim a
Dim b
a=4+8/8*2^1
b= (4+8)/ (8*2) ^1
Msgbox a ‘this will show 4.5 in the message box
Msgbox b ‘this will show 0.75 in the message box
</script>
</body>
</html>

Operators Precedence

Conclusion

Through this tutorial, we have discussed all about Constants, Operators, and Precedence of Operators in the VBScript. I tried to cover all the main topics that are involved in it. And I am sure that this would have given you a great knowledge of the concept.

Next Tutorial #4: We will discuss Conditional Statements in VBScript in my next tutorial.

Stay tuned and let us know if you have any questions about this tutorial.