Decision Structures

<< Click to Display Table of Contents >>

Navigation:  Reference (Scripting) > VBScript Basics > Control Structures >

Decision Structures



Use an If...Then structure to execute one or more statements conditionally. You can use either a single-line syntax or a multiple-line block syntax:


If condition Then statement


If condition Then


End If


The condition is usually a comparison. If condition is True, VBScript executes all the statements following the Then keyword. You can use either single-line or multiple-line syntax to execute just one statement conditionally (these two examples are equivalent):


If anyDate < Now Then anyDate = Now


If anyDate < Now Then

  anyDate = Now

End If


Notice that the single-line form of If...Then does not use an End If statement. If you want to execute more than one line of code when condition is True, you must use the multiple-line block If...Then...End If syntax.




Use an If...Then...Else block to define several blocks of statements, one of which will execute:


If condition1 Then


[ElseIf condition2 Then

[statementblock-2]] ...



End If



Select Case


VBScript provides the Select Case structure as an alternative to If...Then...Else for selectively executing one block of statements from among multiple blocks of statements. A Select Case statement provides capability similar to the If...Then...Else statement, but it makes code more readable when there are several choices.


' Example

Select Case Weekday(now)

  Case 2

     DL.AddComment "Monday"

  Case 3    

     DL.AddComment "Tuesday"

    Case 4

     DL.AddComment "Wednesday"

  Case 5    

     DL.AddComment "Thursday"

  Case 6    

     DL.AddComment "Friday"

  Case Else  

     DL.AddComment "Weekend!"

End Select