Home

Managing Conditional Statements

 

Fundamentals of Managing Conditional Statements

 

Conditional Nesting

So far, we have learned to create normal conditional statements and loops. Here is an example:

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Private Sub RequestNumber()
        Dim Number%

        Number% = 3

        If Number% <= 5 Then
            Response.Write(Number%)
        End If

End Sub

</script>
<title>Exercise</title>

</head>
<body>

<%
    RequestNumber()
%>

</body>
</html>

When this program runs, if the supplied number is lower than 5 (included), a message box would display that number. If the given number is higher than 5, the program would end but would not display it. In a typical program, after validating a condition, you may want to take action. To do that, you can create a section of program inside the validating conditional statement. In fact, you can create a conditional statement inside of another conditional statement. This is referred to as nesting a condition.

The Goto Statement

The Goto statement allows a program execution to jump to another section of a procedure in which it is being used. In order to use the Goto statement, insert a name on a particular section of your procedure so you can refer to that name. The name, also called a label, is made of one word and follows the rules we have applied to names (the name can be anything), then followed by a colon ":".

The following program uses a For loop to count from 2 to 18, but when it encounters 10, it jumps to a designated section of the program:

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
        Dim i As Integer

        For i = 2 To 18 Step 1
            If i = 10 Then
                GoTo StoppingHere
            End If
            Response.Write("Value: " & i & "<br />")
        Next

StoppingHere:
        Response.Write("The execution jumped here.")
%>

</body>
</html>

This would produce:

Go To

In the same way, you can create as many labels as you judge them necessary in your code and refer to them when you want. 

Negating a Conditional Statement

So far, we have learned to write a conditional statement that is true or false. You can reverse the true (or false) value of a condition by making it false (or true). To support this operation, the Visual Basic language provides an operator called Not. Its formula is:

Not Expression

When writing the statement, type Not followed by a logical expression. The expression can be a simple Boolean expression. Here is an example:

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
        Dim IsMarried As Boolean

        Response.Write("Is Married: " & IsMarried & "<br />")
        Response.Write("Is Married: " & Not IsMarried)

%>

</body>
</html>

This would produce:

Not

In this case, the Not operator is used to change the logical value of the variable. When a Boolean variable has been "notted", its logical value has changed. If the logical value was True, it would be changed to False and vice versa. Therefore, you can inverse the logical value of a Boolean variable by "notting" or not "notting" it.

Now consider the following program we saw in Lesson 11:

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
        Dim IsMarried As Boolean
        Dim TaxRate As Double

        TaxRate = 33.0

        Response.Write("Tax Rate: " & TaxRate & "%<br />")

        IsMarried = True
        If IsMarried = True Then
            TaxRate = 30.65

            Response.Write("Tax Rate: " & TaxRate & "%")
        End If

%>

</body>
</html>

This would produce:

Not

Probably the most classic way of using the NOT operator consists of reversing a logical expression. To do this, you precede the logical expression with the Not operator. Here is an example:

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%

        Dim IsMarried As Boolean
        Dim TaxRate As Double

        TaxRate = 33.0

        Response.Write("Tax Rate: " & TaxRate & "%<br />")

        IsMarried = True
        If Not IsMarried Then
            TaxRate = 30.65

            Response.Write("Tax Rate: " & TaxRate & "%")
        End If

%>

</body>
</html>

In the same way, you can negate any logical expression.

Exiting a Procedure or a Loop

 

Exiting a Procedure

In the conditional statements and loops we have created so far, we assumed that the whole condition would be processed. Here is an example:

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Private Sub ShowNumbers()
    Dim Number As Short

    For Number = 1 To 6
        Response.Write(Number)
	Response.Write("<br />")
    Next
End Sub

</script>
<title>Exercise</title>

</head>
<body>

<%
    ShowNumbers()
%>

</body>
</html>

This would produce:

Exiting a Loop

In some cases, you may want to exit a conditional statement or a loop before its end. To assist with with this, the Visual Basic language provides the Exit keyword. This keyword works like an operator. It can be applied to a procedure or a For loop. Consider the following ShowNames procedure:

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Private Sub ShowNames()
        Response.Write("Patricia Katts<br />")
        Response.Write("Gertrude Monay<br />")
        Response.Write("Hermine Nkolo<br />")
        Response.Write("Paul Bertrand Yamaguchi")
End Sub

</script>
<title>Exercise</title>

</head>
<body>

<%
    ShowNames()
%>

</body>
</html>

This would produce:

Exiting a Loop

When the procedure is called, it displays four names. Imagine that at some point you want to ask the compiler to stop in the middle of a procedure. To do this, in the section where you want to stop the flow of a procedure, type Exit Sub. Here is an example:

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Private Sub ShowNames()
        Response.Write("Patricia Katts<br />")
        Response.Write("Gertrude Monay<br />")
        Exit Sub
        Response.Write("Hermine Nkolo<br />")
        Response.Write("Paul Bertrand Yamaguchi")
    End Sub

</script>
<title>Exercise</title>

</head>
<body>

<%
        ShowNames()
%>

</body>
</html>

This time, when the program runs, the ShowNames procedure would be accessed and would start displaying the message boxes. After displaying two, the Exit Sub would ask the compiler to stop and get out of the procedure.

This would produce:

Exiting a Loop

Because a function is just a type of procedure that is meant to return a value, you can use the Exit keyword to get out of a function before the End Function line. To do this, in the section where you want to stop the flow of the function, type Exit Function.

Exiting a For Loop Counter

You can also exit a For loop. To do this, in the section where you want to stop, type Exit For. Here is an example to stop a continuing For loop:

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Private Sub ShowNumbers()
    Dim Number As Short
    
    For Number = 1 To 6
        Response.Write(Number)
	Response.Write("<br />")
        If Number = 4 Then
            Exit For
        End If
    Next
End Sub

</script>
<title>Exercise</title>

</head>
<body>

<%
    ShowNumbers()
%>

</body>
</html>

This would produce:

Exiting a Loop

When this program executes, it is supposed to display numbers from 1 to 6, but an If...Then condition states that if it gets to the point where the number is 4, it should stop. If you use an Exit For statement, the compiler would stop the flow of For and continue with code after the Next keyword.

Exiting a Do Loop

You can also use the Exit operator to get out of a Do loop. To do this, inside of a Do loop where you want to stop, type Exit Do.

 
 
 
 

Logical Conjunction

 

Introduction

As mentioned already, you can nest one conditional statement inside of another.

A Conditional Conjunction

The operator used to perform a logical conjunction is And. By definition, a logical conjunction combines two conditions. To make the program easier to read, each side of the conditions can be included in parentheses.

To understand how logical conjunction works, from a list of real estate properties, after selecting the house type, if you find a house that is a single family home, you put it in the list of considered properties:

Type of House House
The house is single family True

If you find a house that is less than or equal to $550,000, you retain it:

Price Range Value
$550,000 True

For the current customer, you want a house to meet BOTH criteria. If the house is a town house, based on the request of our customer, its conditional value is false. If the house is less than $550,000, such as $485,000, the value of the Boolean Value is true:

If the house is a town house, based on the request of our customer, its conditional value is false. If the house is more than $550,000, the value of the Boolean Value is true. In logical conjunction, if one of the conditions is false, the result if false also. This can be illustrated as follows:

Type of House House Value Result
Town House $625,000 Town House AND $625,000
False False False

Suppose we find a single family home. The first condition is true for our customer. With the AND Boolean operator, if the first condition is true, then we consider the second criterion. Suppose that the house we are considering costs $750,500: the price is out of the customer's range. Therefore, the second condition is false. In the AND Boolean algebra, if the second condition is false, even if the first is true, the whole condition is false. This would produce the following table:

Type of House House Value Result
Single Family $750,500 Single Family AND $750,500
True False False

Suppose we find a townhouse that costs $420,000. Although the second condition is true, the first is false. In Boolean algebra, an AND operation is false if either condition is false:

Type of House House Value Result
Town House $420,000 Town House AND $420,000
False True False

If we find a single family home that costs $345,000, both conditions are true. In Boolean algebra, an AND operation is true if BOTH conditions are true. This can be illustrated as follows:

Type of House House Value Result
Single Family $345,000 Single Family AND $345,000
True True True

These four tables can be resumed as follows:

If Condition1 is If Condition2 is Condition1
AND
Condition2
False False False
False True False
True False False
True True True

As you can see, a logical conjunction is true only of BOTH conditions are true.

Combining Conjunctions

As seen above, the logical conjunction operator is used to combine two conditions. In some cases, you will need to combine more than two conditions. Imagine a customer wants to purchase a single family house that costs up to $450,000 with an indoor garage. This means that the house must fulfill these three requirements:

  1. The house is a single family home
  2. The house costs less than $450,001
  3. The house has an indoor garage

We saw that when two conditions are combined, the compiler first checks the first condition, followed by the second. In the same way, if three conditions need to be considered, the compiler evaluates the truthfulness of the first condition:

Type of House
A
Town House
False

If the first condition (or any condition) is false, the whole condition is false, regardless of the outcome of the other(s). If the first condition is true, then the second condition is evaluated for its truthfulness:

Type of House Property Value
A B
Single Family $655,000
True False

If the second condition is false, the whole combination is considered false:

A B A And B
True False False

When evaluating three conditions, if either the first or the second is false, since the whole condition would become false, there is no reason to evaluate the third. If both the first and the second conditions are false, there is also no reason to evaluate the third condition. Only if the first two conditions are true will the third condition be evaluated whether it is true:

Type of House Property Value Indoor Garage
A B C
Single Family $425,650 None
True True False

The combination of these conditions in a logical conjunction can be written as A And B And C. If the third condition is false, the whole combination is considered false:

A B A And B C A And B And C
True True True False False

From our discussion so far, the truth table of the combinations can be illustrated as follows:

A B C A And B And C
False Don't Care Don't Care False
True False Don't Care False
True True False False

The whole combination is true only if all three conditions are true. This can be illustrated as follows:

A B C A And B And C
False False False False
False False True False
True False False False
True False True False
False True False False
False True True False
True True False False
True True True True
 

Logical Disjunction: OR

 

Introduction

Our real estate company has single family homes, townhouses, and condominiums. All of the condos have only one level, also referred to as a story. Some of the single family homes have one story, some have two and some others have three levels. All townhouses have three levels.

Another customer wants to buy a home. The customer says that he primarily wants a condo, but if our real estate company doesn't have a condominium, that is, if the company has only houses, whatever it is, whether a house or a condo, it must have only one level (story) (due to an illness, the customer would not climb the stairs). When considering the properties of our company, we would proceed with these statements:

  1. The property is a condominium
  2. The property has one story

If we find a condo, since all of our condos have only one level, the criterion set by the customer is true. Even if we were considering another (type of) property, it wouldn't matter. This can be resumed in the following table:

Type of House House
Condominium True

The other properties would not be considered, especially if they have more than one story:

Number of Stories Value
3 False

We can show this operation as follows:

Condominium One Story Condominium or 1 Story
True False True

Creating a Logical Disjunction

To support "either or" conditions in the Visual Basic language, you use the Or operator. As done for the And operator, to make a logical disjunction easy to read, you can include each statement in parentheses.

Suppose that, among the properties our real estate company has available, there is no condominium. In this case, we would then consider the other properties:

Type of House House
Single Family False

If we have a few single family homes, we would look for one that has only one story. Once we find one, our second criterion becomes true:

Type of House One Story Condominium OR 1 Story
False True True

If we find a condo and it is one story, both criteria are true. This can be illustrated in the following table:

Type of House One Story Condominium OR 1 Story
False True True
True True True

A Boolean OR operation produces a false result only if BOTH conditions ARE FALSE:

If Condition1 is If Condition2 is Condition1 OR Condition2
False True True
True False True
True True True
False False False

Combinations of Disjunctions

As opposed to evaluating only two conditions, you may face a situation that presents three of them and must consider a combination of more than two conditions. You would apply the same logical approach we reviewed for the logical conjunction, except that, in a group of logical disjunctions, if one of them is true, the whole statement becomes true.

 
 
   
 

Previous Copyright © 2009-2010 FunctionX Home