Operators in VBScript. Comparison Operators


As the name implies, the comparison operators are used  to compare one or more variables, numbers, and constants and their combination. These operators are used in statement which compares two different numeric expressions and returns a Boolean value of TRUE if the statement is true. If not, the statement returns FALSE. The typical usage is as follows:

expression1 Operator expression2

where expression1 and expression2 are any numeric expression. A description of operators are found in the table below.

Operator To Determine If…
< One expression is less than another expression.
> One expression is greater than another expression.
<= One expression is less than or equal to another expression.
>= One expression is greater than or equal to another expression.
= Two expressions are equal.
<> Two expressions are not equal.

For instance, the < (less than) operator is used to determine if one numeric expression is less than another numeric expression. It has the following form:

expression1<expression2

The examples of the < operators:

a=5
b=3
a>b 'Returns true
b-1>(a+b)*2 'Returns false

The comparison operators are widely used in The If…Then and If…Then…Else conditional statements to control the flow of code based on decisions made within the code (they where discussed last lesson).

You might try them in the following code.

<HTML>
<head>
<script language="VBScript">
<!--
Dim a,b

sub compare a=t1.value if isnumeric(a)=false then 'To check whether the value in the first text box is numeric msgbox "Type a number in the first text box" exit sub end if b=t2.value if isnumeric(b)=false then 'To check whether the value in the second text box is numeric msgbox "Type a number in the second text box" exit sub end if if a>b then 'The comparison by > operator msgbox a & ">" & b elseif a<b then 'The comparison by < operator msgbox a & "<" & b else 'If the values are equal msgbox a & "=" & b end if end sub --></script> </head> <body ondblclick="compare"> 'On double-clicking on the form, the subroutine compare is run

INPUT NUMBERS INTO TEXT BOXES TO COMPARE THEIR VALUES

<p><input type="text" name="t1" size="10"> </p> <p><input type="text" name="t2" size="10"> </p>

The copyright of the article Operators in VBScript. Comparison Operators in VB Script is owned by Maxim Karetnikov. Permission to republish Operators in VBScript. Comparison Operators in print or online must be granted by the author in writing.

Go To Page: 1 2

Articles in this Topic    Discussions in this Topic