Conditional statementselse ...code that displays the text end if What if you had a few other cases to test? You can do as many tests as you want by simply placing ElseIf statements between the first If statement and the End If statement. Say, you are building a Data Base for selling cars and the model of car should be associated with a certain picture. The syntax of the corresponding code might be as follows: If Ford = True Then ...code that shows the picture of Ford ElseIf Mazda=True ...code that displays the picture of Mazda end if Notice that only one of the conditions can be true. If you want to execute code for more than one of these conditions, you cannot use this control structure. In other words, this structure can execute only one of the conditions. Once it finds the first condition in the list that is true, it executes the code off that branch and ignores all the rest even if they are true. If you try the following code: Ford=True Mazda=True you will get only one message with the word "Mazda". Select CaseIf you have a large statements to test, it is better to use Select statement. This statement makes your code easier to read and interpret than a long list of ElseIf statements. The Select Case structure is defined as follows: Select Case test_expression Case expression-1 ...this is the code that executes if expression-1 matches test_expression Case expression-2 ...this is the code that executes if expression-2 matches test_expression Case expression-3 ...this is the code that executes if expression-1 matches test_expression Case Else ...this is the code that executes if expression which matches test_expression is not found End Select expression-1, expression-2,and expression-3 are one or more expressions that must match test_expression in order for the code below each Case statement to execute. As you can see, the same condition is evaluated throughout the structure. Only one case is executed when VBScript looking through them. If more than one case matches, only the first one is executed. If none of the cases match, the code underneath the Case Else section is executed. The Case Elsesection is optional. VBScript won't do anything if none of the cases match.
The copyright of the article Conditional statements in VB Script is owned by Maxim Karetnikov. Permission to republish Conditional statements in print or online must be granted by the author in writing.
Articles in this Topic
Discussions in this Topic
|