Statements
Parent page: VBScript
Conditional Statements Copy Link Copied
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:
1
If
Condition
Then
2
'code
3
Else
If
AnotherCondition
Then
4
'code
5
Else
6
'code
7
End
If
The For Loop
The For Next statement repeatedly loops through a block of code. The basic syntax is:
1
For
counter = start to end
2
' block of code here
3
Next
The Exit For
The Exit For statement exits a For loop prematurely.
1
For
counter = start to end
2
if condition then
Exit
For
3
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:
1
For
Each
ObjectVar in Collection
2
' block of code here
3
Next
The Do Loop
The Do Loop has several loop variations.
1
Do
while until condition
2
' code block
3
Loop
...and;
1
Do
2
' code block
3
Loop
while until condition
...and;
1
Do
2
' code block
3
Loop
The While...WEnd Loop
The While WEnd statement repeatedly loops through a block of code. The basic syntax is:
1
While
until condition
2
' code block
3
WEnd
The Select Case Statement
You can use the SELECT statement if you want to select one of many blocks of code to execute:
1
Select
case payment
2
case
"Cash"
3
msgbox
"pay cash"
4
case
"MasterCard"
5
msgbox
"pay by Mastercard"
6
case
Else
7
msgbox
"Unknown payment method"
8
end select
Expressions and Operators Copy Link Copied
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 |
|
|
|
|
|
|
|
|