语句
Conditional Statements
The main conditional statements supported by VBScript are:
- If...Then
- For Next Loop
- Exit For
- For Each Next
- Do Loop
- While...WEnd
- Select Case
Care needs to be taken to code scripts that avoid infinite loops — that is, ensuring that the conditions will eventually be met.
The If...Then Statement
The syntax is:
If Condition Then
'code
Else If AnotherCondition Then
'code
Else
'code
End If
The For Loop
The For Next statement repeatedly loops through a block of code. The basic syntax is:
For counter = start to end
' block of code here
Next
The Exit For
The Exit For statement exits a For loop prematurely.
For counter = start to end
if condition then Exit For
Next
The For Each Loop
The For Each loop is a variation on the For loop which is designed to iterate through a collection of objects as well as elements in an array. The basic syntax is:
For Each ObjectVar in Collection
' block of code here
Next
The Do Loop
The Do Loop has several loop variations.
Do while until condition
' code block
Loop
...and;
Do
' code block
Loop while until condition
...and;
Do
' code block
Loop
The While...WEnd Loop
The While WEnd statement repeatedly loops through a block of code. The basic syntax is:
While until condition
' code block
WEnd
The Select Case Statement
You can use the SELECT statement if you want to select one of many blocks of code to execute:
Select case payment
case "Cash"
msgbox "pay cash"
case "MasterCard"
msgbox "pay by Mastercard"
case Else
msgbox "Unknown payment method"
end select
Expressions and Operators
An expression is a valid combination of constants, variables, literal values, operators, and function results. Expressions are used to determine the value to assign to a variable, to compute the parameter of a function, or to test for a condition. Expressions can include function calls.
VBScript has a number of logical, arithmetic, Boolean and relational operators. Since these operators are grouped by the order of precedence which is different from the precedence orders used by Basic, C, etc. For example, the AND and OR operators have precedence compared to the relational one.
Arithmetic Operators
|
Addition |
|
Subtraction |
|
Multiplication |
|
Division |
|
Division with an integer result |
|
Exponentiation |
|
Modulo |
Comparison Operators (Lowest Precedence)
|
Test whether equal or not. |
|
Test whether not equal or not. |
|
Test whether less than or not. |
|
Test whether greater than or not. |
|
Test whether less than or equal to or not. |
|
Test whether greater than or equal to or not. |
|
Compares two object reference variables. |
String Operators
|
Concatenation |
Logical Operators
|
Logical NOT |
|
Logical AND |
|
Logical OR |
|
|
|
|
|
|
|
|