FunctionX Tutorials Logo

Conditional Statements

Conditional Operators

Conditional statements allow you to control the flow of execution of a script or one of its sections. To do this, you use some keywords and associate them with expressions. Depending on the outcome of the checking process and other comparison operations, you can take appropriate actions. 

Most of the conditional statements perform comparisons and act depending on the outcome of such comparisons. To assist you in making such comparisons, the VBScript language is equipped with special operators that can act on natural numbers, decimal numbers, or strings.     

Equality =

We previously used the assignment operator = to give a value to a variable. Although the assignment operator works on two operands, one on the left and another on the right of the operator, it doesn't mean that both operands are equal. It is only used to give a new value, the right value, to the left value. Because of that, the left operand must never be a numeric value, although both operands can be variables.

If you want to find out whether two variables hold the same value, you should use the equality operator. This is performed with = and its syntax is:

Variable1 = Variable2

To perform this operation, the browser (actually the interpreter) compares the operands on both sides of the = operator. If both operands hold the same value, the comparison renders a value of true. Otherwise, the comparison renders false.

Inequality <>

To compare two variables in order to find out whether they are different, you can use the inequality operator <> whose syntax is:

Variable1 <> Variable2

When performing this operation, the browser compares the values of Variable1 and Variable2. If their values are different, which means that they are not equal, the comparison results in a true value (very important to understand). If they are equal, the result of the comparison is false (observe the contrast with the equality operator =).

Less Than <

If you want to find out whether one value is less than another, use the "less than" operator <. Its syntax is:

Variable1 < Variable2

The browser compares the values held by Variable1 and Variable2. If the value held by Variable1 is less than that of Variable2, the comparison would produce a true value. Otherwise, the result is rendered false.

Decision Makers: The If...Then Statement

The If...Then statement examines the truthfulness of an expression. Structurally, its formula is:

If ConditionIsTrue Then Statement

Therefore, the program will examine a Condition. This condition can be a simple expression or a combination of expressions. If the Condition is true, then the program will execute the Statement.

There are two ways you can use the If...Then statement. If the conditional formula is short enough, you can write it on one line, like this:

If Condition Then Statement

If there are many statements to execute as a truthful result of the condition, you should write the statements on alternate lines. Of course, you can use this technique even if the condition you are examining is short. In this case, one very important rule to keep is to terminate the conditional statement with End If. Here is an example:

If Condition Then
    Statement
End If

Here is another example:

If Condition Then
    Statement1
    Statement2
    Statementn

End If

 

Practical Learning: Using the if Condition

  1. Start your text editor
  2. In the empty file, type the following:
     
    <html>
    <head>
    <title>Slockum Enterprises - User Account</title>
    <SCRIPT LANGUAGE="VBScript">
    <!--
    Sub CheckPassword()
      Dim Password, ConfPassword, Result
      Password = Document.frmRegistration.txtPassword.Value
      ConfPassword = Document.frmRegistration.txtConfirmPass.Value
    
      If Password <> ConfPassword Then
        Document.frmRegistration.txtResult.Value = "Your Passwords Do Not Match"
      End If
    End Sub
    -->
    </SCRIPT>
    </head>
    <body>
    <h1>Slockum Enterprises</h1>
    <p>In order to access this site, you must be a member. Please create an acount.</p>
    
    <form name="frmRegistration">
      <blockquote>
        Full Name:<input type="text" name="txtFullName" size="32"><br>
        Username: <input type="text" name="txtUsername" size="15"><br>
        Password: <input type="password" name="txtPassword" size="15"><br>
        Confirm:  <input type="password" name="txtConfirmPass" size="15"><br>
        Result: <input type="text" name="txtResult" size="40">
        <blockquote><blockquote>
          <input type="button" value="Send It" onClick="CheckPassword">
        </blockquote></blockquote>
      </blockquote>
    </form>
    
    </body>
    </html>
  3. Save the file as condition1.htm in the vbstutorial folder.
  4. Preview the file in your browser.
  5. After previewing the page, return to your text editor.
  6. To improve your form's appearance, change the file as follows:
     
    <html>
    <head>
    <title>Slockum Enterprises - User Account</title>
    <SCRIPT LANGUAGE="VBScript">
    <!--
    Sub CheckPassword()
      Dim Password, ConfPassword, Result
      Password = Document.frmRegistration.txtPassword.Value
      ConfPassword = Document.frmRegistration.txtConfirmPass.Value
    
      If Password <> ConfPassword Then
        Document.frmRegistration.txtResult.Value = "Your Passwords Do Not Match"
      End If
    End Sub
    -->
    </SCRIPT>
    </head>
    <body>
    <h1>Slockum Enterprises</h1>
    <p>In order to access this site, you must be a member. Please create an acount.</p>
    
    <form name="frmRegistration">
      <table border="0" width="500" cellpadding="0" cellspacing="0">
        <tr>
          <td width="25%">First Name:</td>
          <td width="25%"><input type="text" name="txtFirstName" size = "15"></td>
          <td width="25%">Last Name:</td>
          <td width="25%"><input type="text" name="txtLastName" size = "15"></td>
        </tr>
        <tr>
          <td width="25%">Username:</td>
          <td width="25%"><input type="text" name="txtUsername" size="15"></td>
          <td width="25%">E-Mail Address:</td>
          <td width="25%"><input type="text" name="txtEMailAddress" size = "15"></td>
        </tr>
        <tr>
          <td width="25%">Password:</td>
          <td width="25%"><input type="password" name="txtPassword" size="15"></td>
          <td width="25%">Confirm Password:</td>
          <td width="25%"><input type="password" name="txtConfirmPass" size="15"></td>
        </tr>
        <tr>
          <td width="25%">&nbsp;</td>
          <td width="75%" colspan="3"><input type="button" value="Send It" onClick="CheckPassword()"></td>
        </tr>
        <tr>
          <td width="25%">Result:</td>
          <td width="75%" colspan="3"><input type="text" name="txtResult" size="40"></td>
        </tr>
      </table>
    </form>
    
    </body>
    </html>
  7. Save and preview the file in the browser:
     
  8. After previewing the form, return to your text editor.

Decision Makers: The If...Then...Else Statement

The If...Then statement offers only one alternative: to act if the condition is true. Whenever you would like to apply an alternate expression in case the condition is false, use the If...Then...Else statement. The formula of this statement is:

If ConditionIsTrue Then
    Expression1
Else
    Expression2
End If

 

Decision Makers: The If...Then...ElseIf Statement

The If...Then...ElseIf statement acts like the If...Then...Else, except that it offers as many choices as necessary. The formula is:

If Condition1 Then
    Statement1
ElseIf Condition2 Then
    Statement2
ElseIf Conditionk Then
    Statementk
End If

The program will first examine Condition1. If Condition1 is true, the program will execute Statment1 and stop examining conditions. But if Condition1 is false, the program will examine Condition2 and act accordingly. Whenever a condition is false, the program will continue examining the conditions until it finds one. Once a true condition has been found and its statement executed, the program will terminate the conditional examination at End If.

Decision Makers: The Select Case Statement

If you have a large number of conditions to examine, the If...Then...Else will go through each one of them, which could take long (although usually transparent to the user). VBScript offers the alternative of jumping to the statement that applies to the state of the condition.

The formula of the Select Case is:

Select Case Expression
    Case Expression1
        Statement1
    Case Expression2
        Statement2
    Case Expressionk
        Statementk
End Select

The interpreter will examine the Expression and evaluate it once. Then it will compare the result of this examination with the Expressionn of each case. Once it finds one that matches, it would execute the corresponding Statement.

If you anticipate that there could be no match between the Expression and one of the Expressions, you can use a Case Else statement at the end of the list. The statement would then look like this:

Select Case Expression
    Case Expression1
        Statement1
    Case Expression2
        Statement2
    Case Expressionk
        Statementk
    Case Else
        Statementk
End Select

 

Previous Copyright © 2002-2015 FunctionX Next