omron — Looping Through Code

Appendix: VBScript End If End Sub You can add as many ElseIf clauses as you need to provide alternative choices, but extensive use of the ElseIf clauses often becomes cumbersome. A better way to choose between several alternatives is the Select Case statement. Making Decisions with Select Case The Select Case structure provides an alternative to If…Then…ElseIf 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 efficient and readable. A Select Case structure works with a single test expression that is evaluated once, at the top of the structure.

The result of the expression is then compared to the values for each Case in the structure. If there is a match, the block of statements associated with that Case is executed, as in the following example: Select Case Document.Form1.CardType.Options(SelectedIndex).Text Case "MasterCard" DisplayMCLogo ValidateMCAccount Case "Visa" DisplayVisaLogo ValidateVisaAccount Case "American Express" DisplayAMEXCOLogo ValidateAMEXCOAccount Case Else DisplayUnknownImage PromptAgain End Select Notice that the Select Case structure evaluates an expression once at the top of the structure. In contrast, the If…Then…ElseIf structure can evaluate a different expression for each ElseIf statement.

You can replace an If…Then…ElseIf structure with a Select Case structure only if each ElseIf statement evaluates the same expression. Looping Through Code Looping allows you to run a group of statements repeatedly. Some loops repeat statements until a condition is False; others repeat statements until a condition is True. There are also loops that repeat statements a specific number of times. The following looping statements are available in VBScript: • Do…Loop : Loops while or until a condition is True • While…Wend : Loops while a condition is True • For…Next :

Uses a counter to run statements a specified number of times Using Do Loops You can use Do…Loop statements to run a block of statements an indefinite number of times. The statements are repeated either while a condition is True or until a condition becomes True. Repeating Statements While a Condition is True Use the While keyword to check a condition in a Do…Loop statement. You can check the condition before you enter the loop (as shown in the following ChkFirstWhile example), or you can check it after the loop has run at least once (as shown in the ChkLastWhile example). In the ChkFirstWhile procedure, if myNum is set to 9 instead of 20, the statements inside the loop will never run.

In the ChkLastWhile procedure, the statements inside the loop run only once because the condition is already False. Sub ChkFirstWhile() Dim counter, myNum counter = 0 myNum = 20 Do While myNum > 10 myNum = myNum - 1 counter = counter + 1 Loop MsgBox "The loop made " & counter & " repetitions." InduSoft Web Studio Page 671