Home

Conditional Selections

 

The Select...Case Statement

 

Introduction

If you have a large number of conditions to examine, the If...Then...Else statement will go through each one of them. The Visual Basic language offers the alternative of jumping to the statement that applies to the state of the condition. This is done with the Select and Case keywords.

The formula of the Select Case statement is:

Select Case Expression
    Case Expression1
        Statement1
    Case Expression2
        Statement2
    Case ExpressionX
        StatementX
End Select

The statement starts with Select Case and ends with End Select. The Expression can be Boolean value, a character, a string, a number, a date, a time, an enumeration, or else (an Object type). 

What Case Else?

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

In this case, the statement after the Case Else will execute if none of the previous expressions matches the Expression factor.

Combining Cases

As mentioned in our introduction, the Select Case can use a value other than an integer. For example you can use a character. Here is an example:

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
        Dim Gender As Char

        Gender = "M"

        Select Case Gender
            Case "F"
                Response.Write("Female")
            Case "M"
                Response.Write("Male")
            Case Else
                Response.Write("Unknown")
        End Select
%>

</body>
</html>

Notice that in this case we are using only upper case characters. If want to validate lower case characters also, we may have to create additional case sections for each. Here is an example:

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
        Dim Gender As Char

        Gender = "f"

        Select Case Gender
            Case "f"
                Response.Write("Female")
            Case "F"
                Response.Write("Female")
            Case "m"
                Response.Write("Male")
            Case "M"
                Response.Write("Male")
            Case Else
                Response.Write("Unknown")
        End Select
%>

</body>
</html>

Instead of using one value for a case, you can apply more than one. To do this, on the right side of the Case keyword, you can separate the expressions with commas. Here are examples:

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%

        Dim Gender As Char

        Gender = "F"

        Select Case Gender
            Case "f", "F"
                Response.Write("Female")
            Case "m", "M"
                Response.Write("Male")
            Case Else
                Response.Write("Unknown")
        End Select

%>

</body>
</html>

Validating a Range of Cases

You can use a range of values for a case. To do this, on the right side of Case, enter the lower value, followed by To, followed by the higher value. Here is an example:

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
        Dim Age As Integer
        Age = 24

        Select Case Age
            Case 0 To 17
                Response.Write("Teen")
            Case 18 To 55
                Response.Write("Adult")
            Case Else
                Response.Write("Senior")
        End Select
%>

</body>
</html>

Checking Whether a Value IS

Consider the following code:

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
        Dim Number As Short

        Number = 448

        Select Case Number
            Case -602
                Response.Write("-602")
            Case 24
                Response.Write("24")
            Case 0
                Response.Write("0")
        End Select
%>

</body>
</html>

Obviously this Select Case statement will work in rare cases where the expression of a case exactly matches the value sought for. In reality, for this type of scenario, you could validate a range of values. The Visual Basic language provides an alternative. You can check whether the value of the Expression responds to a criterion instead of an exact value. To create it, you use the Is operator with the following formula:

Is Operator Value

You start with the Is keyword. It is followed by one of the Boolean operators we saw in the previous lessons: =, <>, <, <=, >, or >=. On the right side of the boolean operator, type the desired value. Here are examples:

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
        Dim Number As Short

        Number = -448

        Select Case Number
            Case Is < 0
                Response.Write("The number is negative")
            Case Is > 0
                Response.Write("The number is positive")
            Case Else
                Response.Write("0")
        End Select
%>

</body>
</html>

Although we used a natural number here, you can use any appropriate logical comparison that can produce a True or a False result. You can also combine it with the other alternatives we saw previously, such as separating the expressions of a case with commas.

Select...Case and the Conditional Built-In Functions

With the Select...Case statement, we saw how to check different values against a central one and take action when one of those matches the tag. Here is an example:

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
        Dim Number As UShort, MembershipType As String

        Number = 2

        Select Case Number
            Case 1
                MembershipType = "Teen"
            Case 2
                MembershipType = "Adult"
            Case Else
                MembershipType = "Senior"
        End Select

        Response.Write("Membership Type: " & MembershipType)
%>

</body>
</html>

The Choose() function is another alternative to a Select...Case statement. Once again, consider the syntax of the Choose function:

Public Function Choose( _
   ByVal Index As Double, _ 
   ByVal ParamArray Choice() As Object _
) As Object

This function takes two required arguments. The first argument is equivalent to the Expression of our Select Case formula. Instead of using Case sections, provide the equivalent ExpressionX values as a list of values in place of the second argument. The values are separated by commas. Here is an example:

Choose(Number, "Teen", "Adult", "Senior")

The values of the second argument are provided as a list. Each member of the list uses an index. The first member of the list, which is the second argument of this function, has an index of 1. The second value of the argument, which is the third argument of the function, has an index of 2. You can continue adding the values of the second argument as you see fit.

When the Choose() function has been called, it returns a value of type Object. You can retrieve that value, store it in a variable and use it as you see fit. Here is an example:

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
        Dim Number As UShort, MembershipType As String

        Number = 1

        MembershipType = Choose(Number, "Teen", "Adult", "Senior")

        Response.Write("Membership Type: " & MembershipType)
%>

</body>
</html>
 
 
 
 

Loops Repeaters

 

Introduction

A loop is a technique used to repeat an action. The Visual Basic language presents many variations of loops. They combine the Do and the Loop keywords.

The Do...Loop While Loop

The formula of the Do... Loop While loop is:

Do
    Statement(s)
Loop While Condition

Do...Loop

As you may guess already, the Condition must provide a way for it to be true or to be false. Otherwise, the looping would be executed over and over again.

The Do...Loop Until Statement

An alternative to the Do... Loop While uses the following formula:

Do
    Statement(s)
Loop Until Condition

Once again, the Statement(s) section executes first. Then, the Condition is checked. If the Condition is true, the compiler returns to the Statement(s) section to execute it. This will continue until the Condition becomes false. Once the Condition becomes false, the compiler gets out of this loop and continues with the section under the Loop Until line.

The Do While... Loop Statement

As mentioned above, the Do While... Loop expression executes a statement first before checking a condition that would allow it to repeat. If you want to check a condition first before executing a statement, you can use another version as Do While... Loop. Its formula is:

Do While Condition
  Statement(s)
Loop

In this case, the compiler checks the Condition first. If the Condition is true, the compiler then executes the Statement(s) and checks the Condition again. If the Condition is false, or when the Condition becomes false, the compiler skips the Statement(s) section and continues with the code below the Loop keyword.

The Do Until... Loop Statement

An alternative to the Do While... Loop loop uses the following formula:

Do Until Condition
    Statement(s)
Loop

This loop works like the Do While... Loop expression. The compiler examines the Condition first. If the condition is true, then it executes the Statement(s) section.

Loop Counters

 

Introduction

The looping statements we reviewed above are used when you don't know or can't anticipate the number of times a condition needs to be checked in order to execute a statement. If you know with certainty how many times you want to execute a statement, you can use another form of loops that use the For...Next expression.

The For...To...Next Loop

One of the loop counters you can use is For...To...Next. Its formula is:

For Counter = Start To End
  Statement(s)
Next

Used for counting, the expression begins counting at the Start point. Then it examines whether the current value (after starting to count) is lower than End. If that's the case, it then executes the Statement(s). Next, it increments the value of Counter by 1 and examines the condition again. This process goes on until the value of Counter becomes equal to the End value. Once this condition is reached, the looping stops.

Here is an example:

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Number As Short

    For Number = 5 To 16
        Response.Write(Number)
	Response.Write("<br />")
    Next

    Response.Write("Counting Stopped at: " & Number)
%>

</body>
</html>

This would produce:

Looping

Stepping the Counting Loop

The formula above will increment the counting by 1 at the end of each statement. If you want to control how the incrementing processes, you can set your own, using the Step option. Here is the formula:

For Counter = Start To End Step Increment
  Statement(s)
Next

You can set the incrementing value to your choice. If the value of Increment is positive, the Counter will be added its value. Here is an example:

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Number As Short

    For Number = 5 To 42 Step 4
        Response.Write(Number)
	Response.Write("<br />")
    Next

    Response.Write("Counting Stopped at: " & Number)
%>

</body>
</html>

This would produce:

For

You can also set a negative value to the Increment factor, in which case the Counter will be subtracted the set value.

 
 
   
 

Previous Copyright 2009 FunctionX Next