Lesson Six, Select Case Decision Structure

In the last lesson we looked at making decisions based upon the IF...ElseIF...Else decision logic.  This worked well when we wanted to test a value and if it was true then do something but if it wasn't true then we wanted to test the value again.

For example:

If dayoftheweek = "Sunday" then
    message = "Today is the first day of the week"
ElseIf dayoftheweek = "Monday" then
    message = "Today is the second day of the week"
etc.

If the day was Sunday then a message would be stored.  The program then would skip to the "end IF" line of code and start interpreting the next lines of code.  We use the nested if decision to make the evaluation of many decisions.  Values are tested until "true" then the testing stops.

There's another way we can do this same decision but that's a little easier to understand and to code.  It's call the Select Case decision structure.

This structure does the same type of testing of values and also stops when a test evaluates as "true".  When we code it, it looks like this:

<Script Language = "vbscript">
Dim Name                 '<--- We create a place in memory called "Name"
Name = "Tom"           '<--- We place the text "Tom" into that place in memory

Select Case Name     '<---- We start the Select Case and declare that we want to compare what's stored in "Name" to the selections.

Case "Bill"                '<---- The program compares what is in the place in memory called "Name" ("Tom").  It does not match so it skips to the next Case
Document.write "Bill is a great name"      '<--- This would be displayed if "Bill" was stored in the place in memory called "Name"

Case "Tom"            '<----The program compares what is in the place in memory called "Name" ("Tom").  It does match so it runs the next line of code
Document.write "Tom is a great name"   '<--- This will be displayed since "Tom" was stored in the place in memory called "Name".
                                                                        ' All lines of code are skipped until the line below "End Select"
Case Else               '<--- "Case Else will evaluate true if what is stored in "Name" does not match any of the other cases (case "Bill" or case "Tom")
Document.write "No matching name"

End Select
Document.write "my program is all done now"
</Script>